-1

I need help with the following code- essentially what I'm trying to do is continuously prompt user for numbers until they enter "Done" to finish, then prompts the user for a file name so that these values can be saved to that file. For example, if the user enters "output.txt", then the program should write the numbers that have been read to "output.txt".

This is what I have so far:

public static void main(String[] args) {

try{
    FileWriter file= new FileWriter("filename.txt");

    Scanner input= new Scanner(System.in);

    boolean done= false;

do{
    System.out.println("Enter a number");
    String value= input.nextLine();
        if (value.equalsIgnoreCase("done")){
            done=true;

            Scanner input1= new Scanner(System.in);
            System.out.println("What is the filename?");
            String filename1= input1.next();
            FileWriter finalFile = new FileWriter(filename1);


          } else {
            try{
                double number= Double.parseDouble(value);

                file.write(number+ "\n");
                file.flush();
              }
              catch (NumberFormatException fnfe) {
              System.out.println("Not valid");
            }

          }
        } while(!done);
          input.close();
          file.close();
          System.out.println("Success");

        }
        catch (IOException ioe){
          System.out.println(ioe.toString());
        }   


}

}       

the code below outputs two files, one text file (filename.txt) and the other that is appropriately named by the user. How can I fix this? There should only be one output.

Any advice would be much appreciated!

Bella
  • 41
  • 3
  • So, if I've read this right, you want to prompt a user for an arbitrary number of numbers until they enter `"done"` and then write those values out to a file? If so, you either, need someway to store the values until the user enters `"done"`, request the filename and then write the numbers OR you need to get the name of the file first – MadProgrammer Feb 11 '18 at 22:11
  • The numbers are being stored in the generically named file "filename.txt". See above comment; ask the name of the file first, rather than have to rename that file afterwards. – Ian Mc Feb 11 '18 at 22:15

2 Answers2

0

You could...

Store the values been entered by the user in some kind of list. Since the number of values been entered is arbitrary, you'll probably need to use something like an ArrayList, as it provides a dynamic size

Scanner input = new Scanner(System.in);
List<Double> numbers = new ArrayList<Double>(25);
boolean done = false;
do {
    System.out.println("Enter a number");
    String value = input.nextLine();
    done = value.equalsIgnoreCase("done");
    if (!done) {
        try {
            double number = Double.parseDouble(value);
            numbers.add(number);
        } catch (NumberFormatException fnfe) {
            System.out.println("Not valid");
        }

    }
} while (!done);

System.out.println("What is the filename?");
String filename1 = input.nextLine();
try (BufferedWriter finalFile = new BufferedWriter(new FileWriter(filename1))) {
    for (double number : numbers) {
        finalFile.write(Double.toString(number));
        finalFile.newLine();
    }
} catch (IOException ex) {
    ex.printStackTrace();
}

Or you could...

If you're unable to use a List of some kind, you will need to prompt the user for the file name first and then write the values out as they entered...

    Scanner input = new Scanner(System.in);

    System.out.println("What is the filename?");
    String filename1 = input.nextLine();
    try (BufferedWriter finalFile = new BufferedWriter(new FileWriter(filename1))) {
        boolean done = false;
        do {
            System.out.println("Enter a number");
            String value = input.nextLine();
            done = value.equalsIgnoreCase("done");
            if (!done) {
                try {
                    double number = Double.parseDouble(value);
                    finalFile.write(Double.toString(number));
                    finalFile.newLine();
                } catch (NumberFormatException fnfe) {
                    System.out.println("Not valid");
                }

            }
        } while (!done);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
MadProgrammer
  • 323,026
  • 21
  • 204
  • 329
0

What your code does: Create a FileWriter for file "filename.txt" and add the numbers entered by the user. When the user enters done in the command line ask him for the filename and create a new FileWriterfor that file, but dont write anything to it. Then close the first FileWriter.

What you want: Query the user for values, store them somehow, ask for the file location, save the values to the file location.

This should do the job:

try (Scanner input = new Scanner(System.in))
{
    List<Double> numbers = new ArrayList<>();

    // Query user for numbers.
    boolean done = false;
    do
    {
        System.out.println("Enter a number: ");
        String value = input.nextLine();

        if (value.equalsIgnoreCase("done"))
        {
            done = true;
        }
        else
        {
            try
            {
                double number = Double.parseDouble(value);
                numbers.add(number);
            }
            catch (NumberFormatException fnfe)
            {
                System.out.println("Not valid");
            }
        }
    }
    while (!done);

    // Prompt the user for the file name. If the user just presses enter, reprompt >:-(
    String fileName;
    do
    {
        System.out.println("Specify a filename: ");
        fileName = input.nextLine();
    }
    while (fileName.isEmpty());

    try (PrintStream ps = new PrintStream(fileName))
    {
        for (Double number : numbers)
        {
            ps.print(number);
            ps.println();
        }
    }
    System.out.println("Success");
}
catch (IOException ioe)
{
    ioe.printStackTrace();
}
sn42
  • 1,930
  • 1
  • 9
  • 26