-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,629
  • 4
  • 22
  • 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 Tapia
  • 32,670
  • 6
  • 50
  • 72