Results 1 to 8 of 8
  1. #1
    seanmovieman is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Unhappy Need help with code please

    Every time i try and enter empName as John Doe it gives me an error but if i type it with no space its fine. i need to be able to input the empName with spaces. help please. heres to code.
    import java.util.Scanner;
    import java.util.*;


    public class Assignment1_2{


    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int empID;
    String empName;
    int earn;
    int health = 125;
    System.out.println("Enter an employee ID");
    empID = input.nextInt();
    System.out.println("Enter an employee Name");
    empName = input.next();
    System.out.println("Enter employees Earnings");
    earn = input.nextInt();
    double fedTax = (earn * .15);
    double staTax = (earn * .05);
    double social = (earn * .05);
    double med = (earn * .03);
    double pen = (earn * .08);
    double netPay = (earn - (fedTax + staTax + social + med + pen + health));
    System.out.println("Employee ID: " + empID);
    System.out.println("Employee Name: " + empName);
    System.out.println("============================== =====");
    System.out.println("EARNINGS: " + earn);
    System.out.println("Federal Tax: " + fedTax);
    System.out.println("State Tax: " + staTax);
    System.out.println("Social Security: " + social);
    System.out.println("Medicare/Medicaid: " + med);
    System.out.println("Pension Plan: " + pen);
    System.out.println("Health Insurance: " + health);
    System.out.println("============================== =====");
    System.out.println("NET PAY: " + netPay);
    }
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Because
    Java Code:
    input.next();
    only takes the next amount of input up to the next separator (which, by default, is whitespace, AFAIK), so when you type "John Doe" next only reads "John" and the call to nextInt() attempts to read "Doe". Use
    Java Code:
    input.nextLine();
    at that point.

  3. #3
    seanmovieman is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    i tried that and now it wont even let me input a name. any other thoughts?
    Last edited by seanmovieman; 10-04-2009 at 09:50 PM.

  4. #4
    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 seanmovieman View Post
    i tried that and now it want even let me input a name. any other thoughts?
    His thoughts are correct and you must be trying it incorrectly. I suggest that you call input.nextLine() after every input.nextInt() or input.next(), etc...

    If this still doesn't work, then post your latest code, but please use code tags (see my signature).

    Luck.

  5. #5
    seanmovieman is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    
    
    
    public class Assignment1_2{ 
                 
                  
                  public static void main(String[] args) { 
                  Scanner input = new Scanner(System.in);              
                  int empID;
                  String empName;
                  int earn;
                  double health = 125;
                  System.out.println("Enter an employee ID");
                  empID = input.nextInt();
                  System.out.println("Enter an employee Name");
                  empName = input.nextLine();
                  System.out.println("Enter employees Earnings");
                  earn = input.nextInt();
                  double fedTax = (earn * .15);
                  double staTax = (earn * .05);
                  double social = (earn * .05);
                  double med = (earn * .03);
                  double pen = (earn * .08);
                  double netPay = (earn - (fedTax + staTax + social + med + pen + health));
                  System.out.println("Employee ID:               " + empID);
                  System.out.println("Employee Name:             " + empName);
                  System.out.println("===================================");
                  System.out.println("EARNINGS:                  " + earn);
                  System.out.println("Federal Tax:               " + fedTax);
                  System.out.println("State Tax:                 " + staTax);
                  System.out.println("Social Security:           " + social);
                  System.out.println("Medicare/Medicaid:         " + med);
                  System.out.println("Pension Plan:              " + pen);
                  System.out.println("Health Insurance:          " + health);
                  System.out.println("===================================");
                  System.out.println("NET PAY:                   " + netPay);
                  }
    }
    this is the code i have right now and it still won't let me input a name.

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

  7. #7
    seanmovieman is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    i was confused by what you meant with calling input.nextLine() after input.nextInt() can you explain it again.

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

Similar Threads

  1. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  2. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  3. Why doesn't this code accept my code?
    By PeterFeng in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 01:39 PM
  4. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 10:12 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 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
  •