简体   繁体   中英

What's wrong with this java program that's supposed to output the exact input?

This program is supposed to accept information (through file redirection) and is supposed to output exactly what it is given, including spaces and NOT an extra line at the end. It's supposed to be exactly the same without any differences whatsoever. My professor says that it is wrong.

import java.io.*;
public class driver_proj0{
public static void main(String[] args){
    BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
    String lineInput = "";
    try {
        lineInput = f.readLine();
    }
    catch (IOException e){
        e.printStackTrace();
    }
    while (lineInput != null){
            System.out.print(lineInput); // I just want to print it without a new line out for now
        try {
            lineInput = f.readLine();
        }
        catch (IOException e){
            e.printStackTrace();
        }
        if (lineInput != null){
            System.out.println();
        }
    } // end while
 } //end main
}// end class

" including spaces and NOT an extra line at the end ". If this is the requirement, then shouldn't you comment/remove the line

if (lineInput != null){
        System.out.println();
    }

This is introducing newLine. Your program is returning the output which is same as your input, but line formatting is somemthing that is not expected??.

package eu.duernau.stackoverflow;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Sov33354885 {

    public static void main(String[] a) throws IOException {
        BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
        String lineInput = f.readLine();
        while (lineInput != null) {
            System.out.println(lineInput);
            lineInput = f.readLine();
        }
    }
}

produces for example:

say hello with blanks
say hello with blanks
say anything else
say anything else
test
test
exit does not work, because not implemented.            three tabs
exit does not work, because not implemented.            three tabs

You can't get

say anything elsesayanything else

because there is a linebreak in your INPUT already. Otherwise readLine() would not work. Javadoc for readLine():

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\\n'), a carriage return ('\\r'), or a carriage return followed immediately by a linefeed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM