Java 如何检查文件是否包含用户输入的特定数字?(爪哇)
您好,我正在尝试编写一个程序,该程序将检查用户输入的数字的指定文件,但是,如果该数字不在文件中,我希望该程序循环回到开始,我如何才能做到这一点 以下是我到目前为止所造成的混乱:Java 如何检查文件是否包含用户输入的特定数字?(爪哇),java,java.util.scanner,Java,Java.util.scanner,您好,我正在尝试编写一个程序,该程序将检查用户输入的数字的指定文件,但是,如果该数字不在文件中,我希望该程序循环回到开始,我如何才能做到这一点 以下是我到目前为止所造成的混乱: import java.util.Scanner; public class Classtest2 { public static void main(String[] args) { // //1. class Scanner sc = new Scanner(Classtest2.c
import java.util.Scanner;
public class Classtest2 {
public static void main(String[] args) {
// //1. class
Scanner sc = new Scanner(Classtest2.class.getResourceAsStream("trombones.txt"));
Scanner myScanner = new Scanner(System.in);
System.out.println("What number would you like to check for?");
String number = myScanner.nextLine() ;
String keyWord = number, word;
int lineNum = 0;
while(sc.hasNextLine()) {
word = sc.next();
if(word.equals(myScanner.nextLine().trim())) {
System.out.println("The number "+number+ " is there");
break;
} else {
// not found
}
} // main
} // class
您需要做的就是从用户处获取输入,然后检查它是否存在于文件中。您不应该在while循环中使用
myScanner.nextLine().trim()
,因为它会在每次循环时等待用户输入。您从用户处获得数字的,您应该在文件中检查它。您所做的是获取用户输入,然后再次等待用户一次又一次地输入值
固定代码
import java.util.Scanner;
public class Classtest2 {
public static void main(String[] args) {
// //1. class
Scanner sc = new Scanner(Classtest2.class.getResourceAsStream("trombones.txt"));
Scanner myScanner = new Scanner(System.in);
System.out.println("What number would you like to check for?");
String number = myScanner.nextLine() ;
int lineNum = 0;
while(sc.hasNextLine()) {
word = sc.next();
if(word.equals(number.trim())) { //here was the problem
System.out.println("The number "+number+ " is there");
return;
} else {
}
}
System.out.println("not found"); // not found
}
最佳方法
import java.util.Scanner;
public class Classtest2 {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.println("What number would you like to check for?");
String number = myScanner.nextLine();
if(isFound(number)){
System.out.println("The number "+number+ " is there");
}else{
System.out.println("The number "+number+ " doesn't exist");
}
}
public static boolean isFound(String number) {
Scanner sc = new Scanner(Classtest2.class.getResourceAsStream("trombones.txt"));
String word="";
while (sc.hasNextLine()) {
word = sc.next();
if (word.equals(number.trim())) {
return true;
}
}
return false;
}
}
这对我有用,希望对你有用:
package test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class Classtest2 {
public static void main(String[] args) throws IOException {
File f = new File("trombones.txt");
f.createNewFile();
Scanner myScanner = new Scanner(System.in);
boolean numExiste = menu(new Scanner(f), myScanner);
while (!(numExiste)){
numExiste = menu(new Scanner(f), myScanner);
}
myScanner.close();
}
private static boolean menu(Scanner fileScanner, Scanner myScanner) throws FileNotFoundException{
String word = "";
System.out.println("What number would you like to check for?");
String number = myScanner.nextLine().trim();
while(fileScanner.hasNextLine()){
word = fileScanner.nextLine();
if(word.trim().equals(number)){
System.out.println("The number "+number+ " is there");
return true;
}else{
//Not the number
}
}
System.out.println("The number "+ number+ " is not in the file\n\n");
return false;
}
} // main } // class
添加另一个
while
围绕问题/搜索结构循环,并仅在发现某个内容时退出该结构。if(word.equals(myScanner.nextLine().trim())
是否有理由要求用户对给定文件中的每个单词进行更多输入?我已经尝试了这么久,甚至无法告诉您我为什么要做某些事情。。。任何例子都会有帮助,非常感谢汉克斯!然而,我在else语句中添加了数字,告诉用户该数字不在文件中,但当它读取每一行时,它会多次表示该数字不存在(文件中的行数)-有没有办法解决这个问题?我只是使用了break;并且解决了这个问题,非常感谢大家的帮助@不不不!!你不应该!!如果你在其他内容中加上一个分隔符。它只会显示用户输入是否存在于第一行中。它不关心文件的第2行中是否存在用户输入编号。检查我的更新答案。使用return代替breakah我明白了,我意识到在再次运行程序后不久哈哈,感谢“最佳方法”的帮助那还没有关闭他的资源?嗯,好的。