Results 1 to 5 of 5
  1. #1
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Need help with a Scanner issue...

    For some reason my code is auto-feeding a response for my scanner...can you please take a look at it for me please
    Java Code:
    import java.util.*;
    
    public class Payroll
    {
    	public static void main(String[] args)
    	{
    		//attributes
    		int decision;
    		String empType;
    		String name;
    		String socSec;
    		
    		//create scanner object for system input
    		Scanner scanner = new Scanner(System.in);
    		System.out.print("What do you want to do?\n     1 Create a new employee\n     2 Set pay\n     3 Show weekly pay\n     4 Show annual pay\n     5 Show summary\n     6 Quit\n==>");
    		decision = scanner.nextInt();
    		while (decision > 6||decision < 1)
    			{
    				System.out.println("ERROR Bad option, please try again.");
    				System.out.print("What do you want to do?\n     1 Create a new employee\n     2 Set pay\n     3 Show weekly pay\n     4 Show annual pay\n     5 Show summary\n     6 Quit\n==>");
    				decision = scanner.nextInt();
    			}
    		if(decision == 1)
    			{
    				Scanner employee = new Scanner(System.in);
    				System.out.print("What type of employee would you like to create (Salaried or Hourly)? ");
    				empType = employee.next();
    				if (empType.equals("Salaried")||empType.equals("salaried"))
    					{
    						Salaried emp1 = new Salaried();
    						System.out.print("Please enter the name: ");
    						name = scanner.nextLine();
    						if (name.length() > 1)
    							{
    								name = name;
    							}
    						else
    							{
    								System.out.println("ERROR Please enter more than one character");
    								System.out.print("Please enter the name: ");
    								name = scanner.nextLine();
    							}
    						emp1.setName(name);
    						System.out.print("Please enter the social security number with dashes: ");
    						socSec = scanner.nextLine();
    						if (socSec.length() < 10)
    							{
    								System.out.println("Error Please enter 8 numbers");
    								System.out.print("Please enter the social security number with dashes: ");
    								socSec = scanner.nextLine();
    							}
    						else
    							{
    								socSec = socSec;
    							}
    						emp1.setSocialSecurity(socSec);
    						
    						
    					}
    				else if(empType.equals("Hourly")||empType.equals("hourly"))
    				{
    					Hourly emp2 = new Hourly();
    					System.out.print("Please enter the name: ");
    					name = scanner.nextLine();
    						if (name.length() > 1)
    							{
    								name = name;
    							}
    						else
    							{
    								System.out.println("ERROR Please enter more than one character");
    								System.out.print("Please enter the name: ");
    								name = scanner.nextLine();
    							}
    						emp2.setName(name);
    						System.out.print("Please enter the social security number with dashes: ");
    						socSec = scanner.nextLine();
    						if (socSec.length() < 10)
    							{
    								System.out.println("Error Please enter 8 numbers");
    								System.out.print("Please enter the social security number with dashes: ");
    								socSec = scanner.nextLine();
    							}
    						else
    							{
    								socSec = socSec;
    							}
    						emp2.setSocialSecurity(socSec);
    				}
    				else
    					{
    						System.out.println("***Please enter either 'salaried' or 'hourly'");
    					}
    			}
    		
    	}
    }
    This is what I am getting:

    What do you want to do?
    1 Create a new employee
    2 Set pay
    3 Show weekly pay
    4 Show annual pay
    5 Show summary
    6 Quit
    ==>1
    What type of employee would you like to create (Salaried or Hourly)? salaried
    Please enter the name: ERROR Please enter more than one character
    Please enter the name: n
    Please enter the social security number with dashes:

  2. #2
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    107
    Rep Power
    0

    Default Re: Need help with a Scanner issue...

    1. I would call nextLine() all of the time instead of next() or nextInt() This will clear the end-of-lline character that is put there when the user hits the enter key on their keyboard.

    2. See the comments that I put in the code below that give suggestions. There are several places where there are assignments that don't do anything like:

    name = name

    What you need is a loop to be able to go around again when the input is not valid. When the input *is* valid, you won't need to do anything but break out of the loop.

    See my code below:

    Java Code:
    import java.util.*;
    
    class Employee {
    	
    	private String name;
    	private String ssn;
    	
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    	public void setSocialSecurity(String ssn) {
    		this.ssn = ssn;
    	}
    
    }
    
    class Hourly extends Employee {
    }
    
    class Salaried extends Employee {
    }
    
    // TODO
    // every place that you get input from the user, validate the input and go again around a loop
    // if the input is bad.
    public class Payroll
    {
    	public static void main(String[] args)
    	{
    		//attributes
    		//int decision; <<<<< declare these variables where they are used is better
    		//String empType;
    		//String name;
    		//String socSec;
    
    		//create scanner object for system input
    		Scanner scanner = new Scanner(System.in);
    		System.out.print("What do you want to do?\n     1 Create a new employee\n     2 Set pay\n     3 Show weekly pay\n     4 Show annual pay\n     5 Show summary\n     6 Quit\n==>");
    		String answer = scanner.nextLine();
    		int decision = Integer.parseInt(answer);
    		while (decision > 6||decision < 1)
    		{
    			System.out.println("ERROR Bad option, please try again.");
    			System.out.print("What do you want to do?\n     1 Create a new employee\n     2 Set pay\n     3 Show weekly pay\n     4 Show annual pay\n     5 Show summary\n     6 Quit\n==>");
    			answer = scanner.nextLine();
    			decision = Integer.parseInt(answer);
    		}
    		if(decision == 1)
    		{
    			// Scanner employee = new Scanner(System.in); // <<<< don't need a second Scanner object
    			System.out.print("What type of employee would you like to create (Salaried or Hourly)? ");
    			String empType = scanner.nextLine();
    			if (empType.equals("Salaried")||empType.equals("salaried"))
    			{
    				Salaried emp1 = new Salaried();
    				System.out.print("Please enter the name: ");
    				String name = scanner.nextLine();
    				if (name.length() > 1)
    				{
    					name = name; // <<<<<<< this assignment does nothing
    				}
    				else
    				{
    					System.out.println("ERROR Please enter more than one character");
    					System.out.print("Please enter the name: ");
    					name = scanner.nextLine();
    				}
    				emp1.setName(name);
    				System.out.print("Please enter the social security number with dashes: ");
    				String socSec = scanner.nextLine();
    				if (socSec.length() < 10)
    				{
    					System.out.println("Error Please enter 8 numbers");
    					System.out.print("Please enter the social security number with dashes: ");
    					socSec = scanner.nextLine();
    				}
    				else
    				{
    					socSec = socSec; // <<<< this assignment does nothing either
    				}
    				emp1.setSocialSecurity(socSec);
    
    
    			}
    			else if(empType.equals("Hourly")||empType.equals("hourly"))
    			{
    				Hourly emp2 = new Hourly();
    				System.out.print("Please enter the name: ");
    				String name = scanner.nextLine();
    				if (name.length() > 1)
    				{
    					name = name; // <<<< don't need to do this.  It does nothing
    				}
    				else
    				{
    					System.out.println("ERROR Please enter more than one character");
    					System.out.print("Please enter the name: ");
    					name = scanner.nextLine();
    				}
    				emp2.setName(name);
    				System.out.print("Please enter the social security number with dashes: ");
    				String socSec = scanner.nextLine();
    				if (socSec.length() < 10)
    				{
    					System.out.println("Error Please enter 8 numbers");
    					System.out.print("Please enter the social security number with dashes: ");
    					socSec = scanner.nextLine();
    				}
    				else
    				{
    					socSec = socSec; // <<<<<< this does no t hing
    				}
    				emp2.setSocialSecurity(socSec);
    			}
    			else
    			{
    				System.out.println("***Please enter either 'salaried' or 'hourly'");
    			}
    		}
    
    	}
    }

  3. #3
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Re: Need help with a Scanner issue...

    Thank you...I will try that :)

  4. #4
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Need help with a Scanner issue...

    you might want to put the parseint in a do-while try-catch to prevent bombing.

    Java Code:
    do{ 
                try {
                    System.out.print("enter number \n");
                    String i = scan.nextLine();
                    int number = Integer.parseInt(i);
                    break;
                }
                catch (Exception e)
                {
                    System.out.println("Couldn't parse input, please eneter a valid number");
                }
            }while (true);

  5. #5
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Re: Need help with a Scanner issue...

    it all worked...thank you so much!!

Similar Threads

  1. Scanner issue
    By Imstuck in forum New To Java
    Replies: 3
    Last Post: 12-09-2012, 05:11 PM
  2. Simple Scanner Issue
    By jazzermonty in forum New To Java
    Replies: 7
    Last Post: 07-17-2012, 10:47 PM
  3. Issue with Scanner object
    By smvolt in forum New To Java
    Replies: 1
    Last Post: 06-27-2012, 08:54 AM
  4. Scanner delimiter issue
    By PrinceSendai in forum New To Java
    Replies: 3
    Last Post: 10-05-2010, 09:41 AM
  5. Class Scanner looping issue
    By Stev0 in forum New To Java
    Replies: 1
    Last Post: 05-25-2008, 06:53 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
  •