Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Why do I get this error?

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Why do I get this error?

    Hello,

    I posted this program last night and got help with the sorting of the arrays, but I still get an error whenever I try and run it. There is something wrong with the input portion of my code in the main and I'm not sure what it is.

    The program is to prompt the user for the number of students, the student names, and their scores. It should then print them out in decreasing order of their scores.

    Output should be:

    Enter number of students: 3
    Smith 70
    Jones 30
    Peterson 100

    The print out is
    Jones 30
    Smith 70
    Peterson 100


    However, my output / print out is coming out as:

    Enter number of Students: 3 Smith 70 Jones 30 Mary 100
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at Exercise06_19.main(Exercise06_19.java:18)
    Java Result: 1

     
    import java.util.Scanner;
     
    public class Exercise06_19 {
     
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
     
            System.out.print("Enter number of Students: ");
            int numberOfStudents = input.nextInt();
     
            String[] name = new String[numberOfStudents];
            for (int i = 0; i < name.length; i++) {
                name[i] = input.next();
            }
            int[] score = new int[numberOfStudents];
            for (int i = 0; i < score.length; i++) {
                score[i] = input.nextInt();
            }
            decreasingOrder(name, score);
        }
     
        public static void decreasingOrder(String[] name, int[] score) {
     
            for (int i = 0; i < score.length; i++) {
                if (score[i] < score[i + 1]) {
                    int swap = score[i];
                    String swap2 = name[i];
                    score[i] = score[i + 1];
                    score[i + 1] = swap;
                    name[i] = name[i + 1];
                    name[i + 1] = swap2;
                }
                System.out.println(name[i] + score[i]);
            }
        }
    }

    Any kind of help/guidance would be great Thanks!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why do I get this error?

    InputMismatchException
    The nextInt() method called on line 18 is reading a non-numeric String. It wants all numeric digits it can convert to an int.
    What did you type in?
    What order of input does your program expect? numeric vs non-numeric
    What did you give it?

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Why do I get this error?

    Hey thanks for replying

    I typed in : 3 Smith 70 Jones 30 Mary 100

    The order entered should be numberOfStudents name[0] score[0] name[1] score[1] name[2] score[2]

    I'm giving it an integer, string, integer, string, integer, string, integer.

    Am I not able to do this? I was trying to make it so the String for loop grabs just the names and the Integer for loop grabs the scores, but keep them together, so name[0] = Smith and score[0] = 70

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Why do I get this error?

    If you want to read all names and then all scores then your input will have to be in the same order as you attempt to read it. Surely that makes sense. How is the computer supposed to read your mind and skip the score entries and only read the name entries?
    Improving the world one idiot at a time!

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Why do I get this error?

    Quote Originally Posted by Bryan29 View Post
    Hey thanks for replying

    I typed in : 3 Smith 70 Jones 30 Mary 100

    The order entered should be numberOfStudents name[0] score[0] name[1] score[1] name[2] score[2]

    I'm giving it an integer, string, integer, string, integer, string, integer.

    Am I not able to do this? I was trying to make it so the String for loop grabs just the names and the Integer for loop grabs the scores, but keep them together, so name[0] = Smith and score[0] = 70
    Enter Name: ABC (name[0])
    Enter Name: DEF (name[1])
    ...
    ...
    ...
    Enter Marks: 12 (score[0])
    Enter Marks: 75 (score[1])
    ...
    ...
    ABC=12
    DEF=75

    What else do you want it to do?