WizKid22
WizKid22

Reputation: 23

Second scanner in my program wont put the input value into my integer?

I am going to post my code right away and ask the question below it.

    System.out.println("Enter your starting integer: ");
    firstInt = scnr.nextInt();
    System.out.println("Enter your last integer: ");
    secondInt = scnr.nextInt();

    int i = firstInt;
    while (i < secondInt) {

The first input is taken just fine. But when I try to input to secondInt, I hit enter and it wont move onto my while loop it is just stuck in the scanner. I hit enter and i just move down a line to input more. I watn it to move to my while loop. This is probably an easy fix but im pretty new to coding so any help would be appreciated. Thanks in advance!

Upvotes: 0

Views: 65

Answers (1)

Jeremiah Stillings
Jeremiah Stillings

Reputation: 773

import java.util.Scanner;
public class Tyler

{

    public static void main(String[] args)
    {
        Scanner stdin = new Scanner(System.in);
        // input first int
        System.out.print("Enter your starting integer: ");
        int firstInt = stdin.nextInt();
        //input second int
        // consume line
        stdin.nextLine();
        System.out.print("Enter your last integer: ");
        int secondInt = stdin.nextInt();

        // output data
        // There was no way to break out of your while loop so this should be done with an If/else
        if (firstInt <= secondInt)
        {
            System.out.println("First number is less then second number");
        }
        else
        {
        System.out.println("Second number is less then first number");
        }

    }

}

Upvotes: 1

Related Questions