Search code examples
javatry-catchjava.util.scannerfilewriter

Issues with storing user input in txt.file


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!


Solution

  • 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();
        }