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

Thread: Scanner Issues - I think

  1. #1
    Member
    Join Date
    May 2014
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Scanner Issues - I think

    Hello,

    I have all the code down and it seems to be running ok, except for one part. After I ask the user for the employee's first name, it immediately jumps to asking for the last name, without giving the user a chance to enter a first name. If needed I can post the other classes, but I figured this problem is mainly on the driver.

    import java.util.Scanner;
    import java.util.Vector;
     
    public class PRJ01
    {
        public static void main (String [] args)
        {
            Vector <Employee> emp = new Vector();
            Scanner scannerIn = new Scanner(System.in);
     
            int index = 0;
            int count = 0;
            double sum = 0;
     
            do{
                System.out.println("What type of employee would you like to enter(1 for Executive Employee, 2 for Wage Employee," +
                        " 3 for Part Time Employee. or a NEG number to quit): ");
                index = scannerIn.nextInt();
     
                if (index > 3)
                {
                    System.out.println("Please enter an appropriate number!!");
                }
                else if (index == 1)
                {
                    System.out.println("Enter the employee's ID: ");
                    int eId = scannerIn.nextInt();
                    System.out.println("Enter the employee's first name: ");
                    String fName = scannerIn.nextLine();
                    System.out.println("Enter the employee's last name: ");
                    String lName = scannerIn.nextLine();
                    System.out.println("Enter the employee's year end bonus: ");
                    double yEndBon = scannerIn.nextDouble();
                    System.out.println("Enter the employee's yearly executive bonus: ");
                    double yExBon = scannerIn.nextDouble();
     
                    ExecutiveEmployee temp = new ExecutiveEmployee(eId, fName, lName, 40, 60000.00, yEndBon, yExBon);
                    emp.add(temp);
                    count++;
     
                }
                else if (index == 2)
                {
                    System.out.println("Enter the employee's ID: ");
                    int eId = scannerIn.nextInt();
                    System.out.println("Enter the employee's first name: ");
                    String fName = scannerIn.nextLine();
                    System.out.println("Enter the employee's last name: ");
                    String lName = scannerIn.nextLine();
                    System.out.println("Enter the employee's hours worked: ");
                    double wHours = scannerIn.nextDouble();
                    System.out.println("Enter the employee's pay rate: ");
                    double pRate = scannerIn.nextDouble();
     
                    WageEmployee temp = new WageEmployee(eId, fName, lName, wHours, pRate);
                    emp.add(temp);
                    count++;
     
                }
                else
                {
                    System.out.println("Enter the employee's ID: ");
                    int eId = scannerIn.nextInt();
                    System.out.println("Enter the employee's first name: ");
                    String fName = scannerIn.nextLine();
                    System.out.println("Enter the employee's last name: ");
                    String lName = scannerIn.nextLine();
                    System.out.println("Enter the employee's hours worked: ");
                    double wHours = scannerIn.nextDouble();
                    System.out.println("Enter the employee's pay rate: ");
                    double pRate = scannerIn.nextDouble();
     
                    PartTimeEmployee temp = new PartTimeEmployee(eId, fName, lName, wHours, pRate);
                    emp.add(temp);
                    count++;
                }
     
            }while (index>0);
     
            for(int i=0; i<count; i++)
            {
                sum = sum + emp.get(i).calcWeeklyPay();
            }
     
            System.out.println("The total amount paid for employee's wages this week is: $" + sum);
     
        }
    }


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

    Default Re: Scanner Issues - I think

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Scanner.nextInt() (and others) only take the next 'token' in the input buffer, leaving any other items there, usually a linefeed. Scanner.nextLine() takes everything in the input buffer, up to and including the linefeed character. When a nextLine() follows a nextInt() (and others), the buffer must be 'flushed' and thrown away, something like:
    System.out.println("Enter the employee's ID: ");
    int eId = scannerIn.nextInt();
     
    // flush the buffer
    scannerIn.nextLine();
     
    System.out.println("Enter the employee's first name: ");
    String fName = scannerIn.nextLine();

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    meni (May 11th, 2014)

  4. #3
    Member
    Join Date
    May 2014
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Scanner Issues - I think

    I was fiddling with it more last night, after my post. When I changed the scannerIn.nextline() to just scannerIn.next() the program was stopping to accept input. Does that work as well? On this assignment, we are not looking at the vector, only the grand total of the calcWeeklyPay() method. So my new code looks like this:
    else if (index == 1)
                {
                    System.out.println("Enter the employee's ID: ");
                    int eId = scannerIn.nextInt();
                    System.out.println("Enter the employee's first name: ");
                    String fName = scannerIn.next();
                    System.out.println("Enter the employee's last name: ");
                    String lName = scannerIn.next();
                    System.out.println("Enter the employee's year end bonus: ");
                    double yEndBon = scannerIn.nextDouble();
                    System.out.println("Enter the employee's yearly executive bonus: ");
                    double yExBon = scannerIn.nextDouble();
     
                    ExecutiveEmployee temp = new ExecutiveEmployee(eId, fName, lName, 40, 60000.00, yEndBon, yExBon);
                    emp.add(temp);
                    count++;
     
                }

    If that doesn't work (for some reason), and I understand you right, is this how my code should look instead?
    else if (index == 1)
                {
                    System.out.println("Enter the employee's ID: ");
                    int eId = scannerIn.nextInt();
                    scannerIn.nextLine();
                    System.out.println("Enter the employee's first name: ");
                    String fName = scannerIn.nextLine();
                    System.out.println("Enter the employee's last name: ");
                    String lName = scannerIn.nextLine();
                    System.out.println("Enter the employee's year end bonus: ");
                    double yEndBon = scannerIn.nextDouble();
                    System.out.println("Enter the employee's yearly executive bonus: ");
                    double yExBon = scannerIn.nextDouble();
     
                    ExecutiveEmployee temp = new ExecutiveEmployee(eId, fName, lName, 40, 60000.00, yEndBon, yExBon);
                    emp.add(temp);
                    count++;
     
                }
    Last edited by meni; May 11th, 2014 at 09:03 AM.

Similar Threads

  1. scanner issues
    By CruelCoin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 10th, 2013, 06:05 AM
  2. if else issues
    By coolbreez in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 3rd, 2013, 12:12 AM
  3. 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
  4. [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
  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