好吧,基本上我在这里要做的就是读一个包含单词的文件。其中一些单词在开头和结尾都有特殊字符,如引号,句点,连字符等。我必须计算文件中的所有单词,计算有多少特殊字符,删除它们,并在没有特殊字符的单独行上打印出每个单词。
所以现在我们有了目的,我想用下面的编码来解释我的思考过程。所以在开始时我一直在考虑如何使用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);
}
}
答案 0 :(得分:0)
更简单的方法是简单地使用数组遍历每个字符并使用Character.isLetter()方法。如果字符是字母,则将其添加到字符串中,如果不是,则检查其是空格还是新行。如果它的空格或换行符打印字符串并继续跳过该字符,则不是字母或分隔两个单词的东西。这也将检查所有可能的特殊字符,而不仅仅是您当前正在查看的字符。
答案 1 :(得分:0)
为了提供线索,请使用以下表达式:
fileContent.substring(1, wordNum + fileContent.indexOf(" "))
似乎有问题。考虑当您有一个包含以下内容的文件时会发生什么:
“一”
输出结果为:
Double Quotes: 1
Single Quotes: 0
Semi-Colons: 0
Colons: 0
Periods: 0
Commas: 0
Hyphens: 0
Exclamation Points: 0
Question Marks: 0
Dollar Signs: 0
Words Found: 1
因此,报价被错误计算。逻辑错误可以纠正如下:
switch ((fileContent.charAt(0))) {
case 34:
dubQuote++;
fileContent = fileContent.substring(1, wordNum);
break;
case 36:
dollar++;
fileContent = fileContent.substring(1, wordNum);
break;
case 39:
quote++;
fileContent = fileContent.substring(1, wordNum);
break;
case 46:
period++;
fileContent = fileContent.substring(1, wordNum);
break;
default:
break;
}
wordNum = fileContent.length() - 1;
switch (fileContent.charAt(wordNum)) {
case 33:
exclamation++;
words++;
fileContent = fileContent.substring(0, wordNum);
break;
case 34:
dubQuote++;
words++;
fileContent = fileContent.substring(0, wordNum);
break;
case 39:
quote++;
words++;
fileContent = fileContent.substring(0, wordNum);
break;
case 44:
comma++;
words++;
fileContent = fileContent.substring(0, wordNum);
break;
case 45:
hyphen++;
words++;
fileContent = fileContent.substring(0, wordNum);
break;
case 46:
period++;
words++;
fileContent = fileContent.substring(0, wordNum);
break;
case 58:
colon++;
words++;
fileContent = fileContent.substring(0, wordNum);
break;
case 59:
semi++;
words++;
fileContent = fileContent.substring(0, wordNum);
break;
case 63:
question++;
words++;
fileContent = fileContent.substring(0, wordNum);
break;
default:
words++;
break;
}
答案 2 :(得分:0)
好吧所以我明白了。问题是在第一个switch语句中我为fileContent分配了一个不正确的值。我所要做的就是删除+ fileContent.indexOf(“”)而我是金色的。