0

so i m trying to take all these string inputs from the user. now the problem i m facing is that i m getting output like this when i select case 2. Why are the line first and second both printed together. Cant figure it out. thanks in advance.

Output is

You've chosen item #2
Enter the Street Address of the house:
Enter the name of the town

case 2:    

    System.out.println("You've chosen item #2");
    System.out.println("Enter the Street Address of the house:");
    String streetAddress = sc.nextLine();


    System.out.println("Enter the name of the town");
    String nameTown = sc.nextLine();

    System.out.println("Enter the postcode of the town");
    String townPostcode = sc.nextLine();

    System.out.println("Enter the name of the landlord:");
    String landLordname =sc.nextLine();

    System.out.println("Enter the contact number of the landlord:");
    String landlordContact = sc.nextLine();

    System.out.println("Enter the name of the tenant if any or enter null");
    String tenantName=sc.nextLine();

    System.out.println("Enter the contact number of the tenant if any or enter null");
    String tenantContact = sc.nextLine();


    System.out.println("Enter the rent of the house if any or enter null");
    String rent =sc.nextLine();

    System.out.println("Enter the number of weeks in the lease contract or enter null");
    int lease = sc.nextInt();`
Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346
  • Please look at [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – Hovercraft Full Of Eels Oct 09 '16 at 01:22
  • hi in the duplicate code the scanner is skipping the nextLine(); because there is a nextInt(); above it. but in my case there is no other input statements above it still it is skipping the first input. and the funny thing is rest of the code works fine it is just skipping the first nextLine(); – Vishal Chauhan Oct 09 '16 at 01:33
  • But I'm guessing that you do in fact have nextInt above a nextLine -- The code above is within a loop of some sort and repeats no? – Hovercraft Full Of Eels Oct 09 '16 at 01:34
  • But i got an idea from the reference of the duplicate code. I just fired a blank sc.nextLine(); in between the first two lines and it works now. Thanks :D – Vishal Chauhan Oct 09 '16 at 01:37
  • Because it **is** a duplicate. – Hovercraft Full Of Eels Oct 09 '16 at 01:38
  • ohk.. np :) thanks anyway. – Vishal Chauhan Oct 09 '16 at 01:46

0 Answers0