Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

having trouble with nextInt before nextLine, WHERE am I going wrong?

King_Kong2014King_Kong2014 DublinMember Posts: 2

When I use a scanner class with nextInt before a nextLine it ignores the nextLine command, please help!

import java.util.Scanner;

public class test1
{
public static void main( String [] args )
{
int yes=0,no=1;

    Scanner scan = new Scanner( System.in );

    System.out.println("Enter a 1 or a 0");
    int choice = scan.nextInt();

    if(choice==0)
    {
        System.out.println( "\nEnter your name > " );
        String name = scan.nextLine();
        System.out.println( "\nYour name is " + name );

            System.out.println( "\nEnter your age > " );
            int age = scan.nextInt();
            System.out.println( "\nYour age is " + age );
    }

    else
    {
        System.out.println("Enter 1");
    }

}
}

Comments

Sign In or Register to comment.