BufferedReader字符串检查

时间:2014-03-28 14:21:17

标签: java bufferedreader

请您推荐以下的最佳方式:

  1. 检查input.readLine().isEmpty()
  2. 检查Integer.parseInt(input.readLine()) <10 && Integer.parseInt(input.readLine()) > 1
  3. 我不知道如何使用更少的迭代来执行此操作:

        System.out.println("Please enter N natural number of Strings :");
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        try {
    
            if (input.readLine().isEmpty()) {
                System.out.println("Dear User! \nYou did not specify number of Strings \nOr number of Strings greater then 10 \nOr number of Strings less then 1");
    
            } else {
                int numberOfString = Integer.parseInt(input.readLine());
                stringArrayList = new ArrayList<String>(numberOfString);
    

5 个答案:

答案 0 :(得分:3)

您应该拨打readLine() 一次并使用该输入 - 否则您每次都会检查不同的行!

String line = input.readLine();
// readLine returns null for "end of input"
if (line == null || line.isEmpty()) {
    // Report error
} else {
    int number = Integer.parse(line);
    if (number < 1 || number > 10) {
        // Report error 
    } else {
        // Handle success
    }
}

答案 1 :(得分:0)

您正在使用input.readLine两次:

  • 首先检查空
  • 其次在您的else声明中,当您解析int

始终会评估您的第一个if语句(除非您抛出NullPointerException即:),但永远不会检索该值。

因此,如果您的第一次评估返回false,则int解析将基于input读取的第二行。

在这两种情况下分配String line = input.readLine()和参考line

还要记得检查null

答案 2 :(得分:0)

你正在使用input.readLine()两次。所以把它存放在字符串temp变量中。

String temp=input.readLine();
if(Integer.parseInt(temp)>10)  //or whatever you want
{
    //put your code here
 }

答案 3 :(得分:0)

有些事情需要考虑编写安全代码 1 - BufferedReader.readLine的JavaDoc说

  

返回:包含行内容的String,不包括   任何行终止字符,如果流的末尾有,则返回null   已达成

因此,检查它也很重要,但是当您使用System.in作为输入流时,我认为不存在任何问题。

最简单的方法是先读取字符串:

System.out.println("Please enter N natural number of Strings :");
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
do {
 String line = input.readLine();
 if(line != null) {
  if(list.trim().equals("")) {
   System.out.println("Dear User! \nYou did not specify number of Strings");
  } else {
   int numberOfString = 0;
   try {
    numberOfString Integer.parseInt(line);
    if(numberOfString < 1 || numberOfString > 10) {
     System.out.println("Dear User! \Number of string is either smaller then 1 or greater than 10");
    } else {
     stringArrayList = new ArrayList<String>(numberOfString);
     //ReadString(input);
     break;
    }        
   } catch (NumberFormatException e) {
    System.out.println("Dear User! \n" + line + " is not acceptable number");
   }
  }
 }
} while(true);

答案 4 :(得分:0)

非常感谢你!

我的代码看起来:

    public void initializeStringArr() {
    System.out.println("Please enter N natural number of Strings :");
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

    try {
        String line = input.readLine();
        if (line.isEmpty() || line == null) {
            System.out.println("Dear User! \nYou did not specify number of Strings ");
        } else {
            int numberOfString = Integer.parseInt(line);
            if(numberOfString<=1 || numberOfString>10 ){
                System.out.println("Number of Strings greater then 10 \n" + "Or number of Strings less(equals) then 1");
            }  else {
                stringArrayList = new ArrayList<String>(numberOfString);
                for (int i = 0; i < numberOfString; i++) {
                    System.out.println("Please enter your string :");
                    BufferedReader newStr = new BufferedReader(new InputStreamReader(System.in));
                    stringArrayList.add(newStr.readLine());
                }
            }