Results 1 to 7 of 7
  1. #1
    Bravid2001 is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default using the nextLine method returns nothing

    Hello,

    I am creating an Employee class. I have created the class and now I am creating the test class. I have run into a problem that involves inputing the address. When I ask for an address, I can't figure out how to combine all the strings in line 39. I'm using the nextLine() method. It just returns a blank space. I kind of understand why. It is because, if the nextLine() is invoked after a token-reading method, the method reads characters that start from this delimiter and end with the line seperator. The line seperator is read, but it is not part of the string returned by nextLine(). Ok, that's why it happens. But how do I fix this problem.

    Here is my new code:

    Java Code:
    import java.util.Scanner;
    public class TestEmployeeV4
    {
    	public static void main (String[] args)
    	{
    		//Create variables
    		String nameFirst = "";
    		String nameLast = "";
    		String phone = "";
    		String addressOfEmp = "";
    		int idEmp = 0;
    		String titleEmp = "";
    		double salaryEmp = 0.0;
    		//add this array to UML
    		int numberOfEmployees = 5;
    
    
    		//create a new scanner
    		Scanner input = new Scanner(System.in);
    
    
    		//create a default employee
    		Employee myEmployee = new Employee();
    
    		//create a new employee
    		Employee myEmployee1 = new Employee(nameFirst, nameLast, phone, addressOfEmp, idEmp, titleEmp, salaryEmp);
    
    		System.out.println ("Enter employee first name");
    			nameFirst = input.next();
    
    		System.out.println ("Enter employee last name");
    			nameLast = input.next();
    
    		System.out.println ("Enter employee phone number");
    			phone = input.next();
    
    		//how do you tie all these strings together
    		System.out.println ("Enter employee address");
    			addressOfEmp = input.nextLine();
    
    
    
    		System.out.println ("Enter employee ID");
    			idEmp = input.nextInt();
    
    		System.out.println ("Enter employee title");
    			titleEmp = input.next();
    
    		System.out.println ("Enter employee salary");
    			salaryEmp = input.nextDouble();
    
    		//invoke setFirstName method
    		myEmployee1.setFirstName(nameFirst);
    
    		//invoke setLastName method
    		myEmployee1.setLastName(nameLast);
    
    		//invoke setPhoneNumber method
    		myEmployee1.setPhoneNumber(phone);
    
    		//invoke setAddress method
    		myEmployee1.setAddress(addressOfEmp);
    
    		//invoke setID method
    		myEmployee1.setID(idEmp);
    
    		//invoke setTitle method
    		myEmployee1.setTitle(titleEmp);
    
    		//invoke setSalary method
    		myEmployee1.setSalary(salaryEmp);
    
    		System.out.println("The employee's name is " + myEmployee1.getFullName() );
    		System.out.println("The employee's phone number is " + myEmployee1.getPhoneNumber());
    		System.out.println("The employee's address is " + myEmployee1.getAddress() );
    		System.out.println("The employee's ID# is " + myEmployee1.getID() );
    		System.out.println("The employee's title is " + myEmployee1.getTitle() );
    		System.out.println("The employee's salary is $" + myEmployee1.getSalary() );
    
    
    	}
    
    
    }
    I have attached my 2 programs. Thanks.
    Attached Files Attached Files

  2. #2
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default Re: using the nextLine method returns nothing

    Are you sure that the nextLine method returns nothing?
    Java Code:
    String addressOfEmp;
    addressOfEmp = input.nextLine();
    because it will store the next line that the user stores into the console in the variable addressOfEmp. To test this use System.out.println(addressOfEmp);

    Hope this helps,

    Regards Serb.

  3. #3
    Bravid2001 is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default Re: using the nextLine method returns nothing

    To test this use System.out.println(addressOfEmp);

    Hope this helps,

    Regards Serb.[/QUOTE]

    Hello,

    I tried this test and it just skips over that section and jumps right to the next question.

    Thanks.

  4. #4
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default Re: using the nextLine method returns nothing

    I don't know whether "Thanks" was used sarcastically or not but if it was i feel reluctant to help you any further. In case that was not the intention i have modified your code and it does work (for me anyway). I've commented saying where i have made the changes.

    Java Code:
    import java.util.Scanner;
    public class TestEmployeeV5
    {
        public static void main (String[] args)
        {
            //Create variables
            String nameFirst = "";
            String nameLast = "";
            String phone = "";
            String addressOfEmp = "";
            int idEmp = 0;
            String idemp; //Employee id string declaration
            String titleEmp = "";
            double salaryEmp = 0.0;
            //add this array to UML
            int numberOfEmployees = 5;
     
     
            //create a new scanner
            Scanner input = new Scanner(System.in);
     
     
            //create a default employee
            Employee myEmployee = new Employee();
     
            //create a new employee
            Employee myEmployee1 = new Employee(nameFirst, nameLast, phone, addressOfEmp, idEmp, titleEmp, salaryEmp);
     
            System.out.println ("Enter employee first name");
                nameFirst = input.nextLine();      //I've changed some of these into input.nextLine() instead of input.next()
     
            System.out.println ("Enter employee last name");
                nameLast = input.nextLine();
     
            System.out.println ("Enter employee phone number");
                phone = input.nextLine();
     
            //how do you tie all these strings together
            System.out.println ("Enter employee address");
                addressOfEmp = input.nextLine(); 
     
            System.out.println ("Enter employee ID");
                idemp = input.nextLine();      //Read the next line, place it in the variable i declared earlier
                idEmp = Integer.parseInt(idemp);  //Convert this to an integer (this step is now not skipped)
                
            System.out.println ("Enter employee title");
                titleEmp = input.nextLine();
     
            System.out.println ("Enter employee salary");
                salaryEmp = input.nextDouble();
     
            //invoke setFirstName method
            myEmployee1.setFirstName(nameFirst);
     
            //invoke setLastName method
            myEmployee1.setLastName(nameLast);
     
            //invoke setPhoneNumber method
            myEmployee1.setPhoneNumber(phone);
     
            //invoke setAddress method
            myEmployee1.setAddress(addressOfEmp);
     
            //invoke setID method
            myEmployee1.setID(idEmp);
     
            //invoke setTitle method
            myEmployee1.setTitle(titleEmp);
     
            //invoke setSalary method
            myEmployee1.setSalary(salaryEmp);
     
            System.out.println("The employee's name is " + myEmployee1.getFullName() );
            System.out.println("The employee's phone number is " + myEmployee1.getPhoneNumber());
            System.out.println("The employee's address is " + myEmployee1.getAddress() );
            System.out.println("The employee's ID# is " + myEmployee1.getID() );
            System.out.println("The employee's title is " + myEmployee1.getTitle() );
            System.out.println("The employee's salary is $" + myEmployee1.getSalary() );
     
     
        }
     
     
    }
    Hope this helps,

    Regards Serb.

  5. #5
    Bravid2001 is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default Re: using the nextLine method returns nothing

    Serb,

    So sorry. That was not meant sarcastically. I only had a second to type the reply so I can understand how it might come off short.

    When i get a minute, I will look at the code you sent me.

    Thanks for your help and my appologies.

  6. #6
    Bravid2001 is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default Re: using the nextLine method returns nothing

    This definitely solves the problem. Now I am trying to figure out why. Is it that, if I am going to code something like this, I can't use input.next()? The very last println statement is a double. Does that work because it is the last thing asked for?

    Thank you.

  7. #7
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default Re: using the nextLine method returns nothing

    Don't worry about it. Anyway it's mainly because I am more familiar with using nextLine() for reading strings from the console.

    Visit: Java Input Using Java Scanner

    Scroll down towards the bottom of the page and within red font it should talk about clashes with nextLine and nextInt and it says that may developers use different scanner for reading integers. Maybe this will solve your problem instead. I think that nextDouble() works b/c it is the last one except there may not be a clash with this and nextLine().

    Hope this helps,

    Regards Serb.

Similar Threads

  1. Reflection - Invoking method returns an exception
    By crikey in forum Advanced Java
    Replies: 4
    Last Post: 08-02-2011, 08:17 PM
  2. Inherited method that returns string
    By Xeal Rebad in forum New To Java
    Replies: 5
    Last Post: 05-23-2011, 01:26 PM
  3. Problems with method which returns result
    By new Object() in forum New To Java
    Replies: 2
    Last Post: 12-01-2010, 12:16 PM
  4. Replies: 5
    Last Post: 10-30-2010, 12:05 AM
  5. Problem Of Scanner Object with its method nextLine()
    By Cluster Storm in forum AWT / Swing
    Replies: 12
    Last Post: 06-17-2010, 05:40 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
  •