-1

I call a method in my main app to give some String variables to the constructor of a class but here is a problem: When I call the method (while running the program), my first input line is being ignored. Here is my code :

Here i call the method in main.

Rent r1=new Rent();
RentProcess(r1);

This is the method with the problem.

    public static void RentProcess(Rent r) {
        System.out.println("Name and telephone : \n");
        r.setNameAndTel(in.nextLine());
        System.out.println("Rental date : \n");
        r.setRentDate(in.nextLine());
    }

Here is the ingoring line when i run the app.

Choose between : 

 1 --> Rent 
 2 --> Exit
1
Name and telephone : 

Rental date : 

20/2

And when i print the info above.

Rent Code : 0
 Name and Telephone 
 Date of Rent : 20/2
 Days of Rent : 0
 Rent cost is : 0.0
 Name : The Wolf Of Wall Street
RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
Damis Berzovitis
  • 205
  • 1
  • 3
  • 11
  • Where are you setting the `in` which you are using in your `RentProcess` method? – kaykay May 02 '15 at 13:08
  • 1
    The problem is probably in the place where you read the `1`. You are probably using `in.nextInt()` and without clearing the end-of-line. Please show the code where you do that. – RealSkeptic May 02 '15 at 13:11
  • Is it on purpose, that you make an extra newline, additional to the new line from `System.out.println()` ? – Sebastian Walla May 02 '15 at 13:14
  • Post code which will actually let us reproduce your problem, but as RealSkeptic said 99% cases of this problem is due to using `nextLine()` after `nextInt()` or other `nextFoo()` methods. You can read more about it here http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx – Pshemo May 02 '15 at 13:20

1 Answers1

0

Although you are not putting the complete code. But it seems you are using the same scanner to read the integer and the strings which is causing you the problem.

This line try to read the remainder of the line containing the int which is empty.

 r.setNameAndTel(in.nextLine());

Add another in.nextLine() before this line and you should be fine.

Hussein Zawawi
  • 2,907
  • 2
  • 26
  • 44