2

I'm trying to find the smallest number from the inputs (5 inputs) of users. However, every time I click run and write down the numbers, the smallest number will always be "0". Here is my code:

import java.util.Scanner;

public class Q2_9 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int counter = 1;
        double number;
        double smallest = 0;

        Scanner s = new Scanner(System.in);

        while (counter < 6) {
            System.out.println("Enter the number: ");
            number = s.nextDouble();

            if (smallest > number) {
                smallest = number;
            }

            counter++;

        }

        System.out.println("The smallest number is " + smallest);

        s.close();
    }
}
khelwood
  • 46,621
  • 12
  • 59
  • 83
  • 2
    You test `if (smallest > number)` before updating `smallest`, but you initialize it at 0, so unless the user input a negative number `smallest` will always be 0. A solution would be to initialize it at `Integer.MAX_VALUE` instead of 0 so that the first input will always be considered lower than the value you initialized `smallest` at – Aaron Nov 06 '20 at 00:34
  • Thanks a bunch Aaron! So the way I see it was that I put the smallest number down as 0, so if the user's input was anything below 0, it would print that. I don't really understand why you have to put Integer.MAX_VALUE though. I've just started Java and it's pretty tough :/ –  Nov 06 '20 at 00:37
  • @ScottHouston: if the user's input was anything below 0, it *WOULD* print that! Q: What 5 numbers did you enter (such that the smallest was still "0")? PS: "for loops" are a useful idiom. EXAMPLE: `for (int counter = 0; counter < 5; counter++) { ... }` – paulsm4 Nov 06 '20 at 00:42
  • @paulsm4 Thanks so much! You guys have been a great help to me :') –  Nov 06 '20 at 00:46
  • @ArvindKumarAvinash Oh, thanks a bunch! I am pretty new to StackOverflow –  Nov 07 '20 at 03:02
  • @ScottHouston - You are most welcome. – Arvind Kumar Avinash Nov 07 '20 at 08:56

1 Answers1

1

Algorithm:

  1. Input the first number and assume it to be the smallest number.
  2. Input the rest of the numbers and in this process replace the smallest number if you the input is smaller than it.

Demo:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        final int LIMIT = 5;
        int counter = 1;
        double number;

        Scanner s = new Scanner(System.in);

        // Input the first number
        System.out.print("Enter the number: ");
        number = s.nextDouble();
        double smallest = number;

        // Input the rest of the numbers
        while (counter <= LIMIT - 1) {
            System.out.print("Enter the number: ");
            number = s.nextDouble();

            if (smallest > number) {
                smallest = number;
            }

            counter++;

        }

        System.out.println("The smallest number is " + smallest);
    }
}

A sample run:

Enter the number: -4040404
Enter the number: 808080
Enter the number: -8080808
Enter the number: 8989898
Enter the number: -8989898
The smallest number is -8989898.0

Note: Do not close a Scanner(System.in) as it also closes System.in and there is no way to open it again.

Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72
  • He is closing the scanner after the whole program , which means he doesn't wants the Scanner to take in other input, which is saving us from Resource leak – Swapnil Padaya Nov 06 '20 at 00:56
  • 1
    @SwapnilPadaya - In the case of `System.in`, there may be a negligible resource leak. However, the risk of closing `Scanner(System.in)` is that there is no way to open it again. So, if your application has just one program, it will not be a problem but if your application has other programs and if any of them need to take input from the keyboard, you will get an exception. Check [this discussion](https://stackoverflow.com/questions/14142853/close-a-scanner-linked-to-system-in). You can find many more such discussions on SO. – Arvind Kumar Avinash Nov 06 '20 at 01:00
  • Yeah for a single program there ain't any problem but for an application having other programs can cause an exception. – Swapnil Padaya Nov 06 '20 at 01:16