好的,所以基本上我在这里要做的是读取一个包含单词的文件。其中一些单词在开头和结尾有特殊字符,例如引号、句点、连字符等。我必须计算文件中的所有单词,计算有多少特殊字符,删除它们,然后在没有特殊字符的情况下将每个单词打印在单独的行上。
所以现在我们已经有了目标,我想用下面的代码来解释我的思维过程。所以一开始我在考虑如何使用 switch 语句来删除特定字符,因为我认为你只能在 switch 语句中使用数字,所以我认为使用 ascii 表中的字符的十进制数是最好的方法去。所以我这样做了,它会按原样打印出所有单词。但是,当计算所有特殊字符时,它会完全错误,只计算总数的一小部分。我只是不知道为什么它会这样做,所以任何帮助都将不胜感激!
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class FileWords
{
public static void main( String [] args ) throws IOException
{
Scanner scan = new Scanner(System.in);
System.out.print("Please enter a file name: ");
File file = new File(scan.next() );
Scanner scanFile = new Scanner(file);
String fileContent;
int wordNum = 0, quote = 0, dubQuote = 0, semi = 0, colon = 0, period = 0, comma = 0, hyphen = 0, exclamation = 0, dollar = 0, question = 0, words = 0;
do
{
fileContent = scanFile.next();
wordNum = fileContent.length();
switch ( (fileContent.charAt(0)) )
{
case 34:
dubQuote ++;
fileContent = fileContent.substring( 1 , wordNum + fileContent.indexOf(" ") );
break;
case 36:
dollar++;
fileContent = fileContent.substring( 1 , wordNum + fileContent.indexOf(" ") );
break;
case 39:
quote++;
fileContent = fileContent.substring( 1 , wordNum + fileContent.indexOf(" ") );
break;
case 46:
period++;
fileContent = fileContent.substring( 1 , wordNum + fileContent.indexOf(" ") );
break;
default:
break;
}
wordNum = fileContent.length();
switch ( fileContent.charAt(wordNum - 1) )
{
case 33:
exclamation++;
words++;
fileContent = fileContent.substring( 0 , wordNum + fileContent.indexOf(" ") );
break;
case 34:
dubQuote ++;
words++;
fileContent = fileContent.substring( 0 , wordNum + fileContent.indexOf(" ") );
break;
case 39:
quote++;
words++;
fileContent = fileContent.substring( 0 , wordNum + fileContent.indexOf(" ") );
break;
case 44:
comma++;
words++;
fileContent = fileContent.substring( 0 , wordNum + fileContent.indexOf(" ") );
break;
case 45:
hyphen++;
words++;
fileContent = fileContent.substring( 0 , wordNum + fileContent.indexOf(" ") );
break;
case 46:
period++;
words++;
fileContent = fileContent.substring( 0 , wordNum + fileContent.indexOf(" ") );
break;
case 58:
colon++;
words++;
fileContent = fileContent.substring( 0 , wordNum + fileContent.indexOf(" ") );
break;
case 59:
semi++;
words++;
fileContent = fileContent.substring( 0 , wordNum + fileContent.indexOf(" ") );
break;
case 63:
question++;
words++;
fileContent = fileContent.substring( 0 , wordNum + fileContent.indexOf(" ") );
break;
default:
words++;
break;
}
System.out.println(fileContent);
} // end of do
while (scanFile.hasNext());
System.out.println();
System.out.println("Double Quotes: " + dubQuote);
System.out.println("Single Quotes: " + quote);
System.out.println("Semi-Colons: " + semi);
System.out.println("Colons: " + colon);
System.out.println("Periods: " + period);
System.out.println("Commas: " + comma);
System.out.println("Hyphens: " + hyphen);
System.out.println("Exclamation Points: " + exclamation);
System.out.println("Question Marks: " + question);
System.out.println("Dollar Signs: " + dollar);
System.out.println("Words Found: " + words);
}
}