-1
else if(digit == 2)
{
      System.out.print("Enter the name of artist : ");
      String artist=input.nextLine();  

      System.out.print("Enter the song title : ");
      String song = input.nextLine();

      System.out.print("Enter the number of week :");
      int oldWeek =input.nextInt();

      System.out.print("Enter the new number of week :");
      int newWeek =input.nextInt()
}

Hello , I am running a else if loop where if a user enters 2 the following code will run but the problem here seems to be that when it runs it runs 2 System.out.println code together . Here is the sample of my output

This program will display singles that was Number one on charts 
Enter 1 or 2. 2
Enter the name of artist : Enter the song title : 
PakkuDon
  • 1,631
  • 4
  • 21
  • 21
Fenil_18
  • 1
  • 1

1 Answers1

3

It seems like you have a nextInt() executed before those nextLine(). What happens is that nextInt() doesn't consume the new-line character, therefore the next nextLine() will consume it. That makes like that nextLine() were beign skipped. For further information you could see this entry.

One solution would be calling a nextLine() after the nextInt() in order to consume the new-line character:

int number = scanner.nextInt();
scanner.nextLine(); // Just to consume new-line character
...

Another solution would be using nextLine() to read the integer, and then parsing it with Integer.parseInt():

int number = Integer.parseInt(scanner.nextLine());
...
Christian
  • 31,246
  • 6
  • 45
  • 67
  • Hey i tried doing it but still when it reaches to System.out.print("Enter the name of artist : "); String artist=input.nextLine(); it will automatically show same behaviour – Fenil_18 Feb 04 '14 at 06:23
  • This issue is pretty common, read the link I posted and you will understand why this happens. And maybe you won't need our help anymore. – Christian Feb 04 '14 at 06:25
  • Thank you for the link i went through the page and solved my issue – Fenil_18 Feb 04 '14 at 06:30
  • No problem, and welcome to StackOverflow! – Christian Feb 04 '14 at 06:30