Chr
Chr

Reputation: 29

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

Upvotes: 0

Views: 114

Answers (3)

Alan Caetano
Alan Caetano

Reputation: 11

I think that it is returning to the third loop as the "continue" is meant to do, but the condition isInteger(koord[0]) will be false for sure, because if koord[0].equals("..NØH ") is true then koord[0] is not numeric, right?

Upvotes: 1

Thomas Kläger
Thomas Kläger

Reputation: 21435

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.

Upvotes: 0

Sundeep
Sundeep

Reputation: 472

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)

Upvotes: 0

Related Questions