Mit Scanner innerhalb einer for-Schleife für system-Eingang

Ich habe zu kämpfen mit diesem für eine Weile. Ich habe im wesentlichen wollen die Schleife durch und Lesen Sie, wie viele Saiten, wie bestimmt durch num_choices. Der folgende code nur ausgeführt wird, wird der else-Bedingung.

        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");

        }

    }

`

Bin ich immer dies: Exception in thread "main" java.util.NoSuchElementException. In der main-Klasse, dies ist, wo der Fehler Punkte zu

        choice2 = Integer.parseInt(read_choice2.next());

die innerhalb einer while-Schleife als gut. Hier ist der code dafür:

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);

}
}
}

Merke ich, gibt es eine Menge von unordentlichen code, und ich entschuldige mich dafür. Ich wollte nur zeigen das ganze, so ist es einfacher zu erkennen das problem. Hilfe würde geschätzt.

InformationsquelleAutor dawnoflife | 2012-10-27
Schreibe einen Kommentar