0

I have a requirement where I will have to read inputs from console not from file

The input format is as below.I understand I can use any character to exit .But i cannot give any other input than this.Can i write some thing like wait for a specific amount of time and if there is no input break the loop or else what else can be done to read input whose length we never know before hand.

Hello,Agnes
Minion,Bedo
Vector,Shrink Ray

This following logic will make the readline wait forever for the input and wouldn't help because every enter I press will again be another input character and it never ends.

I neither wanna give any exit character to determine end of input like "exit". How to handle this??

ArrayList<String> emp=new ArrayList<String>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
h=br.readLine();
while((h=br.readLine()) != null)
{           
   emp.add(h);         
}
M A
  • 71,713
  • 13
  • 134
  • 174
Sukanya Moorthy
  • 109
  • 1
  • 2
  • 10

3 Answers3

0

You could add a thread that requests the users input, and then a Timer that's started over on each new input. When the timer expires, it shuts the user input thread down.

Gann14
  • 1,128
  • 1
  • 8
  • 12
0
    Scanner reader = new Scanner(System.in);
    System.out.println("Enter something: ");
    String s=reader.nextLine();
    System.out.println("You enetered: " + s);

Which results in:

Enter something: 
Mike,Elofson
You enetered: Mike,Elofson

Generally speaking, and from my experience, scanners are easier and more commonly used for input.

Mike Elofson
  • 2,017
  • 1
  • 10
  • 16
  • I personally use BufferedReader for reading input rather than the Scanner every time I need it, because I personally had trouble with the Scanner but the BR always worked as intended. – EpicPandaForce Jun 23 '14 at 18:27
0

How about a "" input(namely press Enter directly), or two/three consecutive "" inputs.

I don't think waiting for some time is a good idea. Anyway, you need some input to indicate the end.

user3761116
  • 152
  • 1
  • 4