Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.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 在for循环中使用扫描仪进行系统输入_Java_Java.util.scanner - Fatal编程技术网

Java 在for循环中使用扫描仪进行系统输入

Java 在for循环中使用扫描仪进行系统输入,java,java.util.scanner,Java,Java.util.scanner,我已经为此挣扎了一段时间。我基本上希望循环并读入由num\u选项决定的尽可能多的字符串。以下代码仅执行else条件 Scanner s2 = new Scanner(System.in); for(int i=0; i < this.num_choices; i++) { if(s2.hasNext()) { System.out.println("Enter choice

我已经为此挣扎了一段时间。我基本上希望循环并读入由
num\u选项决定的尽可能多的字符串。以下代码仅执行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");

        }

    }
也在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);

}
}
}
公共类主
{
公共静态void main(字符串args[])引发IOException
{
Vector mysurveys=新向量();
布尔进位=真;
int-choice=0;
扫描仪读取选项=新扫描仪(System.in);
System.out.println(“让我们开始调查/测试应用程序吧!”);
while(继续)
{
System.out.println(“您想做什么?”);
System.out.println(“1.创建新调查”);
System.out.println(“2.创建新测试”);
System.out.println(“3.显示调查”);
System.out.println(“4.显示测试”);
System.out.println(“5.保存调查”);
System.out.println(“6.保存测试”);
System.out.println(“7.加载调查”);
System.out.println(“8.加载测试”);
System.out.println(“9.Quit”);
System.out.println();
System.out.println(“请输入要执行的操作的编号:”);
choice=Integer.parseInt(read_choice.next());
/*试一试
{
choice=Integer.parseInt(buffer.readLine());
}
捕获(输入不匹配异常e)
{
System.out.println(“输入无效,请重新输入”);
System.out.println();
//读_choice.nextInt();
}*/
开关(选择)
{
案例1:
System.out.println(“请输入调查的名称”);
字符串in=buffer.readLine();
调查s1=新调查();
s1.创建新的(in);
mysurveys.add(s1);
////
添加问题(s1.类型);
打破
案例2:
System.out.println(“请输入测试名称”);
//字符串in=buffer.readLine();
测试t1=新测试();
//t1.CreateNew(in);
mysurveys.add(t1);
打破
////
//添加问题(t1.类型);
案例3:
打破
//call Survey.display()
案例4:
打破
案例5:
调查s=新调查();
ReadWriteFiles x=新的ReadWriteFiles();
x、 调查;;
打破
案例6:
测试t=新测试();
//ReadWriteFiles x=新的ReadWriteFiles();
//x、 储蓄调查(t);
打破
案例7:
继续=错误;
打破
违约:
System.out.println(“输入不正确,请重试”);
System.out.println();
打破
}
}
read_choice.close();
}
公共静态void add_问题(字符串类型)引发IOException、NullPointerException
{
问题q=null;
布尔进位2=真;
int-choice2=0;
扫描器读取选项2=新扫描器(System.in);
//BufferedReader buffer2=新的BufferedReader(新的InputStreamReader(System.in));
while(携带2)
{
//
System.out.println(“1.添加新的T/F问题”);
System.out.println(“2.添加新的选择题”);
System.out.println(“3.添加新的简短回答问题”);
System.out.println(“4.添加新的论文问题”);
System.out.println(“5.添加新的排名问题”);
System.out.println(“6.添加新的匹配问题”);
System.out.println(“7.如果您想停止添加更多问题,请返回主菜单。”);
System.out.println(“请输入要执行的操作的编号:”);
choice2=Integer.parseInt(读_choice2.next());
/*试一试
{
choice2=Integer.parseInt(buffer2.readLine());
}
捕获(输入不匹配异常e)
{
System.out.println(“输入无效,请重新输入”);
System.out.println();
//阅读_choice2.nextInt();
}*/
开关(选项2)
{
案例1:
q=新的TrueFalse();
打破
案例2:
q=新的多重切变();
打破
案例3:
q=新的简短回答();
打破
案例4:
q=新论文();
打破
案例5:
q=新排名();
打破
案例6:
q=新匹配();
打破
案例7:
进位2=假;
打破
违约:
System.out.println(“输入错误”);
打破
}
q、 问题(类型);
}
}
}
我意识到有很多乱七八糟的代码,对此我深表歉意。我只是想展示整个过程,这样更容易发现问题。我们将不胜感激

read_choice.close(); 
只要没有读取所有输入,就不要关闭扫描仪。Doing还关闭底层输入流(
系统。在
中),检查

您不需要多次初始化
扫描仪。只需创建一个实例并传递它(继续使用它)

而且

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);
for(int i=0;i

您不需要该条件检查。
next()
将一直阻塞,直到输入。

一般来说,在调用
read\u choice.hasNext()之前,您应该添加
if(read\u choice.hasNext())
您有异常
java.util.NoTouchElementException
,因为找不到可读取的元素。这是一个好习惯

关于您的问题,您遇到了错误,因为您在完成读取之前关闭了扫描仪。请将
read\u choice.close()
置于循环之外

此外,为了简化,如果您想读取整数,只需简单: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);