我正在编写一个循环,当扫描器接收到字符串值“end”时将退出。但是,当使用“结束”值进行测试时,循环会继续。逻辑上如果 file = 输入,那么 if(file=="end") 为假,即使我输入了 end!我的代码中是否有明显的错误?
String file = "";
Scanner in = new Scanner(System.in);
ArrayList<Integer> fileInput = new ArrayList<Integer>();
while(file!="end") {
// Scan for filename/end program
System.out.println("Provide the name of a file in the \"bin/\" folder, i will assume it's .txt");
file = in.nextLine();
System.out.println("." + file + ".");
if(file!="end") {
file= "bin/" + file + ".txt";
// start reading
try {
// If file found then carry on
BufferedReader openFile = new BufferedReader(new FileReader(file));
fileInput = readIn(openFile);
int lowerBound = getLower(fileInput);
int upperBound = getUpper(fileInput);
System.out.println("Lower Bound: " + lowerBound);
System.out.println("Upper Bound: " + upperBound);
// file not found
} catch (FileNotFoundException e) {
System.out.println("File not found!");
}
}
}
System.out.println("Goodbye!");
System.exit(0);