-2

Program to find the maximum average out of given records. Input mismatch exception. string value became blank while iterating over for loop value i=1

import java.util.ArrayList;
import java.util.Scanner;

public class MaxAvg {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        ArrayList<Records> rec = new ArrayList<Records>();
        int maxAvg = 0, maxIndex = 0;
        String s ="";

        for(int i=0;i<3;i++)
        {
            s = sc.nextLine();
            int m1 = sc.nextInt();
            int m2 = sc.nextInt();
            int m3 = sc.nextInt();
            rec.add(new Records(s,m1,m2,m3));
        }

        for(int i=0;i<3;i++)
        {
            int avg = (rec.get(i).m1 + rec.get(i).m2 + rec.get(i).m3 )/3;
            if(avg > maxAvg)
            {
                maxAvg = avg;
                maxIndex = i;
            }
        }

        System.out.println(rec.get(maxIndex).name  + "   " + maxAvg);

    }

}



class Records
{
    String name;
    int m1,m2,m3;
    public Records(String name, int m1, int m2, int m3) {
        super();
        this.name = name;
        this.m1 = m1;
        this.m2 = m2;
        this.m3 = m3;
    }
}

this is the exception i am encountering

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at MaxAvg.main(MaxAvg.java:19)
Madhawa Priyashantha
  • 9,208
  • 7
  • 28
  • 58
yoga ranjan
  • 125
  • 1
  • 9
  • https://docs.oracle.com/javase/7/docs/api/java/util/InputMismatchException.html – Dan May 04 '16 at 12:54
  • print value of s and check the value read this http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods – Madhawa Priyashantha May 04 '16 at 12:55

1 Answers1

0

java.util.InputMismatchException :-

Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

Did you type in something that wasn't an int?

Lee
  • 728
  • 3
  • 13