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 8 of 8

Thread: INPUTMISMATCHEXCEPTION

  1. #1
    Junior Member james's Avatar
    Join Date
    Jan 2010
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question INPUTMISMATCHEXCEPTION

    Hey guys it's me again i dont know whta's wrong with my code.The error goes like this:

    H:\JAVAFILES>javac university.java

    H:\JAVAFILES>java university
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:819)
    at java.util.Scanner.next(Scanner.java:1431)
    at java.util.Scanner.nextDouble(Scanner.java:2335)
    at university.main(university.java:30)



    But here is my program:

    import java.util.*;
    import java.io.*;
     
    public class university 
    {
    	public static void main(String[] args)throws FileNotFoundException
     
    	{
     
            double GPA ;
            double sum = 0;
    		String M ;
            String F ; 
     
     
    		Scanner inFile = new Scanner(new FileReader("studentGPA.txt"));
    		PrintWriter outFile = new PrintWriter("studentGPAavg.out");
     
     
     
            M = inFile.next();
    		F = inFile.next();
     
     
    		outFile.println("Enter code:" + M + " " + F );
     
     
    		outFile.print("Students GPA is:"  );
     
    		GPA = inFile.nextDouble();
    		outFile.printf(" %5.2f", GPA);
    		sum = sum + GPA;
     
            GPA = inFile.nextDouble();
    		outFile.printf(" %5.2f", GPA);
    		sum = sum + GPA;
     
            GPA = inFile.nextDouble();
    		outFile.printf(" %5.2f", GPA);
    		sum = sum + GPA;
     
            GPA = inFile.nextDouble();
    		outFile.printf(" %5.2f", GPA);
    		sum = sum + GPA;
     
            GPA = inFile.nextDouble();
    		outFile.printf(" %5.2f", GPA);
    		sum = sum + GPA;
     
     
            GPA = inFile.nextDouble();
    		outFile.printf(" %5.2f", GPA);
    		sum = sum + GPA;
     
     
            GPA = inFile.nextDouble();
    		outFile.printf(" %5.2f", GPA);
    		sum = sum + GPA;
     
            GPA = inFile.nextDouble();
    		outFile.printf(" %5.2f", GPA);
    		sum = sum + GPA;
     
            GPA = inFile.nextDouble();
    		outFile.printf(" %5.2f", GPA);
    		sum = sum + GPA;
     
            GPA = inFile.nextDouble();
    		outFile.printf(" %5.2f", GPA);
    		sum = sum + GPA;
     
    		outFile.println();
     
    		outFile.printf("The average of the GPA is:   %5.2f %n ", sum/10.0);
     
            inFile.close();
    		outFile.close();
    	}
    }
    Last edited by Json; February 11th, 2010 at 03:25 AM. Reason: Please use code tags.


  2. #2
    Member
    Join Date
    Feb 2010
    Location
    Dehradun, India
    Posts
    37
    Thanks
    1
    Thanked 7 Times in 6 Posts

    Default Re: INPUTMISMATCHEXCEPTION

    Hi, Give me that studentGPA.txt file..then only I can check what happend

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: INPUTMISMATCHEXCEPTION

    The InputMismatchException is usually thrown because you are trying to use a method on the Scanner object which returns a specific type such as nextDouble() but when the next token isn't a double it throws the exception at you.

    You can always check what the next token is before trying to grab it using Scanner.hasNextDouble().

    See Scanner (Java Platform SE 6))

    // Json

  4. #4
    Junior Member james's Avatar
    Join Date
    Jan 2010
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: INPUTMISMATCHEXCEPTION

    hey, jassi here is the text file :

    M 3.50
    M 4.31
    F 3.46
    M 2.98
    F 4.10
    M 3.10
    F 4.23
    M 2.99
    F 3.65
    F 2.53

  5. #5
    Member
    Join Date
    Feb 2010
    Location
    Dehradun, India
    Posts
    37
    Thanks
    1
    Thanked 7 Times in 6 Posts

    Post Re: INPUTMISMATCHEXCEPTION

    Can you please elaborate what is your requirement??

    What must be the output you want from your program?

    Actually Json is correct as the value you are reading from the file is character and double so you are mismatching input by reading char value in double.
    Last edited by jassi; February 11th, 2010 at 11:57 PM. Reason: Need to elaborate more

  6. #6
    Junior Member james's Avatar
    Join Date
    Jan 2010
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: INPUTMISMATCHEXCEPTION

    Well jassi the requirement is this:
    You receve a file that contains female and male student's GPA for certain courses .Due to confidentility, only the letter code f is used to represent female students; m is used to repesent male students.Every file entry consists of a letter code followed by a GPA .Each line has one entry The number of entries that may be in a file is unknown. Write a program that computes and outputs the average GPA for both female and male students Format your results to two decimal places.

  7. #7
    Junior Member james's Avatar
    Join Date
    Jan 2010
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Red face Re: INPUTMISMATCHEXCEPTION

    Well I have made some changes to my program.But i still have this error:

    [
    import java.util.*;
    import java.io.*;

    public class university
    {
    public static void main(String[] args)throws FileNotFoundException

    {

    double GPA ;
    double sum = 0;
    String Mcode ;
    String Fcode ;


    Scanner inFile = new Scanner(new FileReader("studentGPA.txt"));
    PrintWriter outFile = new PrintWriter("studentGPAavg.out");


    Mcode = inFile.next();
    Fcode = inFile.next();



    outFile.println("Enter code:" + Mcode + " " + Fcode );


    outFile.print("Students GPA is:" );

    GPA = inFile.nextDouble();
    outFile.printf(" %5.2f", GPA);
    sum = sum + GPA;

    GPA = inFile.nextDouble();
    outFile.printf(" %5.2f", GPA);
    sum = sum + GPA;

    GPA = inFile.nextDouble();
    outFile.printf(" %5.2f", GPA);
    sum = sum + GPA;

    GPA = inFile.nextDouble();
    outFile.printf(" %5.2f", GPA);
    sum = sum + GPA;

    GPA = inFile.nextDouble();
    outFile.printf(" %5.2f", GPA);
    sum = sum + GPA;


    GPA = inFile.nextDouble();
    outFile.printf(" %5.2f", GPA);
    sum = sum + GPA;


    GPA = inFile.nextDouble();
    outFile.printf(" %5.2f", GPA);
    sum = sum + GPA;

    GPA = inFile.nextDouble();
    outFile.printf(" %5.2f", GPA);
    sum = sum + GPA;

    GPA = inFile.nextDouble();
    outFile.printf(" %5.2f", GPA);
    sum = sum + GPA;

    GPA = inFile.nextDouble();
    outFile.printf(" %5.2f", GPA);
    sum = sum + GPA;

    outFile.println();

    outFile.printf("The average of the GPA is: %5.2f %n ", sum/10.0);

    inFile.close();
    outFile.close();
    }
    }
    /]

    here is the error:

    C:\TEMP>java university
    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:817)
    at java.util.Scanner.next(Scanner.java:1431)
    at java.util.Scanner.nextDouble(Scanner.java:2335)
    at university.main(university.java:64)

  8. #8
    Member
    Join Date
    Feb 2010
    Location
    Dehradun, India
    Posts
    37
    Thanks
    1
    Thanked 7 Times in 6 Posts

    Post Re: INPUTMISMATCHEXCEPTION

    Resolved Program without errors.After running your program

    You can check studentGPavg.out

    import java.util.*;
    import java.io.*;

    public class university
    {
    public static void main(String[] args)throws FileNotFoundException

    {

    double GPA ;
    double sum = 0;
    String gender ;
    float counter=0;


    Scanner inFile = new Scanner(new FileReader("studentGPA.txt"));
    PrintWriter outFile = new PrintWriter("studentGPAavg.out");

    outFile.println("Code: M F");
    outFile.println("Students GPA is:" );

    while(inFile.hasNext()){

    gender=inFile.next();
    if(gender.equalsIgnoreCase("M")){
    GPA = inFile.nextDouble();
    outFile.printf(" %5.2f", GPA);
    sum = sum + GPA;
    counter++;
    }else if(gender.equalsIgnoreCase("F")){
    GPA = inFile.nextDouble();
    outFile.printf(" %5.2f", GPA);
    sum = sum + GPA;
    counter++;
    }

    }

    outFile.println();
    outFile.printf("The average of the GPA is: %5.2f %n ", sum/counter);
    inFile.close();
    outFile.close();
    }
    }

    Click on Thanks link. and mark your thread as resolved.

  9. The Following User Says Thank You to jassi For This Useful Post:

    james (February 15th, 2010)