我正在创建一个程序,它将文件名作为参数并检查文本中的回文。该程序还将根据用户输入替换字母或根据用户输入删除单词。出于某种原因,
下的扫描仪public void letter
and
public void word
无法正常工作。
这是程序运行后我收到的错误,用户输入:
What would you like to do? Here are your options:
Press 1 to Print all palindromes
Press 2 to Replace any letter
Press 3 to remove all occurences of a word
Press 4 to exit
2
What letter would you like to remove?
e
What letter would you like to add?
r
[ ]
What would you like to do? Here are your options:
Press 1 to Print all palindromes
Press 2 to Replace any letter
Press 3 to remove all occurences of a word
Press 4 to exit
这是我得到的错误
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at Homework6.main(Homework6.java:28)**
代码:
import java.util.ArrayList;
import java.util.Scanner;
import java.util.*;
import java.io.File;
public class Test {
static ArrayList<String> strings = new ArrayList<String>();
public static void main(String[] args) {
Test test2 = new Test();
try {
Scanner scanner = new Scanner(new File(args[0]));
while (scanner.hasNext()) {
strings.add(scanner.next());
}
} catch (Exception e) {
}
//ArrayList<String> a = new ArrayList<String>(strings);
while (true) {
System.out.println("\nWhat would you like to do? Here are your options: \nPress 1 to Print all palindromes \nPress 2 to Replace any letter \nPress 3 to remove all occurences of a word \nPress 4 to exit\n");
Scanner s = new Scanner(System.in);
String command = s.next();
if (command.equals("1")) {
test2.pal();
} else if (command.equals("2")) {
test2.letter();
} else if (command.equals("3")) {
test2.word();
} else if (command.equals("4")) {
System.exit(0);
}
}
}
public void pal() {
int a;
int c;
// boolean isPalindrome() ;
for (a = 0; a < strings.size(); a++) {
String replace = strings.get(a);
replace = replace.replaceAll("[.,']", "");
}
for (c = 0; c < strings.size(); c++) {
boolean pal = PalindromeChecker.isPalindrome(strings.get(c));
if (true) {
System.out.print(strings.get(c));
}
}
}
public void letter() {
int b;
Scanner replaceLetter = new Scanner(System.in);
System.out.println("What letter would you like to remove?");
String badLetter = replaceLetter.next();
System.out.println("What letter would you like to add?");
String newLetter = replaceLetter.next();
for (b = 0; b < strings.size(); b++) {
String replaceVowels = strings.get(b);
replaceVowels = replaceVowels.replace("badVowel", "newVowel");
}
replaceLetter.close();
System.out.print(strings);
}
public void word() {
Scanner removeWord = new Scanner(System.in);
System.out.println("What word would you like to remove?");
String word = removeWord.next();
strings.remove(word);
removeWord.close();
System.out.print(strings);
}
}
答案 0 :(得分:2)
删除这些行..它会起作用......
replaceLetter.close ();
removeWord.close();
答案 1 :(得分:1)
扩展@TheLostMind的答案,问题是当你调用Scanner#close
时,如果它实现Closeable
接口,它也将关闭底层流。取自文档:
如果此扫描仪尚未关闭,则表明其底层 可执行也实现了Closeable接口,然后可读 将调用close方法。如果此扫描仪已经关闭,那么 调用此方法将无效。
因此,在您的letter()
和word()
函数中,当您调用close时,它也将关闭System.in 。因此,当您尝试使用Scanner
再次从System.in
读取时,没有什么可以阅读,而您获得了NoSuchElementException
。
答案 2 :(得分:0)
使用
while(scanner.hasNextLine()){}
和scanner.nextLine
代替hasNext()
&amp; next()
。
如果您使用java7
,那么最好像这样编码:
try (Scanner scanner = new Scanner(new FileInputStream(file)) {
}
它会自动关闭Scanner
并且对任何类型的leak