我能够让程序运行并使用错误检查来确保用户输入实际上是一个int。我遇到的问题是我只想要它是一个3位数的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位数字:
2345
请输入一个3位数字:
12
请输入一个3位数字:
123
请输入一个3位数字:
MOTU
这不是一个3位数的数字!再试一次!
答案 0 :(得分:0)
压缩并重新格式化您的代码
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);
将其与the Scanner
documentation进行比较,我们可以看到scan.next()
调用将读取(并丢弃)非int令牌。否则j
将是您读取的整数。并且你继续这样做,而你读的数字不同于3.不是数字的长度,而是数字本身。因此,如果您想结束循环,请输入3
。如果您想在按照提示操作时这样做,请输入003
。
如果这不是您要检查的内容,则更改循环结束条件。或者通过使用正则表达式匹配这些数字来改变测试三位数字的方式。
Scanner scan = new Scanner(System.in);
Pattern threeDigitNumber = Pattern.compile("\\d\\d\\d");
int j;
System.out.println("Please enter a 3-digit number: ");
while (!scan.hasNext(threeDigitNumber)) {
if (scan.hasNext()) {
System.out.println(scan.next() + " is not a 3-digit number! Try again!");
} else {
System.out.println("Input terminated unepxectedly");
System.exit(1);
}
}
j = scan.nextInt();
正如评论所指出的那样,模式"\\d\\d\\d"
也可以写为"[0-9]{3}"
,或"\\d{3}"
或"[0-9][0-9][0-9]"
。在数字位数是变量的情况下,使用{…}
可能很有用。
documentation for Scanner.hasNext(Pattern)
要求模式匹配输入。这显然遵循将Matcher.matches()
语义与模式匹配整个字符串,而不是Matcher.find()
来检查字符串是否包含与模式匹配的任何部分。因此,输入不必包含在^
和$
中,正如我最初假设的那样,实际上不应该使用这些输入,除非使用Pattern.MULTILINE
flag编译模式。 / p>
您可能希望仅使用换行符调用Scanner.useDelimiter
来划分界限。
Scanner.useDelimiter("[\\r\\n]+")
答案 1 :(得分:0)
在MvG和pingul的帮助下,这正是我目前希望的工作方式:
import java.util.*;
import java.util.regex.Pattern;
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);
// Gives instructions to the user to enter 3-digit number
// This 'Pattern' also guarantees that only 3 digits works.
Pattern threeDigitNumber = Pattern.compile("[0-9]{3}");
int j;
do
{
System.out.println("Please enter a 3-digit phone number: ");
// If it's not a 3-digit int, try again
while (!scan.hasNext(threeDigitNumber))
{
System.out.println("That's not a 3-digit number! Try again!");
// This is important!
scan.next();
}
j = scan.nextInt();
}
while (j <= 0);
String n = Integer.toString(j);
// 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, "");
}
// Here is where the magic happens and creates the possible
// letter combinations based on the user input and characters
// selected in previous steps.
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]);
}
}
}