Burak.
Burak.

Reputation: 598

Skipping nextInt without any nextLine - Java

Even though I'm not concerning with that nextLine() method, these lines of code pose a problem. I still couldn't figure it out why. I have checked other sections of my code and I come up with this part where I suspect.

    int value;
    int capacity;
    int choice;
    int testValue;
    int index;
    ArrayList<IntBag> bags = new ArrayList<IntBag>();

    choice = 0;
    testValue = 0;

    Scanner scan = new Scanner( System.in );

    while( choice != 9 )
    {
        value = 0;
        index = 0;
        System.out.println( "\n*********\n1.Create a new empty collection"
                + " with a specified maximum capacity\n2.Read a set "
                + "of positive values into the collection\n3.Print the"
                + " collection of values.\n4.Add a value to the collection"
                + " of values at a specified locaiton\n5.Remove the value at"
                + " a specified location from the collection of values\n6.Read "
                + "a single test value.\n7.Compute the set of locations of the "
                + "test value within the collection\n8.Print the set of locations."
                + "\n9.Quit the program.");
        System.out.print( "\nChoice: " );
        choice = scan.nextInt();

        if( choice == 1 )
        {
            System.out.print( "\nPlease enter the capacity: " );
            capacity = scan.nextInt();
            if( capacity > 0 )
            {
                bags.add( new IntBag( capacity ) );
                System.out.println( "\nCollection has successfully created!" );
            }
            else
                System.out.println( "\nInvalid capacity!");
        }
        if( !bags.isEmpty() )
        {
            if( choice == 2 )
            {
                int input;
                System.out.print( "\nPlease enter the value you want to assign(use 0 to finish): " );

                for( int i = 0; i < bags.get(0).size(); i++ )
                {
                    input = scan.nextInt();
                    if( input != 0 )
                    {
                         System.out.println( i + ": " ); 
                         bags.get(0).add( input );
                    }
                    else
                        break;
                }

It actually should seek for any input other than 0 (0 terminates it) however, not even getting closer that point, this issue began to occur.

Note: bags is just an ArrayList object which has a generic type of a class that I wrote. It basically represents an array of integers, I think it does not constitute anything about this problem.

EDIT: It skips the last input = scan.nextInt(); line in for loop.

Thanks!

Upvotes: 1

Views: 1303

Answers (1)

SMA
SMA

Reputation: 37023

You are pressing return key after every number. So when you type number followed by return key, scanner treats number as one input while enter as another input and hence skips the next integer number.

You should either enter all the numbers in a line and then press enter or better use nextLine method and then convert that String into Integer.

Upvotes: 2

Related Questions