Search code examples
javaexceptionfilereaderfilewriter

Need some Understanding for filereader or filewriter


need some understanding here on what my objective is in this assignment. I'm not looking for anyone to write my code for me. I'm still a noob in coding, and don't quite understand the lingo as good. However, I will mention what I thought of so far and hopefully I'm right.

The assignment:

"Write a program that reads strings from the user and writes them to an output file called userStrings.txt. Stop processing when the user enters the string "DONE". Do not write the sentinel string ("DONE") to the output file."

I can't copy/paste pics yet since I'm also new to Stack Overflow.

The results however show this in the IDE's terminal:

A string.

Another string.

Yet more text...

DONE

Then right next to that pic is showing text but in notepad:

A string.

Another string.

Yet more text...

So my conflictions lie in "Write a program that reads strings from the user.." which made me think that I have to write a scanner that reads lines from a user input on the IDE terminal and the program stores it into userStrings.txt.

That seems excessively hard from my point of view compared to an assignment that was 30 points dealing with inheritance and classes; and this only being worth 15 points seems to be asking for a lot.

After I studied and a little bit and search good'o google I found that people mainly program either filewriter (which writes a directly into notepad) or a filereader (that reads a current file in notepad).

So which one is it? Scanner + filewriter? Just a plain file writer or reader?

Literally losing my mind lol. Also any brief elaboration is helpful too, in noobish please.

Appreciate the help!


Solution

  • Sounds like you've got the right answer in using the Scanner class. I'm sure FileWriter would work to write to file, but there's a class PrintWriter which is basically the output version of Scanner. Both Scanner and PrintWriter have methods that are pretty easy to understand, so I'd go with those two.

    Once you have those set up, it's shouldn't be too difficult to read in a string from Scanner, pass it PrintWriter, and then write that to a file.

    Good luck on the homework! :)