6 Replies - 1381 Views - Last Post: 15 January 2009 - 09:10 PM Rate Topic: -----

#1 s3thst4  Icon User is offline

  • a * a = (b * b) + (c * c) - 2(b)(c)cos(A)
  • member icon

Reputation: 10
  • View blog
  • Posts: 587
  • Joined: 20-November 08

Can you compare an int to nothing ?

Post icon  Posted 14 January 2009 - 06:06 PM

Hey, I'm trying to write a guessing game in Java in Console.
So, I'm user Scanner for input, and using input.nextInt();, and I need to see if they typed nothing in.
Also, it would be nice to be able to detect if they entered a string, rather than an int, or does nextInt cover that ?
Here's my code.

do {
System.out.println("Please enter a number from 0-10!");
int gn;
gn = input.nextInt(); 
} while (gn == ?? || gn < 0 && gn > 10); 



Where gn == ?? is the comparison to see if they entered nothing.

Thanks for your help

~Seth.

Is This A Good Question/Topic? 0
  • +

Replies To: Can you compare an int to nothing ?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Can you compare an int to nothing ?

Posted 14 January 2009 - 06:12 PM

View Posts3thst4, on 14 Jan, 2009 - 05:06 PM, said:

Hey, I'm trying to write a guessing game in Java in Console.
So, I'm user Scanner for input, and using input.nextInt();, and I need to see if they typed nothing in.
Also, it would be nice to be able to detect if they entered a string, rather than an int, or does nextInt cover that ?
Here's my code.

do {
System.out.println("Please enter a number from 0-10!");
int gn;
gn = input.nextInt(); 
} while (gn == ?? || gn < 0 && gn > 10); 



Where gn == ?? is the comparison to see if they entered nothing.

Thanks for your help

~Seth.


Don't worry about it
Scanner.nextInt() wont return to you until the users effectively types an int... so no need to check
Was This Post Helpful? 1
  • +
  • -

#3 s3thst4  Icon User is offline

  • a * a = (b * b) + (c * c) - 2(b)(c)cos(A)
  • member icon

Reputation: 10
  • View blog
  • Posts: 587
  • Joined: 20-November 08

Re: Can you compare an int to nothing ?

Posted 14 January 2009 - 06:15 PM

Thank you for your help!

~Seth.
Was This Post Helpful? 0
  • +
  • -

#4 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Can you compare an int to nothing ?

Posted 14 January 2009 - 06:25 PM

If this is what you're left with you'll get into problems though..

do {
//statements
} while (gn < 0 && gn > 10); 



gn can't be less than 0 and greater than 10 at the same time.
Was This Post Helpful? 0
  • +
  • -

#5 s3thst4  Icon User is offline

  • a * a = (b * b) + (c * c) - 2(b)(c)cos(A)
  • member icon

Reputation: 10
  • View blog
  • Posts: 587
  • Joined: 20-November 08

Re: Can you compare an int to nothing ?

Posted 14 January 2009 - 06:33 PM

Haha, I mistyped.

It was suppose to be

while(gn > 0 && gn < 10);

Sorry for any confusion.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10564
  • View blog
  • Posts: 39,091
  • Joined: 27-December 08

Re: Can you compare an int to nothing ?

Posted 15 January 2009 - 07:30 PM

View Postpbl, on 14 Jan, 2009 - 05:12 PM, said:

Don't worry about it
Scanner.nextInt() wont return to you until the users effectively types an int... so no need to check


Let me clarify this. If nothing is entered, then Scanner.nextInt() won't return anything. But if the user enters a String, char, boolean, long, float or double then it will crash the program. You have to check with a try/catch block. The exception is InputMismatchException I believe.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Can you compare an int to nothing ?

Posted 15 January 2009 - 09:10 PM

View Postmacosxnerd101, on 15 Jan, 2009 - 06:30 PM, said:

View Postpbl, on 14 Jan, 2009 - 05:12 PM, said:

Don't worry about it
Scanner.nextInt() wont return to you until the users effectively types an int... so no need to check


Let me clarify this. If nothing is entered, then Scanner.nextInt() won't return anything. But if the user enters a String, char, boolean, long, float or double then it will crash the program. You have to check with a try/catch block. The exception is InputMismatchException I believe.

macosxnerd101 is right.
The easiest way, when reading user input, is to always read as a String and then convert to what it is suposed to be:

int number;
bolean error = false;

do {
	 System.out.print("Enter an int: ");
	 String str = scanner.nextLine();
	 try {
		number = Integer.parseInt(str);
	 }
	 catch (NumberFormatException e) {
		 System.out.println("Sorry " + str + " is not a number"); 
		 error = true;
	 }
	 while(error);
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1