简体   繁体   中英

Java program does not terminate?

I've recently gotten a new 64-bit Windows Laptop that I need to set up. While trying to set up Java and a working IDE, I ran into the following issue:

I have a very simple test program that I try to run in Eclipse Oxygen 3.0 (also tried in IntelliJ, but the same problem occurred):

public class MinSuche {

    public static void main(String[] args) {
        int[] feld = {12, 18, 19, 22, 1, 37, 5};
        int min = feld[0];
        int a = 1;
        while(a<feld.length){
            if(feld[a]<min){
                System.out.println(min);
                min = feld[a];
                a++;
            }
        }
        System.out.println(min);
    }
}

The Compliler Compliance Level is set to 1.8 and I am using jdk1.8.0_161 (also tried Java 9, same issue; and my Eclipse apparently can't handle Java 10 yet). Now the problem:

It does not terminate on its own and the results of the System.out.println-commands do not show up in the console. I get no errors and have failed to find any other hints as to what could be wrong, but I get the feeling that this doesn't have anything to do with the code. The process just keeps running and running for minutes (maybe even hours if I let it) until I terminate it manually, nothing shows up on the console the entire time of running.

Has this happened to anyone before? What could be the problem? Or am I missing something in the code? Please help, I need a working Java IDE on my PC!

It's quite simple: a++ is never executed, since feld[a] (18) is not less than min (12). You need to move this statement out of the if-statement

Let's watch what is happening step by step:

Iteration 1:

a=1
min=12
feld[a]=18

Iteration 2:

a=1
min=12
feld[a]=18

Iteration 3,4,5....:

a=1
min=12
feld[a]=18

You need to increment a outside of the if block else it will never go through the whole array.

    while(a<feld.length){
        if(feld[a]<min){
            System.out.println(min);
            min = feld[a];
        }
        a++;
    }

This could have been easily spotted by using a debugger.

@jhamon and @JeroenSteenbeeke already pointed out that your need to move a++ out of the if block.

I want to add that you better use for cycle instead of while here:

public static void main(String[] args) {
        int[] feld = {12, 18, 19, 22, 1, 37, 5};
        int min = feld[0];
        int a = 1;
        for (int a = 1; a < feld.length; a++) {
            if(feld[a]<min){
                System.out.println(min);
                min = feld[a];
            }
        }
        System.out.println(min);
    }

This is more idiomatic, which may seem unimportant. But my point is that if you would have used the for cycle, you most probably would not have made the mistake you've made. (Oh my English, I hope tenses past conditional tenses make some sense here.)

Simply remove the a++ from the if block and place it after the if block as follows. Then your program will work fine as you expected. And there is nothing wrong with your JDK or with your IDE. Thanks!

while(a<feld.length){
        if(feld[a]<min){
            System.out.println(min);
            min = feld[a];
        }
        a++;
    }

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