2014-11-08 104 views
0

我正在通过命令行创建一个用户界面,该用户界面将要求从1到4的选项,并且我想要进行错误检查。只允许1到4之间的整数。这是迄今为止的代码。我想方法返回userInput整数到另一个方法,它会做一些东西。try catch块中的变量范围

package contactmanager; 

import java.util.InputMismatchException; 
import java.util.Scanner; 

/** 
* 
* @author andyjohnson 
*/ 
public class UserInterface { 

    public static Integer GetInput() { 
     Scanner in = new Scanner(System.in); 
     //Integer userInput; 
     System.out.println("Welcome to the contact manager\nMake a selection below:"); 
     System.out.println("1)Display Contacts\n2)Add new business contact\n3)Add new personal contact\n4)Quit"); 
     try { 
      Integer userInput = in.nextInt(); 
      if (userInput < 1 || userInput > 4) { 
       System.out.println("Please enter a valid selection"); 
       UserInterface.GetInput(); 
      } 

     } catch (InputMismatchException e) { 
      e.getMessage(); 
      System.out.println("Please enter a valid selection"); 
      UserInterface.GetInput(); 
     } 

     return userInput; 

    } 

} 

我的return语句在IDE中被加下划线,并告诉我它没有被初始化。我想全局初始化它,但允许try语句改变它的值。我试过this.userInput = userInput,但我无法弄清楚我的范围被破坏了。我如何给try块的全局范围?我是新来的Java,所以任何东西都有帮助。谢谢!

回答

0

你可以只声明userInput变量try-catch块外:

package contactmanager; 

import java.util.InputMismatchException; 
import java.util.Scanner; 

public class UserInterface { 

    public static Integer GetInput() { 
     Scanner in = new Scanner(System.in); 
     System.out.println("Welcome to the contact manager\nMake a selection below:"); 
     System.out.println("1)Display Contacts\n2)Add new business contact\n3)Add new personal contact\n4)Quit"); 
     Integer userInput = null; 
     try { 
      userInput = in.nextInt(); 
      if (userInput < 1 || userInput > 4) { 
       System.out.println("Please enter a valid selection"); 
       UserInterface.GetInput(); 
      } 

     } catch (InputMismatchException e) { 
      e.getMessage(); 
      System.out.println("Please enter a valid selection"); 
      UserInterface.GetInput(); 
     } 

     return userInput; 

    } 

} 
0

try-catch块的涵盖范围是不同的,只是写行内代码的方法的其余部分。

我想你所遇到的问题是在这条线,其中变量userInput首先在try-catch块内部声明:

Integer userInput = in.nextInt(); 

之所以这样,是一个问题:

考虑一下,如果try-catch阻止失败。然后会返回什么?变量userInput还没有定义,所以Java不知道该返回什么。

修复相对简单。您想要将其从try-catch块中移出,就像这样。这应该摆脱您的return错误。我注意到你评论了这个改变。为什么?

但我有一个额外的建议。你为什么打电话给UserInterface.GetInput()?为什么不让方法接受有效输入的参数,并且在数据格式不正确时根本不调用它?你用它吗?这将消除真正的全球范围变量的需求。

由于如何编写此方法,它必须返回某种类型的Integer,除非您编写该方法,否则该方法会引发在下游某个位置捕获的异常。

我试图做一些修正,我认为将最有意义:

Scanner in = new Scanner(System.in); 
Integer userInput; // Integer representation of user input 

try { 
    Integer a = in.nextInt(); // if a can be assigned to an Integer this line work 
    if (a < 1 || a > 4) { 
     // called if the input can be assigned to an Integer and is within the range 
     userInput = a; 
    } 

} catch (InputMismatchException e) { 
    // otherwise the catch block is called 
    System.out.println("Please enter a valid selection"); 
} 

return userInput; 

也许你想打电话UserInterface.GetInput()范围检查里面?希望这可以帮助!

编辑:使用识别标记,而不是回顾的方法

Scanner input = new Scanner(System.in); 
System.out.println("Please enter a valid Integer. When done type DONE "); 

// this method will keep cycling until the String DONE is typed 
// you could make this condition whatever you want 
while (!input.hasNext("DONE")) { 

    String a = input.next(); // gets the next item from the Scanner 

    try { 
     Integer b = Integer.parseInt(a); // tries to 'cast' the String to an Integer 

     if (b < 1 || b > 4) { 
      System.out.println("Input is valid!"); 
     } else { 
      System.out.println("Input is invalid!"); 
     } 

    } catch (NumberFormatException e) { 
     System.out.println("Please enter a valid selection!"); 
    } 
} 
+0

我需要为每个规范再次调用UserInterface.GetInput()。如果输入验证失败,程序需要重新输入菜单。我试过这个建议,但它似乎没有验证输入的整数,如果它是0或5.输入像foo这样的字符串给了我在catch语句中的代码。如果尝试失败验证或者catch执行什么是再次调用该菜单方法的最佳方式?规范说这应该给用户输入验证失败后的菜单。我想让UserInterface方法是静态的,所以它不需要实例化。 – 2014-11-08 21:06:02

+0

我不知道你正在查看的'规范'中列出了什么条件。我测试了这个方法,它似乎适用于我。我不明白'UserInterface.GetInput()'如何绑定到它,因为你已经得到了输入。 – 2014-11-08 21:15:29

+0

为了清楚起见,我在原始答案中添加了评论。 – 2014-11-08 21:18:57