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

Thread: scanner issues

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default scanner issues

    Hi All,

    Having some difficulties with this section.

    The program runs but is giving me some errors.

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

    My 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();
     
    			if (line.toLowerCase().contains(assistant.toLowerCase()))
    			{
    				assistantCounter++;
    				assistantTotal += input.nextDouble();
    				assistantAverage = assistantTotal / assistantCounter;
    				allFacultyTotal += input.nextDouble();
    				allFacultyCounter++;
    			}
    			else if (line.toLowerCase().contains(associate.toLowerCase()))
    			{
    				associateCounter++;
    				associateTotal += input.nextDouble();
    				associateAverage = associateTotal / associateCounter;
    				allFacultyTotal += input.nextDouble();
    				allFacultyCounter++;
    			}
    			else if (line.toLowerCase().contains(full.toLowerCase()))
    			{
    				fullCounter++;
    				fullTotal += input.nextDouble();
    				fullAverage = fullTotal / fullCounter;
    				allFacultyTotal += input.nextDouble();
    				allFacultyCounter++;	
    			}
    		}	
    		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:

    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:38)

    At line 38 is this: assistantTotal += input.nextDouble();
    This is in the IF block.

    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


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: scanner issues

    At line 38 in main() - we can't tell where that is - the Scanner method being used does not agree with the data found in the file. This usually occurs when a type of number is expected but text is found instead, or the number found does not comply with the requirements for that type. Try to find that mismatch and fix it.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: scanner issues

    Quote Originally Posted by GregBrannon View Post
    At line 38 in main() - we can't tell where that is - the Scanner method being used does not agree with the data found in the file. This usually occurs when a type of number is expected but text is found instead, or the number found does not comply with the requirements for that type. Try to find that mismatch and fix it.

    Hi.

    At line 38 is this: assistantTotal += input.nextDouble();
    This is in the IF block.

    Just for info, the file 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

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: scanner issues

    Then the input.nextDouble() statement isn't finding a legal token for a double in the file. Intelligently play with it and/or add print statements to determine what the Scanner object is seeing and then adjust your code to sync it up with the data in the text file. There isn't much more we can do for you without having all of the code and the text file (and we don't want it), but you should be able to figure this out yourself.

    Another option is to read a whole line at a time with Scanner.nextLine(), store that in a String array using the split() method:

    String[] inputData = input.nextLine().split( " " );

    Then you'd know that there are 4 elements in the array, the first 3 actual Strings, the last a String that can be parsed to a double when needed.

    Or, inputData could be sent directly to a constructor:

    personnelItem[i] = new PersonnelItem( inputData );

    Some variation of the above alternatives is how I would normally do it to avoid the errors you're fighting and to reduce the code required to read the file.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: scanner issues

    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: scanner issues

    Hi All.

    Fixed this.

    Change this:
    while (input.hasNextLine())
    {
    String line = input.nextLine();

    to this:

    while(input.hasNextLine()) {

    //line is next line from file
    String line = input.nextLine();
    //put into array contents of the line split by spaces
    String[] words = line.split(" ");
    //wage is variable at array position 4
    double wage = Double.parseDouble(words[3]);

    Mark as answered please, and thanks for all the help.

Similar Threads

  1. Issues with my ordering program - if statement issues?
    By Shenaniganizer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 31st, 2012, 10:17 PM
  2. [SOLVED] Scanner Issues: Read for more detail, thanks.
    By Staticity in forum Java SE APIs
    Replies: 3
    Last Post: September 8th, 2011, 03:38 AM
  3. Scanner help
    By sp11k3t3ht3rd in forum Java Theory & Questions
    Replies: 3
    Last Post: June 22nd, 2011, 11:55 AM
  4. Scanner/FileReader help
    By JohnTravoltaire in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 28th, 2011, 12:28 AM
  5. Help With Scanner
    By jtphenom in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 12th, 2009, 08:49 PM