2

I am entering lines from input (IDE eclipse Luna) on pressing enter the cursor keeps moving down but no output shows up. In tried second method but i have to press enter twice to print output how can i fix both errors . Why in second method as soon as it detects blank line it doesn't print why i have to press enter twice And what commands goes when we press enter in console

FIRST METHOD

private static String para = null;
public static void main(String[] args) throws IOException {
    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(System.in));
    String line;
    while ((line = bufferedReader.readLine()) != null) {
        para = para + line;
    }
    bufferedReader.close();
    System.out.println(para);
}

SECOND METHOD

String line;
    while (!(line = br.readLine()).equals("")) {
        s1 = line.split(" ");
        for (int j = 1; j < s1.length; j++) {
            int m = Integer.parseInt(s1[j]);
            edges[Integer.parseInt(s1[0]) - 1].add(vertices[m - 1]);
        }

    }
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
JSONParser
  • 1,112
  • 2
  • 15
  • 30
  • thanks , but , I want to read lines from console convert them into single line do some operations and then print a result i don't want to print lines which i read , i want on putting the input in console when i press enter it should return that single result so is there any solution for this in bufferreader? – JSONParser Sep 16 '15 at 11:50

3 Answers3

0

When you press the entre key the bufferedReader object receive an empty string so it's not null.

To end the input should look for a key such as this :

private static String para = null;

public static void main(String[] args) throws IOException {
    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(System.in));
    String line;
    while (!"end".equals((line = bufferedReader.readLine()))) {
        para = para + line;
    }
    bufferedReader.close();
    System.out.println(para);
}

here I choose 'end' as a ending sequence but you can choose anything such as empty string wich is the result for pressing enter on an empty line.

Jib'z
  • 953
  • 1
  • 12
  • 23
0

no output shows up why??

because you are not printing any thing inside loop

while (!(line = bufferedReader.readLine()).equals("")) // read until enter without inputting anything is pressed.

private static String para = ""; // initialize your para with empty string(i.e "") not with null otherwise null will be added to your para string.
public static void main(String[] args) throws IOException {
    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(System.in));
    String line=bufferedReader.readLine();  
    do{
         System.out.println(line);  // print what you input
         para = para + line;
    }
    while (!(line = bufferedReader.readLine()).equals("")); 
   bufferedReader.close();
    System.out.println(para);
}
Rustam
  • 6,485
  • 1
  • 25
  • 25
0

Method1 is not going to end ever as below readLine() will never returns null, it will always return empty String while reading from console input.

while ((line = bufferedReader.readLine()) != null) { para = para + line; }

Remember on pressing enter java considers it as newLine() command, which surely not null, so your program goes on with empty string and never terminates from the while loop.

I would suggest to have some meaning full check to terminate while loop. You can try Scanner instead of BufferedReader as it provides lot of utility methods.

In Scanner you have methods like hasNext(), hasNextLine() etc which tells whether you have further input or not. Similarly methods like nextLine(), nextInt(), nextDouble() are there to get specific converted values from the command line or any other stream.

Method2 is doing lot of operations which were not part of the Method1, can you please share full runnable code for the same. I doubt on the logic again for terminating the loop.

So I would suggest to use more specific check to terminate your loop while iterating through some data.

  • Whereas if you are reading data from some file, your first method still valid as once file read is done, it will return null to method `readLine()` in the end. – Sandeep Mandori Sep 16 '15 at 11:37
  • thanks , but , I want to read lines from console convert them into single line do some operations and then print a result i don't want to print lines which i read , i want on putting the input in console when i press enter it should return that single result so is there any solution for this in bufferreader? – JSONParser Sep 16 '15 at 11:46
  • Sorry but can you please be more specific, e.g. step by step and with an example, i would try to give you a solution. – Sandeep Mandori Sep 16 '15 at 12:06
  • question is :these are lines find the largest palindrome sentence, – JSONParser Sep 16 '15 at 12:11
  • My question is how you wanted to tell Java that this is new line which would be appended to previous line and now you wanted to print everything to the comman line? There must be some logic to come out of the loop. You can type any number of character and on pressing say q you can exit the loop `line.equals('q')` will terminate you loop and rest all remains same. – Sandeep Mandori Sep 16 '15 at 12:11
  • Ok, so you wanted to enter multiple sentences, among which you wanted to find which sentence is longer palindrome, right? – Sandeep Mandori Sep 16 '15 at 12:14
  • question is :these are lines find the largest palindrome sentence, Line 1 : there was a sheep . line 2 : sheep is fat line 3: there ana there . I want to read these lines convert them to single line perform various algorithms and display the result. so i write these lines in console and press enter key nothing happens the cursor goes to the next line and further no termination . I want on pressing enter final result should get display – JSONParser Sep 16 '15 at 12:19
  • `BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(System.in)); StringBuilder data = new StringBuilder(); String line; while (!"".equals((line = bufferedReader.readLine()))) { data.append(line); } bufferedReader.close(); System.out.println(data.toString());` – Sandeep Mandori Sep 16 '15 at 13:15
  • Above code would accept multiple lines from command line, and once user supplies empty value, it will print the entire entered values as a String. Instead using a `String` try to use `StringBuilder` for string manipulation. I hope it helps. Here we just changed the condition of the while loop, try to put this code under main() and see how it behaves. – Sandeep Mandori Sep 16 '15 at 13:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89822/discussion-between-sesy1-and-sandeep-mandori). – JSONParser Sep 16 '15 at 14:10
  • The assertion that readLine() will never return null is not true. Standard input can be terminated with Ctrl-D in Unix/Linux, Ctrl-Z in Windows. – VGR Sep 17 '15 at 22:37