循环检查用户输入的空白​​区域

时间:2015-02-10 09:31:22

标签: java arrays

我有一个编写应用程序的任务,该应用程序将从用户获取输入,并将每个答案存储在String数组中。此应用程序还必须检查用户输入的空值,并重新提示用户输入他们的答案。应用程序必须只重复空白问题,因为此应用程序有50个问题可以使用。

到目前为止,这是我的代码:

for(iLoop=0; iLoop<noOfQuest; iLoop++)
        {
            if(noOfEmails<1)
            {
                System.out.println("FILE_ERROR: Please ensure line \"noOfEmails\" is more than 0");
            }
            if(noOfQuest==1)
            {
                System.out.println(prefinalMess);
                System.out.println(questionArr[0]);
                ansArray[iLoop]=ans.next();
                System.out.println(finalMess);
            }
            if(noOfQuest==2)
            {
                System.out.println(questionArr[0]);
                ansArray[iLoop]=ans.next();
                System.out.println(prefinalMess);
                System.out.println(questionArr[1]);
                ansArray[iLoop]=ans.next();
                System.out.println(finalMess);
            }
            if(noOfQuest==3)
            {
                System.out.println(questionArr[0]);
                ansArray[iLoop]=ans.next();
                System.out.println(questionArr[1]);
                ansArray[iLoop]=ans.next();
                System.out.println(prefinalMess);
                System.out.println(questionArr[2]);
                ansArray[iLoop]=ans.next();
                System.out.println(finalMess);
            }
            if(noOfQuest==4)
            {
                System.out.println(questionArr[0]);
                ansArray[iLoop]=ans.next();
                System.out.println(questionArr[1]);
                ansArray[iLoop]=ans.next();
                System.out.println(questionArr[2]);
                ansArray[iLoop]=ans.next();
                System.out.println(prefinalMess);
                System.out.println(questionArr[3]);
                ansArray[iLoop]=ans.next();
                System.out.println(finalMess);
            }
            if(noOfQuest==5)
            {
                System.out.println(questionArr[0]);
                ansArray[iLoop]=ans.next();
                System.out.println(questionArr[1]);
                ansArray[iLoop]=ans.next();
                System.out.println(questionArr[2]);
                ansArray[iLoop]=ans.next();
                System.out.println(questionArr[3]);
                ansArray[iLoop]=ans.next();
                System.out.println(prefinalMess);
                System.out.println(questionArr[3]);
                ansArray[iLoop]=ans.next();
                System.out.println(finalMess);
            }
        }

我还必须开发方法来检查每个答案的输入。

1 个答案:

答案 0 :(得分:0)

这里有一个从命令行读取和检查用户输入的方法:

public static List<String> readNLinesOfInput(int lines2Read){
    List<String> inputs = new ArrayList<String>();
    try (Scanner sc = new Scanner(System.in)) { 
        while(inputs.size() < lines2Read) {
            System.out.println("Enter the "+(inputs.size()+1)+" line: ");
            String line = sc.nextLine();
            if("".equals(line)){
                System.out.print("Empty lines a not allowed. ");
                continue;
            }
            inputs.add(line);
        }
    }
    return inputs;
}

您可以调用它并迭代每一行,如下所示:

public static void main(String[] args) {
    List<String> list = readNLinesOfInput(50);
    for(String line: list){
        System.out.println(line);
    }
}