1

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();
                }
            }
3
  • read java spec - section 6.4 as pointed here Commented Mar 2, 2015 at 1:35
  • Declare the variable within the context that it needs to be accessed... Commented Mar 2, 2015 at 1:41
  • You already have sCurrentLine declared outside your while loop. What is the behavior you are trying to achieve? As currently written you only test if sCurrentLine contains pwd when it must be null. Do you still want it to print out every line of the file? Do you simply want to remember that pwd was found after the looping is over? Commented Mar 2, 2015 at 1:43

3 Answers 3

1

Put your if statement inside your for loop, but use a break:

while...
    if(sCurrentLine.contains(pwd)){
        System.out.println("password accepted");
        break;
    }

This breaks out of the for loop, so that once the password is found, it stops looping. You can't really move that if-check outside of the loop, because you want to check every line for the password until it is found, right?

If you do that, you don't need to move the sCurrentLine variable out of the loop. You also might want to doublecheck if you want to do sCurrentLine.equals(pwd) instead of using contains.

Sign up to request clarification or add additional context in comments.

Comments

1

You already have sCurrentLine declared outside your while loop. The problem is that you keep using it again and again for the next line. If you still want it to print the file and what you are trying to do is remember that the password was found or what line it was found in try this:

    BufferedReader br = null;
    boolean pwdFound = false;
    String pwdLine = "";


        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");
                    pwdFound = true;
                    pwdLine = sCurrentLine;
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

Comments

1

boolean flag = false; while ((sCurrentLine = br.readLine()) != null) {

   if(sCurrentLine.contains(pwd))
   {
      flag = true;
      break;
   }

} if(flag){System.out.println("password accepted");}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.