0

我已经为此苦苦挣扎了一段时间。我本质上想循环并读入由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);

}
}
}

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

4

2 回答 2

1

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

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

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

于 2012-10-27T02:17:00.940 回答
0
read_choice.close(); 

只要您没有完成所有输入的读取,就不要关闭扫描仪。这样做还会关闭底层输入流(System.in),检查文档

您不需要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()阻塞直到输入输入。

于 2012-10-27T02:13:31.157 回答