Hello! its me again, I need a liitle help.
Our teacher gave us a homework where we need to create a program that will determine the larger of the two inputed number, plus we have to determine if the input is an integer or not. Ive already coded the program, but when I run it whenever I enter anything other than an integer it goes into an infinte loop (instead of asking for another input) HELP PLEASEEEE

import java.util.*;
public class Homework1b {

    public static void main (String [] args)
    {
    	Scanner input = new Scanner(System.in);
    	int number1=0,
    		number2=0;
    	boolean num1=false,
    			num2=false;
    	Compare check =  new Compare();
    	while(num1==false){
    	System.out.print("Enter an Integer: ");
    		if (input.hasNextInt())
    		{
    				number1=input.nextInt();
    				num1=true;
    		}
			else
    			System.out.println("Invalid Input!");
    	}
    	while(num2==false){
    	System.out.print("Enter another Integer: ");
    		if (input.hasNextInt())
    		{
    				number2=input.nextInt();
    				num2=true;
    		}
			else
    			System.out.println("Invalid Input!");
    	}
    	check.compare(number1,number2);
    	
    }
    
    
}

This is how I would approach your problem:

import java.io.*;
class homeWorklb
{
public static void main(String[] args) throws IOException
{
   BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
   String inPut;

   System.out.println("Please enter first integer");
   inPut = stdin.readLine();
   int num1 = Integer.parseInt(inPut);

   System.out.println("Please enter second integer");
      inPut = stdin.readLine();
   int num2 = Integer.parseInt(inPut);

if(num1 > num2)
      System.out.println("It appears that the first number is larger");

else if(num1 == num2)
     System.out.println("It appears that numbers are equal");

else
     System.out.println("It appears that the second number is larger");

}
}

Not sure how to check if input is integers, but this way automatically parses to integer.

The problem there is that our teacher told us to use the method hasNextInt() so I cant use the BufferedReader

I think that in the part of your code after while, where you tell them to enter first integer, you haven't made a statement which saves the inputted integer to he variable number1

I think that in the part of your code after while, where you tell them to enter first integer, you haven't made a statement which saves the inputted integer to he variable number1

I did, it will store the inputted number only when you enter an integer.

while(num1==false){
    	System.out.print("Enter an Integer: ");
    		if (input.hasNextInt())
    		{
    				[B]number1=input.nextInt();[/B]
    				num1=true;
    		}
			else
    			System.out.println("Invalid Input!");
    	}

Ok, the only thing preventing your loop from working is that that the call to hasNextInt() does not actually read (advance) past the input, it just determines what is there. If you enter a number then your code reads it with the nextInt() call, but if something other than an int is entered, the input is never read and the scanner stays at that position, so every hasNextInt() call still says "Nope, the input isn't an int". To fix that, you need to read off the invalid input before asking again, like so

while(num1==false){
System.out.print("Enter an Integer: ");
    if (input.hasNextInt())
    {
            number1=input.nextInt();
            num1=true;
    }
    else {
        System.out.println("Invalid Input!");
        [B]input.next();  // This will advance the scanner[/B]
    }
}

redZero's code is completely invalid for what you are trying to do, so just disregard it. It doesn't use Scanner, it has no loop to re-query after invalid input, and throws IOException from main() rather than dealing with it in a try-catch block, which is a really bad idea. Exceptions should never be thrown by main().

Something you might want to consider is putting the code that gets the int input into a separate method. You have repeated the same code twice to get two numbers and repetition like that should stand out as a candidate for it's own method.

commented: AWESOME!!! +1

ooohhh.. THANK YOU THANK YOU!!!! SOOO MUCH!! You Saved My (Academic) Life!!! I need to submit this prog in 5 hrs!!! thank youuuu

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.