I have a variable that is set in a while loop because it is reading from a file. I need to access and use the code from the outside of the loop, because I'm using the variable in an if statement and the if statement can't be in the while loop or else it will be repeated multiple times. Here is my code.
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("C:\\Users\\Brandon\\Desktop\\" + Uname + ".txt"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}if(sCurrentLine.contains(pwd)){System.out.println("password accepted");}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
sCurrentLinedeclared outside your while loop. What is the behavior you are trying to achieve? As currently written you only test ifsCurrentLinecontainspwdwhen it must benull. Do you still want it to print out every line of the file? Do you simply want to remember thatpwdwas found after the looping is over?