0

This is what I have to do:

Create a new copy of the TestFileWriter program called WriterDemo that takes input from the user and writes it into the output file. The program should continue writing lines (a loop may help) until the user supplies an empty line (no text) as their input. Hint: a while loop that has a termination condition that depends on the input string from the user is a good place to start...

The program should be accessed from the terminal and I can't figure out where to put the while loop without ruining the program. The code below is the unmodified version of TestFileWriter. I don't need the full code of WriterDemo, but just some advice on how to use it. An help is greatly appreciated.

import java.io.FileReader;

import java.io.FileWriter;

public class WriterDemo {
public static void main(String args[]){;

FileWriter fout;
    FileReader fin;
    String str;
    int k;
    if(args.length==0){
        System.out.println("Use an argument in the command line");
        System.exit(0);
    }
    try{
        fout = new FileWriter("WrittingProbe.txt");
        for(int i=0; i<args.length; i++){
            fout.write(args[i]);
            fout.write(' ');
        }
        fout.close();

        fin= new FileReader("WrittingProbe.txt");
        System.out.println("The file content is:");
        while((k=fin.read()) !=-1) 
        System.out.println((char)k);
        System.out.println();
        fin.close();

        fout = new FileWriter("WrittingProbe.txt", true);
        str="\nAdded Text\n";
        fout.write(str);
        fout.close();

        fin = new FileReader("WrittingProbe.txt");
        System.out.println("\nNow the file content is:");
        while((k=fin.read()) != -1)
            System.out.print((char)k);
            System.out.println();
            fin.close();

    }
    catch(Exception e){
        System.out.println("Exception: " + e);
    }

}
}
8
  • How are you getting user input? Scanner?
    – Yoland Gao
    Feb 28, 2015 at 15:53
  • I get the user imput from the terminal. Example: java TestFileWriter text_to_be_written_in_file Feb 28, 2015 at 16:00
  • Wait, if you getting arguments from the terminal, how do you get the other lines?
    – Yoland Gao
    Feb 28, 2015 at 16:12
  • That's what I am trying to figure out right now. I don't really understand the problem. Feb 28, 2015 at 16:13
  • The terminal only provides input one time.
    – Yoland Gao
    Feb 28, 2015 at 16:15

2 Answers 2

0
 try (FileWriter fileWriter = new FileWriter("G:\\test.txt")) {
            Scanner scn = new Scanner(System.in);

            while (true) {
                String string = scn.nextLine();

                if (string.equals("0")) {
                    break;
                } else {
                    fileWriter.write(string+"\n");
                }
            }
        }
1
  • this will loop through and take input till entering 0(zero) as input to come out from loop otherwise infinitely keep writing into file
    – TechnoCrat
    Feb 28, 2015 at 16:49
0
    Scanner scn = new Scanner(System.in);
    ArrayList<String> list = new ArrayList<String>();

    list.add(scn.nextLine());
    for(;!list.get(list.size()-1).equals("");){ //Loops until the last input is a blank line
        list.add(scn.nextLine());

    //Or, you can do it here, as you go, if you want
    }
    //Or here, all at once, using the list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.