0

I have a task:

5-8) (Find the highest score) Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the name of the student with the highest score.

When I run this code with entering a name like "john", it works well but when I'm trying to enter a name like "John Doe" it throws an InputMismatchException. I tried using nextLine() but the program moves to the next item without waiting user to enter a data. Finally, still throws the InputMismatchException error. Screenshot: ibb.co/ZT2ZhMz

Solution: I created a new Scanner object Scanner inp = new Scanner(System.in).useDelimiter(" "); and got the name input with this.

package pp;

import java.util.Scanner;

public class BestStudent {

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

        System.out.print("Enter the number of students: ");
        int nos = input.nextInt();

        //Creating arrays to store the inputs
        String []names = new String[nos];
        double []grades = new double[nos];

        //A loop for getting inputs until it reaches number of students(nos) value
        for (int i = 0; i<nos; i++) {
            System.out.println("Student "+(i+1)+" of "+nos);
            System.out.println("Enter student's name: ");
            names[i] = input.next();

            System.out.println("Enter student's score: ");
            grades[i] = input.nextDouble();
        }
        System.out.println("The highest score was:"+highest(grades)+" and "+name(names, grades)+" got it.");


        input.close();
    }
    //A method for finding the highest value
    public static double highest (double []grades) {
        double highest = 0;
        for (int k = 0; k<grades.length; k++) {

            if (grades[k]>highest)
                highest = grades[k];
        }
        return highest;
    }

    //A method for the order of the name that has the highest value.
    public static String name(String []names, double []grades) {
        double highest = 0;
        int order = 0;
        for (int k = 0; k<grades.length; k++) {

            if (grades[k]>highest) {
                highest = grades[k];
                order = k;
            }

        }
        return names[order];

    }

}

enter image description here

qwitz
  • 3
  • 3
  • 1
    Does this answer your question? [What's the difference between next() and nextLine() methods from Scanner class?](https://stackoverflow.com/questions/22458575/whats-the-difference-between-next-and-nextline-methods-from-scanner-class) – Guy Dec 22 '19 at 12:21
  • No, when I use nextLine(), the program moves to the next item without waiting user to enter a data. Finally, still throws the InputMismatchException error. Screenshot: https://ibb.co/ZT2ZhMz – qwitz Dec 22 '19 at 13:22

1 Answers1

0

This happens because the default delimiter of a token is a white space. You just have to set some configuration when init the Scanner object:

Scanner input = new Scanner(System.in).useDelimiter("\\n");
Renato
  • 1,725
  • 8
  • 18
  • I created another Scanner object as you have stated, it worked well, thanks: Scanner input = new Scanner(System.in).useDelimiter(" ") – qwitz Dec 22 '19 at 15:33