Reputation: 338
I have a for
loop nested inside a do-while
loop which is to read through a text file and look for the given input. I have noticed that it works perfectly fine if you give an input which exists in the file, however can someone help me understand why the program never exits the loop if you give an input which does not exist in the text file, even though it should exit when the variable with the text from the file becomes null
. I have posted all relevant code from the method where the for loop is being executed, including the part where it gains the user input; just to be clear, there are no errors given when compiling
or running the program.
FileReader fileReader = new FileReader("VirtualATM.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
System.out.print("Enter your pin: ");
char [] EnterPin = {0, 0, 0 ,0};
try{
EnterPin = System.console().readPassword();
}catch(InputMismatchException e){
e.printStackTrace();
System.out.println(e.getMessage());
//System.out.println(e.getLocalizedMessage());
}
boolean pinTrue = false;
String LineFromFile = null;
LineFromFile = bufferedReader.readLine();
String [] PinSearch = LineFromFile.split("\\s+");
String UserPin = java.util.Arrays.toString(EnterPin);
String PinMod = UserPin.replaceAll("\\[", "");
String PinMod2 = PinMod.replaceAll("\\,", "");
String PinMod3 = PinMod2.replaceAll("\\s+", "");
String PinToWrite = PinMod3.replaceAll("\\]", "");
do{
for(int search = 0; search < PinSearch.length; search++){
String SearchForPin = PinSearch[search];
if(SearchForPin.matches(PinToWrite)){
pinTrue = true;
System.out.println("Success!");
}
else if(search => PinSearch.length){
System.out.println("Error! Invalid pin.");
}
}
LineFromFile = bufferedReader.readLine();
}while(pinTrue == false && line != null);
}catch(IOException e){
e.printStackTrace();
System.out.println(e.getLocalizedMessage());
}
Upvotes: 1
Views: 153
Reputation: 393986
You need to move the line processing to be inside the loop, after each line you read. Otherwise, you only process the first line you read before the loop.
do{
for(int search = 0; search < PinSearch.length; search++){
String SearchForPin = PinSearch[search];
if(SearchForPin.matches(PinToWrite)){
pinTrue = true;
System.out.println("Success!");
}
else if(search => PinSearch.length){
System.out.println("Error! Invalid pin.");
}
}
LineFromFile = bufferedReader.readLine();
if (LineFromFile != null) {
PinSearch = LineFromFile.split("\\s+");
UserPin = java.util.Arrays.toString(EnterPin);
PinMod = UserPin.replaceAll("\\[", "");
PinMod2 = PinMod.replaceAll("\\,", "");
PinMod3 = PinMod2.replaceAll("\\s+", "");
PinToWrite = PinMod3.replaceAll("\\]", "");
}
} while(pinTrue == false && LineFromFile != null);
Upvotes: 1
Reputation: 201507
Because you're testing line
, but reading into LineFromFile
.
Change this
}while(pinTrue == false && line != null);
to something like
}while(!pinTrue && LineFromFile != null);
And, as Eran notes here, your line processing logic should be in the body of the do
, so
do{
String LineFromFile = bufferedReader.readLine();
String [] PinSearch = LineFromFile.split("\\s+");
String UserPin = java.util.Arrays.toString(EnterPin);
String PinMod = UserPin.replaceAll("\\[", "");
String PinMod2 = PinMod.replaceAll("\\,", "");
String PinMod3 = PinMod2.replaceAll("\\s+", "");
String PinToWrite = PinMod3.replaceAll("\\]", "");
Upvotes: 1