4

I am using bufferedReader to read System.in from the user, The user will enter a domain address and source address separated by a space.

eg 
auckland.ac.nz. 198.19.20.44
www.esc.auckland.ac. 173.27.28.93
stat.auckland.ac.nz. 52.159.152.105
student.auckland.ac.nz. 64.247.240.232

Now the problem is that once the user finishes entering the data, the input stream does not terminate and hence the program does not get executed. I have written some code for the BufferedReader if anyone is willing to check it and tell me where I went wrong I would greatly appreciate it. Notice the two variables "count" and "n", is there anyway I can use those to help with that?

     try {
        String fqdn, src, key, temp;
        Integer count;
        int n = 0;

        while ((temp = br.readLine()) != null){
            int divide = temp.indexOf(" ");
            fqdn = temp.substring(0, divide); // divide the input 
            src = temp.substring(divide+1);
            key = fqdn + " " + src;
            //System.out.printf("%d: %s\n", n, key);
            dht.lookup(key);
            n += 1;
        } 
        br.close();
    } catch (NoSuchElementException e) {
        //System.out.printf("end-of-file\n");    
    }
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jay
  • 520
  • 2
  • 9
  • 23

2 Answers2

4

The loop will terminate when readLine() returns null. The only way that this will happen is if standard input it closed. The user can do this by typing Ctrl+D, but that's perhaps too much to ask from a typical user. One easy alternative is to ask them to type "done" when they're done; then your loop should check for that value, rather than null.

Shog9
  • 156,901
  • 35
  • 231
  • 235
Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • Hi Ernest. Thanks for that. I am actually going to be testing it on an automated user input (like from a computer that generates input) so it can't include "done" or CTRL -D at the end. Is there any other way? – Jay Oct 03 '11 at 04:31
  • @Jay:The way you are trying to achieve it, there must be an indication from user that it has finished sending input. Like a terminating char/String or the no. of inputs should be known etc. – Umer Hayat Oct 03 '11 at 05:19
  • I dont know the no. of inputs that the computer will run on my code because its random. Is there no way of that it shuts down automatically once no more input is supplied? – Jay Oct 03 '11 at 05:28
  • 1
    If the program is reading from a pipe -- i.e., `java MyClass < file.txt` -- then it should work correctly as written; standard input should be closed at the end of the file. – Ernest Friedman-Hill Oct 03 '11 at 11:10
  • And also, as I said, you could write the program to stop when a sentinel value (the word "done", for example) is input. That works the same for user input or automated input. – Ernest Friedman-Hill Oct 03 '11 at 11:12
2

What I would look at is Observer Pattern http://en.wikipedia.org/wiki/Observer_pattern and multitreading releated things such as wait() - use this link: How can I set a timeout against a BufferedReader based upon a URLConnection in Java?

and take a look here: http://www.javaspecialists.eu/archive/Issue153.html

Community
  • 1
  • 1
dantuch
  • 9,123
  • 6
  • 45
  • 68
  • I dont know the no. of inputs that the computer will run on my code because its random. Is there no way of that it shuts down automatically once no more input is supplied? – Jay Oct 03 '11 at 05:29
  • You need to set `timeout time` somehow. I don't know how :) – dantuch Oct 03 '11 at 05:44