Results 1 to 9 of 9
  1. #1
    CruelCoin is offline Member
    Join Date
    Dec 2013
    Posts
    8
    Rep Power
    0

    Default Scanner not picking up doubles

    Hi All,

    Having some difficulties with this section.

    The program compiles with 0 errors but is giving me some errors on running.

    Not sure on how to fix these.
    Any guidance would be greatly appreciated.

    My code:

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class Q4
    {
    	public static void main(String[] args)
    	//Throw any exceptions
    	throws Exception
    	{
    		Scanner input = new Scanner(new File("Salary.txt"));
    
    		String assistant = "assistant";
    		String associate = "associate";
    		String full = "full";
    		
    		double assistantTotal = 0.0;
    		double associateTotal = 0.0;
    		double fullTotal = 0.0;
    		double allFacultyTotal = 0.0;
    		
    		double assistantAverage = 0.0;
    		double associateAverage = 0.0;
    		double fullAverage = 0.0;
    		double allFacultyAverage = 0.0;
    		
    		int assistantCounter = 0;
    		int associateCounter = 0;
    		int fullCounter = 0;
    		int allFacultyCounter = 0;
    		
    		while (input.hasNextLine())
    		{
    			String line = input.nextLine();
    			Scanner lineScan = new Scanner(line);
    			double wage = lineScan.nextDouble();
    			
    			if (line.toLowerCase().contains(assistant.toLowerCase()))
    			{
    				assistantCounter++;
    				assistantTotal += wage;
    				assistantAverage = assistantTotal / assistantCounter;
    				allFacultyTotal += wage;
    				allFacultyCounter++;
    			}
    			else if (line.toLowerCase().contains(associate.toLowerCase()))
    			{
    				associateCounter++;
    				associateTotal += wage;
    				associateAverage = associateTotal / associateCounter;
    				allFacultyTotal += wage;
    				allFacultyCounter++;
    			}
    			else if (line.toLowerCase().contains(full.toLowerCase()))
    			{
    				fullCounter++;
    				fullTotal += wage;
    				fullAverage = fullTotal / fullCounter;
    				allFacultyTotal += wage;
    				allFacultyCounter++;	
    			}
    			lineScan.close();
    		}	
    		allFacultyAverage = allFacultyTotal / allFacultyCounter;
    		
    		System.out.println("The average pay for Assistants is: " + assistantAverage);
    		System.out.println("The average pay for Associates is: " + associateAverage);
    		System.out.println("The average pay for Full Professors is: " + fullAverage);
    		System.out.println("The average pay all faculty members is: " + allFacultyAverage);
    		System.out.println("The total pay for all Assistants is: " + assistantTotal);
    		System.out.println("The total pay for all Associates is: " + associateTotal);
    		System.out.println("The total pay for all Full Professors is: " + fullTotal);
    		System.out.println("The total pay for all faculty members is: " + allFacultyTotal);
    
    		input.close();
    	}
    }
    I'm getting these errors:

    Java Code:
    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.nextDouble(Unknown Source)
    	at Q4.main(Q4.java:35)
    At line 35 is this: double wage = lineScan.nextDouble();

    Just for info, the file Salary.txt is comprised of many lines of the following format.

    FirstName1 LastName1 assistant 79174.73
    FirstName2 LastName2 associate 70817.75
    FirstName3 LastName3 associate 69619.0
    FirstName4 LastName4 full 116992.43
    Last edited by CruelCoin; 12-09-2013 at 11:38 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Scanner not picking up doubles

    What is the symbol for a decimal point in your locale? Is it a dot '.' or a comma ','?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    CruelCoin is offline Member
    Join Date
    Dec 2013
    Posts
    8
    Rep Power
    0

    Default Re: Scanner not picking up doubles

    Hi Jos,

    Thanks for the quick reply.

    In my locale decimal point is denoted with a dot " . "

    Thanks Roy.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,280
    Rep Power
    6

    Default Re: Scanner not picking up doubles

    But the point is that Java seems to disagree with you. Try inputting a number with a comma and see if that makes the problem go away.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Scanner not picking up doubles

    Your second (inner) Scanner is reading this:
    "FirstName1 LastName1 assistant 79174.73"
    You then do nextDouble() which reads in "FirstName1" (which is the first token in the Scanner) and attempts to parse it to a double.

    Boom.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    CruelCoin is offline Member
    Join Date
    Dec 2013
    Posts
    8
    Rep Power
    0

    Default Re: Scanner not picking up doubles

    Quote Originally Posted by gimbal2 View Post
    But the point is that Java seems to disagree with you. Try inputting a number with a comma and see if that makes the problem go away.
    Hi,

    I tried entering a comma, but it's giving me the same error.

    Regards,

    Roy.

  7. #7
    CruelCoin is offline Member
    Join Date
    Dec 2013
    Posts
    8
    Rep Power
    0

    Default Re: Scanner not picking up doubles

    Quote Originally Posted by Tolls View Post
    Your second (inner) Scanner is reading this:
    "FirstName1 LastName1 assistant 79174.73"
    You then do nextDouble() which reads in "FirstName1" (which is the first token in the Scanner) and attempts to parse it to a double.

    Boom.
    Hi Tolls.

    Any tips on how to correct this?

    CC.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,280
    Rep Power
    6

    Default Re: Scanner not picking up doubles

    Okay, so that was not the problem. So I tried out your code. Its failing on the line:

    Java Code:
                double wage = lineScan.nextDouble();
    Which is the first read action that the code does. But the lines of your file do not only contain a double, they start with text that is not numeric at all.You will need to investigate the Scanner class so you can tell it to skip everything that you don't want to read, or add code that reads over the bits you don't want.

    docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

    Or find another solution than Scanner of course; you might see your file as containing four columns, perhaps you could build something that splits the lines into those four columns so you can just focus on the fourth column only.

    EDIT: ninja'd!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Scanner not picking up doubles

    Also posted at: scanner issues
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Scanner reading in doubles
    By lyss13 in forum New To Java
    Replies: 11
    Last Post: 09-01-2013, 09:19 PM
  2. Picking out substrings
    By based_walt in forum New To Java
    Replies: 13
    Last Post: 08-21-2013, 05:23 PM
  3. help picking
    By gg4545 in forum New To Java
    Replies: 8
    Last Post: 04-05-2012, 08:57 AM
  4. Cant use doubles
    By Freakzoyd in forum New To Java
    Replies: 27
    Last Post: 07-24-2010, 05:48 AM
  5. Picking randomly
    By AbdulAziz Bader in forum New To Java
    Replies: 17
    Last Post: 06-02-2010, 01:39 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
  •