简体   繁体   中英

How to use variable in a while loop outside of the loop in java?

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();
                }
            }

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 .

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();
            }
        }

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM