Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
处理输入非整数变量Java的错误_Java - Fatal编程技术网

处理输入非整数变量Java的错误

处理输入非整数变量Java的错误,java,Java,我正在编写一个程序,它接受用户输入的整数和字符串,并对字符串执行操作。我已经创建了这个程序,它运行得很好,我的问题是,我现在正在尝试处理有人为userInput输入非整数值的错误,我不知道该怎么做。我曾尝试使用Try-Catch语句,但在这样做时,我不断收到userInput变量的错误消息 我想做的是当userInput不是整数时,将布尔inputError设置为true,这样我的while循环反复要求用户输入整数,直到他们输入为止 public class Q3 { public static

我正在编写一个程序,它接受用户输入的整数和字符串,并对字符串执行操作。我已经创建了这个程序,它运行得很好,我的问题是,我现在正在尝试处理有人为userInput输入非整数值的错误,我不知道该怎么做。我曾尝试使用Try-Catch语句,但在这样做时,我不断收到userInput变量的错误消息

我想做的是当userInput不是整数时,将布尔inputError设置为true,这样我的while循环反复要求用户输入整数,直到他们输入为止

public class Q3 {
public static void main(String[] args)
{   
    Scanner in = new Scanner(System.in);
    boolean inputError = false;

    try{
        System.out.print("Please enter a number between 5 and 10, inclusively: ");
        String userInput1 = in.nextLine();
        int userInput = Integer.parseInt(userInput1);
    }
    catch(InputMismatchException e)
    {
        inputError = true;
    }

    // If userInput is not between 5 and 10, set the boolean inputError to true.
    if (userInput < 5 || userInput > 10)
    {
        inputError = true;
    }

    // Repeatedly ask for user input if they do not enter a number between 5 and 10.
    while(inputError)
    {
        System.out.print("Error. Please enter a number between 5 and 10, inclusively: ");
        userInput = in.nextInt();
        in.nextLine();
        if (userInput >= 5 || userInput <= 10)
        {
            inputError = false;
        }
    }

    // Take user's input for the string.
    System.out.print("Please enter a string of length 6 characters: ");
    String textToChange = in.nextLine();
    int length = 6;
    String printArray = "";
    String wordsOdd = "";
    String finalConcat ="";
    String transitionString="";

    // Print error if text is not 6 characters long.
    while(textToChange.length() != 6) 
    {
        System.out.println("Error! Enter a string of length 6.");
        textToChange = in.nextLine();
    }
公共类Q3{
公共静态void main(字符串[]args)
{   
扫描仪输入=新扫描仪(系统输入);
布尔输入错误=false;
试一试{
System.out.print(“请输入一个介于5和10之间的数字,包括:”);
字符串userInput1=in.nextLine();
int userInput=Integer.parseInt(userInput1);
}
捕获(输入不匹配异常e)
{
inputError=真;
}
//如果userInput不在5和10之间,请将布尔inputError设置为true。
如果(用户输入<5 | |用户输入>10)
{
inputError=真;
}
//如果用户没有输入介于5和10之间的数字,则重复要求用户输入。
while(输入者)
{
System.out.print(“错误。请输入一个介于5和10之间的数字,包括:”);
userInput=in.nextInt();
in.nextLine();

如果(userInput>=5 | | userInput,问题是变量“userInput”在
try catch
块中声明,这意味着在该块结束后将不存在。您应该在主方法开始时初始化它们,以便可以从主方法内的任何代码块全局访问它们

int userInput = 1; // Set default initialisation. 
String userInput1 = "";

try {
  NumberFormat.getInstance().parse(userInput1);
  userInput = Integer.parseInt(userInput1);
}
catch(ParseException e) {
  inputError = true; //not a number
}

您需要检查循环内输入有效性的所有代码:

Scanner in = new Scanner(System.in);
boolean inputError = true;
int userInput = 0;

while (inputError) {
    try {
        System.out.print("Please enter a number between 5 and 10, inclusively: ");
        String userInput1 = in.nextLine();
        userInput = Integer.parseInt(userInput1);
        inputError = (userInput < 5 || userInput > 10);
    } catch (NumberFormatException e) {
        inputError = true;
    }
    if (inputError)
        System.out.println("Wrong input");        
}
System.out.print("Please enter a string of length 6 characters: ");
..................................
Scanner-in=新的扫描仪(System.in);
布尔输入错误=真;
int userInput=0;
while(输入者){
试一试{
System.out.print(“请输入一个介于5和10之间的数字,包括:”);
字符串userInput1=in.nextLine();
userInput=Integer.parseInt(userInput1);
inputError=(用户输入<5 | |用户输入>10);
}捕获(数字格式){
inputError=真;
}
if(输入者)
System.out.println(“错误输入”);
}
System.out.print(“请输入长度为6个字符的字符串:”);
..................................

在将有效整数作为
userInput

传递之前,上述循环将永远不会结束。您的实际问题是什么?try-catch会出现哪些错误?您是如何实现try-catch的?@Stultuske我已更新代码以反映我的尝试。我收到错误“userInput无法解析为变量”。我认为这是因为作用域的缘故,但我不确定,并尝试在catch之外声明它,这也不起作用。您的问题是作用域。您在第一个try块中将userInput声明为局部变量,这意味着在执行try块并finished@Stultuske即使我在外面宣布,我也会n出现错误“变量可能未初始化”,所以我仍然不确定它需要什么修复。你能提供建议吗?谢谢你的帮助。所以给它一个默认初始化。String userInput=null;在第一次尝试使用它之前,用userInput=…替换当前的字符串userInput=…这并不能真正解决他在实现中遇到的问题。@NickAth tha谢谢。我这样做了,并在main中将inputError初始化为默认值,它起了作用。@LukeByrne在我的第二条评论中,您应该能够解决您的问题issue@NickAth始终建议您不仅发布代码,还应详细说明代码解决问题的原因/方式。请记住,此处的答案不仅适用于提问者,也适用于also对于世界上其他面临同样问题的人来说。@Ivar你是对的,但我认为评论部分已经给出了必要的解释,无论如何,我将尝试更新我的答案:)上面给出了一个错误,当我在控制台输入一个字符串以进行用户输入时。@LukeByrne捕获得很好,必须捕获的是
NumberFormatException
,而不是
InputMismatchException
。请参阅我的编辑。感谢更新。我还有一个问题,即当它重复时,如果输入错误,它不会打印错误消息输入put。我想首先要求他们输入整数,然后如果他们输入了错误的值,则打印一个错误。有没有办法通过try/catch实现这一点?我尝试了,但没有成功。@LukeByrne在我的回答中看到了编辑消息