0

my intent for this program was to create a simple program that takes input from the terminal line by line, and stores it into a file in the same fashion, ending when the user inputs "end".

I'm confused about why... 1) the while loop does not end when the user inputs "end" 2) the data is not written to the file (I've checked, it doesn't for whatever reason)

Edit: the answer has been found already! For those just arriving, my while condition should have been !temp.equals("end")

public static void main(String[] args) throws IOException{

    Scanner in = new Scanner(System.in);

    System.out.println("Type in data and it will be written to a file");
    System.out.println("filename = blah.txt");

    String temp, filename="blah.txt";

    PrintWriter outfile = new PrintWriter(new FileWriter(filename));

    System.out.println("enter next line... \"end\" to exit");
    temp=in.nextLine();
    while(temp!="end"){
        outfile.println(temp);
        System.out.println("enter next line... \"end\" to exit");
        temp=in.nextLine();
    }

    outfile.close();
}
Brent Allard
  • 386
  • 5
  • 20

3 Answers3

2

Use equals instead of

temp!="end" // temp.equals("end")

Flush the output stream before closing because by default PrintWriter object is created without automatic line flushing. Hence

outfile.flush()

or when you are creating PrintWriter object then pass true as second argument like this

PrintWriter outfile = new PrintWriter(new FileWriter(filename), true);
sol4me
  • 15,233
  • 5
  • 34
  • 34
  • Closing the stream also flushes it. Also, autoflushing can have performance effects, although in this case it's not important. – Dave Newton Dec 13 '14 at 16:12
  • I agree that autoflushing has additional cost that's why i mentioned about flush method as well. Thanks – sol4me Dec 13 '14 at 16:22
  • people have mentioned that I should use temp.equals("end"), but why doesn't my method work? It would have been my go-to method in c++, which was my first language. From what I understand, Java was built out of C++. – Brent Allard Dec 13 '14 at 16:34
  • == tests for reference equality but you want to test for value equality(actual content of the string) that why use equals() – sol4me Dec 13 '14 at 16:39
  • @BrentAllard C string comparison would use `strcmp`; C++ happens to overload `==`, but the semantics are different than `strcmp`. Java (deliberately) does not support operator overloading. – Dave Newton Dec 13 '14 at 17:30
  • Thanks, Dave and sol! – Brent Allard Dec 15 '14 at 04:42
0

when checking if 2 strings are equal or not use equals method i.e temp.equals("end") this is the reason your while loop is not ending

Vihar
  • 3,626
  • 2
  • 24
  • 47
0

The while-loop does not end, because you are comparing reference types to each other, not the values of the Strings. This means, that

while( temp != "end" )

Checks if the reference of the String stored in temp equals the reference of "end". Thus the comparison looks somewhat like

java.lang.String@549f9afb != java.lang.String@583dad65

which always evaluates to true.

In order to compare the values of two String objects with each other you need to call equals on one String. So that in your case one has to write:

while ( temp.equals( "end" ) )

The PrintWriter does not write the received output immediately, but stores it until it is flushed. By activating autoFlush, the PrintWriter is flushed after every call to println. It can be activated setting the autoFlush Parameter of the constructor.

PrintWriter outfile = 
    new PrintWriter( new FileWriter( filename ), true );
bbastu
  • 508
  • 3
  • 10