gpt4 book ai didi

java - 错误 : java. util.NoSuchElementException - 扫描仪未按预期运行

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:30:36 27 4
gpt4 key购买 nike

扫描器返回 NoSuch 元素异常错误。您能解释一下为什么会这样吗。

Scanner 现在通过并运行良好,但它没有从第二次 Scanner 调用中获取 nextLine 输入。这可能是一个小调整,但有人可以指出错误是什么。

public class JavaHW1_1 {

private static Scanner userInput = new Scanner(System.in);


public static void main(String[] args) throws IOException {

String pattern ;
String fileName = null;



// Method to manage user inputs
fileName = userInputFileName(userInput);
pattern = userInputPattern(userInput);

// To find the pattern in the file
// findPattern();

}


private static String userInputPattern(Scanner userInput) {
String pattern = "JollyGood";
System.out.println(". Please enter a pattern to find in the file");

while(userInput.hasNextLine()){
pattern = userInput.nextLine();
System.out.println("The pattern to be searched: "+ pattern);
}
userInput.close();

return pattern;
}


private static String userInputFileName(Scanner userInput) throws IOException {
String path = "./src";
String files, fileName;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();

System.out.println("Please input the desired file name:\n");
System.out.println("Some suggestions:\n");
for (int i = 0; i < listOfFiles.length; i++)
{

if (listOfFiles[i].isFile() && listOfFiles[i].getName().toLowerCase().endsWith(".txt"))
{

files = listOfFiles[i].getName();
System.out.println(files);
}
}

int userAttempt = 0;

do{
fileName = userInput.nextLine();

if(fileName.toLowerCase().endsWith(".txt")){
System.out.println("The file name entered is in correct format");
File file = new File("./src",fileName);

try {
file.createNewFile();
System.out.println("File is created. Please enter text to be written in the file. End the content with \"eof\"");
InputOutput(file.getName());
} catch (IOException e) {
e.printStackTrace();
}

userAttempt = 10;
}
else
{System.out.println("Please enter correct format file with .txt extension");
userAttempt++;}
}while (userAttempt <10);

return fileName;
}




private static void InputOutput(String fName) throws IOException {

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter("./src/" + fName));
String inputLine = null;
do {
inputLine=in.readLine();
out.write(inputLine);
out.newLine();
} while (!inputLine.equalsIgnoreCase("aaa"));
System.out.print("Write Successful");
} catch(IOException e1) {
System.out.println("Error during reading/writing");
} finally {
out.close();
in.close();
}

}


private static void findPattern() {
// TODO Auto-generated method stub

}


}

最佳答案

基于 this因此,您可能会关闭 Scanner 并创建一个新的扫描器以从 System.in 中读取,通过查看您的代码,这很有意义。

所以我对您的代码的建议是通过参数接收扫描仪,如下所示:

public static void main(String[] args){

Scanner scan = new Scanner (System.in);
String pattern = userInputPattern(scan);
String test = readSomethingElse(scan);
}

private static String readSomethingElse(Scanner scan) {
System.out.println(". Read something else");
return scan.nextLine();
}

private static String userInputPattern(Scanner scan) {

String pattern = "JollyGood";
System.out.println(". Please enter a pattern to find in the file");
pattern = scan.nextLine();
System.out.println("The pattern to be searched: "+ pattern);
return pattern;
}

关于java - 错误 : java. util.NoSuchElementException - 扫描仪未按预期运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17036935/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com