Scanner « Integer « Java Data Type Q&A

1. Java - Easier way to guarantee integer input through Scanner?

For a program I am writing, I need to ask a user for an integer between 1 and 8. I've tried multiple (cleaner) ways of doing this but none of them ...

2. How to use Scanner to accept only valid int as input

I'm trying to make a small program more robust and I need some help with that.

Scanner kb = new Scanner(;
int num1;
int num2 = 0;
System.out.print("Enter number 1: ");
num1 = kb.nextInt();
System.out.print("Enter number 2: ...

3. Java: When using a scanner on a series of integers, how do I skip some integers?

For example

 while (lineScan.hasNextLine()) {
        int x = lineScan.nextInt();
        x = lineScan.nextInt();

4. Scanner's nextDouble() method -- returning an integer?

Hello! Given the following code import java.util.Scanner; /** This program prints a description of an earthquake of a given magnitude. */ public class EarthquakeRunner { public static void main(String[] args) { Scanner in = new Scanner(; System.out.print("Enter a magnitude on the Richter scale: "); double magnitude = in.nextDouble(); Earthquake quake = new Earthquake(magnitude); System.out.println(quake.getDescription()); } } If I pass in "1 ...

5. Scanner Strings and Ints dont play nice

Hey guys I wanted to check this before I start researching into something thats not my problem in the code package bank; import java.util.Scanner; public class Bank { public static void main (String[] args) { int greeting; String name; System.out.println("Welcome to Eros's bank type 1 to make a new account or 2 to exit: "); Scanner scan = new Scanner (; ...

6. How to make a scanner read an int?

System.out.println("Maak een selectie uit de volgende opties..." + "\n" + "1" + ". Nieuwe tafel openen." + "\n" + "2" + ". Toon open tafels." + "\n" + "3" + ". Gebruiker opties"); String a =; if(a.equals("1")){ System.out.println("Gelieve een pin in te geven..."); int p = sc.nextInt(); [B][COLOR="Red"]Here is the problem[/COLOR][/B] if(p.equals(waiterz.getPin())){ System.out.println("U heeft een nieuwe tafel geopend!"); System.out.println("Gelieve een ...

7. scanner to read string or int

hello all, just getting into java and have a question i am sure has been asked many times. i could not find anything through the search so here is my question. i am asking the user for some input which may be some text or an integer. how can i analyse both the string and int without knowing which will be ...

8. how to input unspecified number if ints with a scanner

i have to get input using the scanner class which is no big deal but it has to read an unspecified number of digits and am not sure how to do that and haven't had any luck finding out how.... this program runs on a loop and i dont think my problem is the loop maybe the input scanner? here is ...

9. Scanner to int?

You can't compare a Scanner object to an int, and you don't want to try either. Your Scanner object is used to get input from the user and should only be used for that. Then once you've used one of the Scanner's methods to get input from the user, you then test the input received in your while condition.

10. End Scanner int input with text value

I have a Scanner object and the values being input are integers. What I would like to do is have the user enter an 's' to stop the input. I have the following code (which of course throws an error). How can I accomplish my goal? Thank you. int stop = 's'; int number; Scanner input = new Scanner(; System.out.println("Enter the ...

11. How do I read a string and int from the same line with scanner class?

I didn't try to read the grade yet in that code. I can read grade easily with scan.nextInt(); but I dont know how to read and assign the names on my file to String name before I assign the numbers to int grade Edited by: JacenSolo on Apr 7, 2008 12:35 PM

13. Enforcing integer input with Scanner

hasNextXxx doesn't actually consume the next token. It just peeks at it. I type "ABC". So "ABC" is waiting for the scanner to consume it. You check hasNextInt(), which returns false and doesn't consume "ABC". You tell me to re-enter. I enter "123". So the Scanner now has "ABC" and "123" waiting to be consumed. You check hasNextInt() which returns false, ...