Reading multiple inputs from Scanner

utnicho :

(I'm a beginner at Java) I am trying to write a program that asks for 6 digits from the user using a scanner, then from those 6 individual digits find the second highest number. So far this works however I'd like to have the input read from a single line rather than 6 separate lines. I've heard of delimiters and tokenisation, but have no idea how to implement it. I'ld like to have it read like "Enter 6 digits: 1 2 3 4 5 6" and parsing each digit as a separate variable so i can then place them in an array as shown. If anyone could give me a run-down of how this would work it would be much appreciated. Cheers for your time.

    public static void main(String[] args)
    {
        //Ask user input 
        System.out.println("Enter 6 digits: ");            
        //New Scanner
        Scanner input = new Scanner(System.in);

        //Assign 6 variables for each digit
        int num1 = input.nextInt();            
        int num2 = input.nextInt();            
        int num3 = input.nextInt();            
        int num4 = input.nextInt();            
        int num5 = input.nextInt();            
        int num6 = input.nextInt();            

      //unsorted array
      int num[] = {num1, num2, num3, num4, num5, num6};     
      //Length 
      int n = num.length;     
       //Sort 
      Arrays.sort(num);
       //After sorting
      // Second highest number is at n-2 position

       System.out.println("Second highest Number: "+num[n-2]);

 }           
}   
Samuel Silver Moos :

Your solution does this allready!

If you go through the documentation of scaner you will find out that your code works with different inputs, as long they are integers separated by whitespace and/or line seperators.

But you can optimice your code, to let it look nicer:

public static void main6(String[] args) {
    // Ask user input
    System.out.println("Enter 6 digits: ");
    // New Scanner
    Scanner input = new Scanner(System.in);

    // Assign 6 variables for each digit
    int size=6;
    int[] num=new int[size];
    for (int i=0;i<size;i++) {
        num[i]=input.nextInt();
    }
    Arrays.sort(num);
    // After sorting
    // Second highest number is at n-2 position

    System.out.println("Second highest Number: " + num[size - 2]);
}

As an additional hint, i like to mention this code still produces lot of overhead you can avoid this by using:

public static void main7(String[] args) {
    // Ask user input
    System.out.println("Enter 6 digits: ");
    // New Scanner
    Scanner input = new Scanner(System.in);

    // Assign 6 variables for each digit
    int size=6;
    int highest=Integer.MIN_VALUE;
    int secondhighest=Integer.MIN_VALUE;
    for (int i=0;i<size-1;i++) {
        int value=input.nextInt();
        if (value>highest) {
            secondhighest=highest;
            highest=value;
        } else if (value>secondhighest) {
            secondhighest=value;
        }
    }
    //give out second highest
    System.out.println("Second highest Number: " + secondhighest);
}

if you do not like to point on highest if there are multiple highest, you can replace the else if:

public static void main7(String[] args) {
    // Ask user input
    System.out.println("Enter 6 digits: ");
    // New Scanner
    Scanner input = new Scanner(System.in);

    // Assign 6 variables for each digit
    int size = 6;
    int highest = Integer.MIN_VALUE;
    int secondhighest = Integer.MIN_VALUE;
    for (int i = 0; i < size - 1; i++) {
        int value = input.nextInt();
        if (value > highest) {
            secondhighest = highest;
            highest = value;
        } else if (secondhighest==Integer.MIN_VALUE&&value!=highest) {
            secondhighest=value;
        }
    }
    // give out second highest
    System.out.println("Second highest Number: " + secondhighest);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=169271&siteId=1