Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Minimum value in java array

The program needs to find and print a minimum value from array. Where did i make mistake here? It prints 0 instead of real value:

  public static void main(String[] args) {     
         Scanner input = new Scanner(System.in);
         System.out.println("Enter numbers");

     int n = input.nextInt();
     double a[] = new double[n];
     double min= a[0];
     for(int i=0;i<n;i++){
         System.out.println("Enter numbers");
         a[i] = input.nextDouble();
         if(a[i]<min) min = a[i];
     }
     System.out.println("Minimal value is" +min);
}
         }

2 Answers

This seems to do the trick:

    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        System.out.printf("Enter your first number: ");
        String response = sc.nextLine();
        int currentLowest = Integer.parseInt(response);

        while(!response.equals("x")) {
            if(Integer.parseInt(response) < currentLowest){
                currentLowest = Integer.parseInt(response);
            }
            System.out.printf("Enter another number or 'x' to exit: ");
            response = sc.nextLine();
        }
        System.out.printf("The lowest number entered was %s", currentLowest);
    }

Here we set up the Scanner, output a prompt and read in a String, assigning that to response. That is then parsed to an integer and stored in the currentLowest variable - the first number eneterd must be the current lowest.

Then we enter a while loop that is looking for the escape character 'x' to stop the loop. The currentLowest is again compared with the existing input (nothing has changed here yet) and the code moves on to prompt for another number, reading that in as a String into response. The loop resets, testing whether that response is the escape character; stopping if it is. If not, the number entered is compared to the previous lowest and the lower of the two values is stored in currentLowest. This repeats until the user enters an 'x' character.

Once the 'x' is entered, the currentLowest value is output.

I hope that was what you were looking for.

Steve.

What are you looking to create here?

It looks like you want the user to enter some numbers. The program then states what the lowest value number is. Is that correct?

Steve.

Yes, the user needs to enter some numbers, like ( 1,5,6,7,8) And the output needs to be "Minimal value is 1" . Sorry if i was unclear. I can't find the mistake.

OK. Any specific number of inputs? Does the code need to demonstrate the use of a particular code structure like a for loop, for example?

We can create a solution to this pretty easily but need to know the requirements.

No specific number needed. It can go from 0 to 1 million. I believe it is done with the use of for loop, but i made the mistake somewhere. The main thing is the program which has to print the minimal value of entered numbers.

OK. So, we should create a while loop that continues accepting numbers until an escape character is entered.

As these numbers are being entered, the test can take place. There's no need to store the numbers; just the smallest one. No array, no for loop, just an input for the user. Check if the number just entered is less than the previous lowest. If it is store that, else carry in. Repeat until the escape character is entered.

Sounds about right?

That's it. Now it prints what is the lowest number. Thanks sir!

Cool. No problem, I will get on with that tomorrow; 10pm here now.

That shouldn't take me long to do for you. I'll post back asap.

Can't wait to see how did you solve it, and compare my code to yours. :)