0

after asking for the age the scanner stops asking for the users input for favorite dessert. i followed it like above myObj next line but it still stops. please help

package scanners; import java.util.Scanner;

public class usingscanners {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner myObj = new Scanner(System.in);
    String userName;
    String dessert;
    
    int age; 
    
    System.out.println("Hi What is your name? ");
    
     userName = myObj.nextLine();
    
    System.out.println ("How are you doing, " + userName + "?");
    System.out.println ("What is you age, " + userName + "?");
    
    age = myObj.nextInt();
    
    //System.out.println ("Hello, " + userName +  " you are " + age);
    
    System.out.println ("ok, " + userName + " .Your are " + age + "." + " What is your favorite dessert?"); 
    
    dessert = myObj.nextLine();
    
    System.out.println (" Hello, " + userName + " You are " + age + " and your favorite dessert is " + dessert);
    
    }

}

swoly
  • 21
  • 2

1 Answers1

0

Don't use nextLine, it interacts weirdly with all the other nextX() methods. Instead, change the scanner's delimiter. After making a scanner, invoke:

scanner.useDelimiter("\r?\n");

Then to get e.g. an int, call .nextInt(). And to get somebody's name, call .next() (and don't call .nextLine() for anything).

rzwitserloot
  • 44,252
  • 4
  • 27
  • 37