プログラムを実行してエラーチェックを行い、ユーザー入力が実際にintであることを確認できました。私が遭遇した問題は、それが3桁のintであることだけです。私はトラブル適切な場所にそれを得ることを持っている:私はそれを実装する必要があると私は問題に実行しているところで右ここにスキャナ入力のint長を確認するにはどうすればよいですか?
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);
です。私は、それがたぶん唯一の場所のラインまたは私が必要とする行方不明であると確信しています、私はちょうど何かを知らない。それが座っている間、それは常に長さにかかわらず、3桁の数字を入力するように私に求めます。入力した文字列のエラーチェックは、現在動作しません:
//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);
そして、ここではそれが私がそれを必要なものを行うためのを取得し、残りされています
//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]);
}
}
}
は、ここで出力です:
---- jGRASP exec:java listMnemonics
3桁の数字を入力してください:
MOTU
ではありません。
は3桁の数字を入力してください:
は3桁の数字を入力してください:
は3桁の数字を入力してください。 3桁の数字!再試行する!
は可能性ゼロをリードされていますか? 'nbr> 99 && nbr <1000'の何が問題なのですか? – pingul
はい、先頭または末尾のゼロは受け入れ可能な入力です。その場合、どこにその行を置くのですか?それは私のトラブルです。 – motu
その場合、 'int'として解析することはできません。あなたはそれを文字列として読み、それを自分で解析する必要があります。 – pingul