简体   繁体   中英

why does if statement send continue to wrong while loop

My program keeps jumping back to my second nested while-loop, what i want to do is continue my Third nested while loop. I have tried continue by itself, continue with label "her" as shown below and have also tried without continue statement. Every time the program jumps to the second while loop.

while((line = x.readLine()) != null){
            tekst = line.split("\\s+");
            if(tekst[0].equals(".PUNKT")||tekst[0].equals(".KURVE")
                    ||tekst[0].equals(".FLATE")){
                atrib = new String[100];
                a = 0;
                while(line != null){
                    if(line.equals("..NØH ")){
                        int []N = new int[10000];
                        int []Ø = new int[10000];
                        int []H = new int[10000];
                        line = x.readLine();
                        koord = line.split("\\s+");
                        i = 0;
                        her:while(isInteger(koord[0])){
                            System.out.println(koord[2]);
                            N[i] = Integer.parseInt(koord[0]);
                            Ø[i] = Integer.parseInt(koord[1]);
                            H[i] = Integer.parseInt(koord[2]);
                            i++;
                            line = x.readLine();
                            koord = line.split("\\s+");
                            if (koord[0].equals("..NØH ")){
                                line = x.readLine();
                                koord = line.split("\\s+");
                                continue her;
                            }
                            if(koord[0].equals(".PUNKT")||koord[0].equals(".KURVE")
                                    ||koord[0].equals(".FLATE")){
                                midl = line;
                                break;
                            }
                        }

我认为它正按“继续”的方式返回到第三循环,但条件isInteger(koord[0])肯定为假,因为if koord[0].equals("..NØH ")是真的,那么koord[0]不是数字,对吗?

the if statment excuted only when

if(tekst[0].equals(".PUNKT")||tekst[0].equals(".KURVE")
                ||tekst[0].equals(".FLATE"))

then how can you use statement

line.equals("..NØH ")

I think this program will stuck on the while loop

while(line != null)

Look at these three lines:

line = x.readLine();
koord = line.split("\\s+");
if (koord[0].equals("..NØH ")){

The condition koord[0].equals("..NØH ") will never be true.

  • if line contains "..NØH ", then koord[0] will be "..NØH" (without a trailing space!)
  • if line contains something else then koord[0] will be something different

The next if condition in the innermost while loop is also not fulfilled, so the break statement is skipped.

The next test is the condition of the innermost while loop:

while(isInteger(koord[0]))

Since line was "..NØH ", koord[0] is not an integer, so this loop is terminated.

And that brings you back to the beginning of the middle while loop.

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