Can't see me
Can't see me

Reputation: 501

While Loop and difficulties

I am a beginner in Java and had a question regarding an assignment I was doing.

I am trying to read a sequence of integer inputs and print out the largest and the smallest number. Though I already wrote the code, but the problem is that when I run it, it doesn't print the largest nor the smallest number. The code seems right even though its not! Any help would be appreciated.

import java.util.Scanner;

public class Practice {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter integers: ");

        int largest = in.nextInt();
        int smallest = largest;

        while (in.hasNextInt()) {
            int input = in.nextInt();

            if (input > largest) {
                largest = input;
            } else if (input < smallest) {
                smallest = input;
            }
            System.out.println();

        }

        System.out.println(largest);
        System.out.println(smallest);
    }
}

Upvotes: 0

Views: 101

Answers (3)

blackbird014
blackbird014

Reputation: 2069

Reading the code I would say that you expect the user to enter a set of numbers separated by space (the default separator chosen by the Scanner) The scanner will loop endless parsing the input. Now you need to decide a condition to exit and make your code safer.
When I say make your vode safer I mean put a try catch around your code in case the user doesn't write a number. Moreover you should close the scanner in a finally. If you write something like the code below, the cycle is broken whenever u write a letter. E.g 1 2 4 6 A will print 2 and 6. Take it as a suggestion to work out a bit. Actually. you need to still protect the first nextInt, which, as it is, could throw exception if you don't start with a number and ideally decide an exit character handling the other exceptions. But these are implementation details. The code below will work provided that you start with a number and finish with a character different from a number

    import java.util.Scanner;

    public class Practise {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            System.out.print("Enter integers: ");

            int largest = in.nextInt();
            int smallest = largest;
            try {
                while (in.hasNextInt()||in.hasNext()) {
                    int input = in.nextInt();

                    if (input > largest) {
                        largest = input;
                    } else if (input < smallest) {
                        smallest = input;
                    }
                    System.out.println("Computing "+input);
                } 

                System.out.println(largest);
                System.out.println(smallest);
            } catch (Exception ex) {
                System.out.print("Exception caught: " + ex);
            } finally {
                in.close();
            }
        }
    }

Upvotes: 0

VishalDevgire
VishalDevgire

Reputation: 4268

Your code will not stop accepting numbers until it get something other than a number.

Like a alphabetical character. Enter an alphabetical character or keep something as a terminator.

Like enter -99 to quit or something.

Upvotes: 0

mustaphahawi
mustaphahawi

Reputation: 297

To Stop Waiting for an input : Enter A character as input : in.hasNextInt() return False

Upvotes: 1

Related Questions