Results 1 to 7 of 7
  1. #1
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default Cannot figure out error messages

    Java Code:
    Gpa.java:31: incompatible types
    found   : boolean
    required: double
                gpa = scan.hasNextDouble();
                                        ^
    I don't exactly know why this is, or how to fix it. (I honestly get this error message any time I use scan.hasNext() ) When I change this to scan.nextDouble() I get this when I run my program:

    Java Code:
    Exception in thread "main" java.util.InputMismatchException
            at java.util.Scanner.throwFor(Scanner.java:857)
            at java.util.Scanner.next(Scanner.java:1478)
            at java.util.Scanner.nextDouble(Scanner.java:2404)
            at Gpa.main(Gpa.java:31)
    And it's at the same (Gpa.java:31) line as where I got the first error. What do I need to do?

    Java Code:
    public class Gpa
    {
       public static void main(String[] args)
       {
          Scanner scan = new Scanner(System.in);
          DecimalFormat fmt = new DecimalFormat("0.00");
          boolean groupName;
          final String SENT = "EndOfGroup";
          String stuName;
          int groupCount, numGroup;
          double groupMin, groupMax, gpaAverage, gpa, groupSum;
          while(scan.hasNext())
          {
             groupName = scan.hasNextLine();
             numGroup = 0;
             groupSum = 0;
             groupCount = 0;
             groupMin = 0.0;
             groupMax = 4.0;
             gpaAverage = 0;
    
             stuName = scan.next();
             while(!stuName.equals(SENT))
             {
                numGroup++;
                gpa = scan.nextDouble();
                groupSum = groupSum + gpa;
                groupCount++;
                if(gpa >= groupMin)
                {
                   groupMin = gpa;
                }
                if(gpa <= groupMax)
                {
                   groupMax = gpa;
                }
    
                gpaAverage = groupSum/numGroup;
                System.out.print("Student: "+stuName);
                System.out.print("                 GPA = "+fmt.format(gpa));
                stuName = scan.next();
    
             }
    
             System.out.println("Average GPA for this group = "+fmt.format(gpaAverage));
             System.out.println("Minimum GPA for this group = "+fmt.format(groupMin));
             System.out.println("Maxium GPA for this group = "+fmt.format(groupMax));
    
    
    
    
       }
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Scanner#hasNextXXX() only checks to see if the next token exists and is of type XXX. It doesn't retrieve the token itself which is the job of the nextXXX() method (note that XXX can be Double, Boolean, Int, etc...).

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Here you declare variable gpa as a double:
    Java Code:
    double groupMin, groupMax, gpaAverage, gpa, groupSum;

    And here you attempt to assign a boolean value to it (see this for details):
    Java Code:
    gpa = scan.hasNextDouble();

    The reason you are getting an error with gpa = scan.nextDouble(); is that the input received is not a float data. For example, if the scanner's next token was "ABC", it would throw this error. What data are you feeding to it?

    Edit: Beaten. :(

  4. #4
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    The input Im trying to feed into that variable are all numbers in the form of X.XXX (3.756, 2.394, etc).

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Teclis View Post
    The input Im trying to feed into that variable are all numbers in the form of X.XXX (3.756, 2.394, etc).
    Fair enough, but do you understand what we are trying to tell you? If not, please let us know just what is unclear.

  6. #6
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    I think I understand what your saying. It's just that I don't understand how to correct it so that I can read 3.765 into double gpa.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Again you use scan.hasNextDouble() to see if a double is available and you use scan.nextDouble() to actually get the double value from the user. Please try to incorporate this into your code and see what happens. If it still doesn't work, please post your latest attempt, any error messages and let's see what we can fix.

Similar Threads

  1. Can someone figure out this error for me
    By Dev23 in forum New To Java
    Replies: 2
    Last Post: 02-24-2011, 04:31 AM
  2. [SOLVED] error messages
    By raghu9198 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-10-2008, 06:21 AM
  3. displaying error messages differently
    By Jothi in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 05-20-2008, 01:22 PM
  4. Error Messages Not Showing Up
    By nvidia in forum Web Frameworks
    Replies: 0
    Last Post: 04-07-2008, 10:41 PM
  5. Jsf Error Messages - Retain
    By vaswin in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 08-05-2007, 08:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •