motu motu - 1 month ago 5
Java Question

How can I verify the int length of scanner input?

I was able to get the program to run and work with error checking to make sure that the user input is in fact an int. The issue I ran into is that I only want it to be a 3-digit int. I'm having trouble getting that into the right place:

import java.util.*;

public class listMnemonics

{

public static void main(String[] args)

{

//Defines the "keypad" similar to that of a phone
char[][] letters =

{{'0'},{'1'},{'A','B','C'},{'D','E','F'},{'G','H','I'},{'J','K','L'},
{'M','N','O'},{'P','Q','R','S'},{'T','U','V'},{'W','X','Y','Z'}};

//Creates the Scanner
Scanner scan = new Scanner(System.in);


Right here is where I need to implement that and I am running into the issue. I'm sure it's maybe only a line out of place or missing that I need, I just don't know what or where. As it sits, it will constantly ask me to enter a 3-digit number, no matter the length. Error checking for a string entered does currently work:

//Gives instructions to the user to enter 3-digit number
//Any amount of numbers will work, but instructions help
//System.out.println("Please enter a 3-digit number: ");

int j;


do
{
System.out.println("Please enter a 3-digit number: ");

while (!scan.hasNextInt()) {

System.out.println("That's not a 3-digit number! Try again!");
scan.next(); // this is important!
}

j = scan.nextInt();

}



//while (j <= 0); This works while not checking digit length
while (j != 3);
int w = (int) Math.log10(j) +1; //Found this, but not sure if it helps or not

String n = Integer.toString(w);


And here is the rest that get's it to do what I need it to:

//Determines char length based on user input
char[][] sel = new char[n.length()][];

for (int i = 0; i < n.length(); i++)

{

//Grabs the characters at their given position
int digit = Integer.parseInt("" +n.charAt(i));
sel[i] = letters[digit];

}

mnemonics(sel, 0, "");

}

public static void mnemonics(char[][] symbols, int n, String s)

{

if (n == symbols.length)

{

System.out.println(s);
return;

}

for (int i = 0; i < symbols[n].length; i ++)

{

mnemonics(symbols, n+1, s + symbols[n][i]);

}
}
}


Here's the output:


----jGRASP exec: java listMnemonics

Please enter a 3-digit number:

2345

Please enter a 3-digit number:

12

Please enter a 3-digit number:

123

Please enter a 3-digit number:

motu

That's not a 3-digit number! Try again!

MvG MvG
Answer

Condensed and reformatted your code reads

Scanner scan = new Scanner(System.in);
int j;
do {
  System.out.println("Please enter a 3-digit number: ");
  while (!scan.hasNextInt()) {
     System.out.println("That's not a 3-digit number! Try again!");
     scan.next(); // this is important!
   }
   j = scan.nextInt();
} while (j != 3);

Comparing that to the Scanner documentation we can see that the scan.next() call will read (and discard) the non-int token. Otherwise j will be the integer you read. And you continue doing so while the number you read is different from 3. Not the length of the number, but the number itself. So if you want to end the loop, enter 3. If you want to do so while following the prompt, enter 003.

If that's not what you want to check, then change the end of loop condition. Or perhaps change the way you test for three-digit numbers, by using regular expressions to match these.

Pattern threeDigitNumber = Pattern.compile("^\\d\\d\\d$");
int j;
System.out.println("Please enter a 3-digit number: ");
while (!scan.hasNext(threeDigitNumber))
  System.out.println(scan.next() + " is not a 3-digit number! Try again!");
j = scan.nextInt();
Comments