空输入java

时间:2013-12-29 22:09:58

标签: java input null

您好我正在尝试创建一个程序,为此我需要多次读取输入,但在某些时候输入为空。我无法理解我做错了什么可以帮助我?

         System.out.print("Give destination folder: \n");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String destination=null;
        String input = null;
        try {
            input = br.readLine();
            //br.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        destination=Directory + File.separator +input;
        File destination_folder=new File(destination);
        if (destination_folder.exists()==true){
            System.out.print("");
        }
        else {
            System.out.print("Folder doesn't exists! Do you want to create it?\n");
            if(Prompt()==true){
                File dir=new File(input);
                dir.mkdir();                        
            }
        }

        System.out.print("Give name of folders to continue\n");
        br = new BufferedReader(new InputStreamReader(System.in));
        try {
            input = br.readLine();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String name=input;
        resultList = new ArrayList<File>();
        File Dir=new File(Directory);
        File[] fList = Dir.listFiles();
        resultList.addAll(Arrays.asList(fList)); 
        System.out.print("name=" + name);

        System.out.print("Folders of Directory:\n");
        for (File file : fList) {
             if (file.isDirectory()) {
                 if(file.getName().toLowerCase().contains(name)) 
                    System.out.println(file.getName());
            }
        }



public static boolean Prompt(){
        do{
            System.out.print("Do you want to cotinue? \nPress y or n\n");
            char c = 'a';
            try {
             c = (char) System.in.read();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(c=='y') return true;
            if(c=='n') return false;

        }while(true);
    }
}

当我调用上面的函数时,如果调用了prompt函数,则name字符串为null。所以我想我必须在某种程度上清除输入垃圾或者其他错误?

3 个答案:

答案 0 :(得分:1)

我建议您使用java.util.Scanner,并且在调用hasNextLine()之前,请务必检查是否有一行要与nextLine()一起阅读(另外,您应始终clean up after yourself )。

java.util.Scanner scanner = new java.util.Scanner(
    System.in);
try {
  while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    // use the user's input....
  }
} finally {
  scanner.close();
}

答案 1 :(得分:1)

更改提示方法以将BufferedReader br解析为参数,并使用该对象读取字符,如下所示

public static boolean Prompt(BufferedReader br){
    do{
        System.out.print("Do you want to cotinue? \nPress y or n\n");
        char c = 'a';
        try {

         c = (char) br.read();//System.in.read();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(c=='y') return true;
        if(c=='n') return false;

    }while(true);
}

输入为空的原因是,一旦在提示方法中使用System.in.read(),它就不会等到在后续行中调用BufferedReader.readLine()时读取用户输入。

正如Elliott Frisch所说,最好使用java.util.scanner

答案 2 :(得分:1)

如果您想使用BufferedReader,您可以执行以下操作。

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = br.readLine()) != null) {
    //Do what you want with the line.
}