0

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!

4
  • 5
    move a++outside of the if. You never stop looping Commented Apr 13, 2018 at 11:47
  • 2
    Learn to use the debugger, makes it easy to find these kinds of bugs. Commented Apr 13, 2018 at 11:48
  • I'd also suggest using for instead of while, it's more idiomatic. Commented Apr 13, 2018 at 11:49
  • I will heed your advice. Not used it very often before. Commented Apr 13, 2018 at 11:56

4 Answers 4

3

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

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

Comments

2

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.

1 Comment

Wow.. I have made a phenomenal fool of myself. Uh, thanks, I guess ^^;
1

@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.)

1 Comment

The tenses make perfect sense (I understood what you want to say at least) and yes, that, along with the using the debugger more often advice, will be what I take from this small oversight. Thanks.
1

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

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.