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

Thread: i cannot figure out what's wrong

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default i cannot figure out what's wrong

    I think i am doing it right but the program skips line 30 where it asks user to insert first name. i dont know why this happens and would like help.

    code is unfinish btw i just needed to fix this before i want to move forward

    import java.util.Scanner;
    public class EmployeeTest {
     
    	public static void main(String[] args){
    		String yourName;
    		String yourLastName;
    		double yourSalary;
     
    		Scanner input = new Scanner(System.in);
    		Employee applicant1 = new Employee("Trung","Lam",2729.00);
    		Employee applicant2 = new Employee("Vanessajoie","Castillo",4055.00);
     
    		System.out.printf("Initial employee1 is %s %s and makes $%.2f monthly\n",
    				applicant1.getName(), applicant1.getSurName(), applicant1.getSalary());
    		System.out.printf("Initial employee2 is %s %s and makes $%.2f monthly\n",
    				applicant2.getName(), applicant2.getSurName(), applicant2.getSalary());
     
    //set up new employee1
    		System.out.print("New employee1 first name: ");
    		yourName = input.nextLine();
    		System.out.print("New employee1 last name: ");
    		yourLastName = input.nextLine();
    		System.out.print("New employee1 salary: $");
    		yourSalary = input.nextDouble();
    		applicant1.setName(yourName);
    		applicant1.setSurName(yourLastName);
    		applicant1.setSalary(yourSalary);
    //set up new employee2
    		System.out.print("New employee2 first name: ");
    		yourName = input.nextLine();
    		System.out.print("New employee2 last name: ");
    		yourLastName = input.nextLine();
    		System.out.print("New employee2 salary: $");
    		yourSalary = input.nextDouble();
    		applicant2.setName(yourName);
    		applicant2.setSurName(yourLastName);
    		applicant2.setSalary(yourSalary);
     
    //list current employees
    		System.out.printf("Current employee1 is %s %s and makes $%.2f monthly\n",
    				applicant1.getName(), applicant1.getSurName(), applicant1.getSalary());
    		System.out.printf("Current employee2 is %s %s and makes $%.2f monthly\n", 
    				applicant2.getName(), applicant2.getSurName(), applicant2.getSalary());
     
    	}
    }

     
     
    public class Employee {
     
    	private String name;
    	private String surName;
    	private double salary;
     
    	public Employee(String eName, String eSurName, double eSalary){
    		name = eName;
    		surName = eSurName;
    		salary = eSalary;
    	}
    //setting values
    	public void setName(String eName){
    		name = eName;
    	}
    	public void setSurName(String eSurName){
    		surName = eSurName;
    	}
    	public void setSalary(double eSalary){
    		if (eSalary >= 0)
    			salary = eSalary;
    		else
    			salary = 0.0;
    	}
    //get values
    	public String getName(){
    		return name;
    	}
    	public String getSurName(){
    		return surName;
    	}
    	public double getSalary(){
    		return salary;
    	}
    }
    Last edited by biyuss; October 17th, 2011 at 09:16 PM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: i cannot figure out what's wrong

    A pitfall of using nextInt and nextDouble is that those methods do not consume the end of line. When user enters the salary of employee 1 the EOL is still in the buffer. When the program reaches the line asking for the name of employee 2 it scans the EOL still in the buffer and returns an empty String. Simple solution: add a call to nextLine after the call to nextDouble.
    Improving the world one idiot at a time!

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

    biyuss (October 17th, 2011)

  4. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: i cannot figure out what's wrong

    Thanks it worked! but what does EOL stand for?

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: i cannot figure out what's wrong

    Read my post again. It is short for something I mentioned earlier.
    Improving the world one idiot at a time!

Similar Threads

  1. can't figure whats wrong with my add method?? help!
    By b094mph in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 8th, 2011, 05:00 PM
  2. [SOLVED] can't figure out..
    By darego in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 10th, 2010, 02:26 PM
  3. I'm new at this and can't figure it out
    By jaheh06 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 22nd, 2010, 08:44 AM
  4. Generation of Palindrome number in Java
    By tina.goyal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 26th, 2009, 08:49 AM
  5. [SOLVED] Java program to prompt and display the user input number as odd or even
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 22nd, 2009, 01:19 AM