在for循环中使用Scanner进行系统输入

时间:2012-10-27 01:59:37

标签: java java.util.scanner

我一直在努力解决这个问题。我基本上想要循环并读入由num_choices确定的尽可能多的字符串。以下代码仅执行else条件。

        Scanner s2 = new Scanner(System.in);

        for(int i=0; i < this.num_choices; i++)
        {
          if(s2.hasNext())
          {

            System.out.println("Enter choice " + (i+1) +":");
            String ch = s2.next();
            //this.choices.addElement(ch);
          }
        else
        {
            System.out.println("Lets end this");

        }

    }

`

我得到了这个:Exception in thread "main" java.util.NoSuchElementException。在主类中,这是错误指向

的位置
        choice2 = Integer.parseInt(read_choice2.next());

也在while循环中。这是代码:

public class Main 
{
public static void main(String args[]) throws IOException
{
    Vector<Survey> mysurveys = new Vector<Survey>();
    boolean carry_on = true;
    int choice = 0;
    Scanner read_choice = new Scanner(System.in);

    System.out.println("Let's begin the Survey/Test application!");
    while(carry_on)
    {
        System.out.println("What would you like to do?");
        System.out.println("1. Create a new Survey");
        System.out.println("2. Create a new Test");
        System.out.println("3. Display a Survey");
        System.out.println("4. Display a Test");
        System.out.println("5. Save a Survey");
        System.out.println("6. Save a Test");
        System.out.println("7. Load a Survey");
        System.out.println("8. Load a Test");
        System.out.println("9. Quit");
        System.out.println();
        System.out.println("Please enter a number for the operation you want to perform: ");
        choice = Integer.parseInt(read_choice.next());
        /*try
        {
            choice = Integer.parseInt(buffer.readLine());

        }
        catch(InputMismatchException  e)
        {
            System.out.println("Invalid input. Please Enter again.");
            System.out.println();
            //read_choice.nextInt();
        }*/

        switch(choice)
        {
        case 1:
            System.out.println("Please Enter a Name for your Survey");
            String in = buffer.readLine();
            Survey s1 = new Survey();
            s1.CreateNew(in);
            mysurveys.add(s1);
            ////
            add_question(s1.type);
            break;

        case 2:
            System.out.println("Please Enter a Name for your Test");
            //String in = buffer.readLine();
            Test t1 = new Test();
            //t1.CreateNew(in);
            mysurveys.add(t1);
            break;
            ////
            //add_question(t1.type);
        case 3:


            break;
            // call Survey.display()


        case 4:


            break;


        case 5:

            Survey s = new Survey();
            ReadWriteFiles x = new ReadWriteFiles();
            x.SaveSurvey(s);
            break;


        case 6:

            Test t = new Test();
            //ReadWriteFiles x = new ReadWriteFiles();
            //x.SaveSurvey(t);
            break;


        case 7:

            carry_on = false;
            break;

        default:

            System.out.println("Incorrect Input. Try Again");
            System.out.println();
            break;
        }
    }

    read_choice.close();


}







 public static void add_question(String type) throws IOException, NullPointerException
 {
Questions q = null;
boolean carry_on2 = true;
int choice2 = 0;
Scanner read_choice2 = new Scanner(System.in);
//BufferedReader buffer2=new BufferedReader(new InputStreamReader(System.in));

while (carry_on2)
{
    //
    System.out.println("1. Add a new T/F Question");
    System.out.println("2. Add a new Multiple Choice Question");
    System.out.println("3. Add a new Short Answer Question");
    System.out.println("4. Add a new Essay Question");
    System.out.println("5. Add a new Ranking Question");
    System.out.println("6. Add a new Matching Question");
    System.out.println("7. If you want to stop adding more questions, and go back to the main menu.");
    System.out.println("Please enter a number for the operation you want to perform: ");
    choice2 = Integer.parseInt(read_choice2.next());

    /*try
    {
        choice2 = Integer.parseInt(buffer2.readLine());

    }
    catch(InputMismatchException  e)
    {
        System.out.println("Invalid input. Please Enter again.");
        System.out.println();
        //read_choice2.nextInt();
    }*/
    switch(choice2)
    {
        case 1:
            q = new TrueFalse();
            break;
        case 2:
            q = new MultipleChoice();
            break;
        case 3:
            q = new ShortAnswer();
            break;
        case 4:
            q = new Essay();
            break;
        case 5:
            q = new Ranking();
            break;
        case 6:
            q = new Matching();
            break;
        case 7:
            carry_on2 = false;
            break;
        default:
            System.out.println("Incorrect Input.");
            break;

    }
    q.createQuestion(type);

}
}
}

我意识到有很多混乱的代码,我为此道歉。我只是想展示整个事情,所以更容易发现问题。帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

一般来说,您应该在调用if(read_choice.hasNext())之前添加read_choice.next();您有异常java.util.NoSuchElementException,因为找不到要读取的元素。这是一个好习惯。

关于您的问题,您收到错误是因为您在完成阅读前已关闭扫描仪。将read_choice.close()置于循环之外。

此外,为了简化,如果你想读取整数,只需简单:scanner.nextInt()

答案 1 :(得分:0)

read_choice.close(); 

只要您没有读完所有输入,请勿关闭扫描仪。还要关闭基础输入流(System.in),检查documention;

您无需多次初始化Scanner。只需创建一个实例并传递它(继续使用它)。

另外,

for(int i=0; i < this.num_choices; i++)
{
   //if(s2.hasNext())
   //{

       System.out.println("Enter choice " + (i+1) +":");
       String ch = s2.next();
       //this.choices.addElement(ch);

你不需要那个条件检查。 next()将阻止,直到输入输入。