0

I'm trying to create a file by getting the user input. For example, it the user enter number of sets and write that to a file and writing two more user input. But it doesn't show any files. Also for additional questions how do I record a random numbers in the file?

My purpose of the code is that when the user choose 2 sets and the size of sets that user wants. For example, if the user pick 4 and 6, it randomly generate 4 numbers and 6 numbers seperately. I know how to use random but just don't know how to implement this to writing a file.

These are the sample output that I should get

2

5

23 789 900 4000 4

10

10002 50930 2345 30 20 1 0 45 5 8000

From the code:

public static void main(String[] args) throws IOException {
Random rnd = new Random();
File file = new File("C://Users//Hyeon Jin Ryu//nums.txt");
FileWriter w = new FileWriter(file);
Scanner in = new Scanner(System.in);



System.out.println("Please enter numbers of sets: ");
int a = in.nextInt();


System.out.println("Please enter the size of two each sets");
int firstSet = in.nextInt();
int secondSet = in.nextInt();
int t = rnd.nextInt(firstSet);
int k = rnd.nextInt(secondSet);


w.write(a);
w.write(t);
w.write(k);
in.close();
w.flush();
w.close();

}

Jason.R
  • 1
  • 1
  • 5
  • for your improvement, you can use the same scanner for your entire code. – Omri Attiya Sep 02 '19 at 19:50
  • Possible duplicate of [FileWriter is not writing in to a file](https://stackoverflow.com/questions/30525437/filewriter-is-not-writing-in-to-a-file) – Josie Thompson Sep 02 '19 at 21:24
  • You might consider using `PrintWriter` instead: https://stackoverflow.com/questions/2885173/how-do-i-create-a-file-and-write-to-it-in-java – Josie Thompson Sep 02 '19 at 21:25
  • I've figure how to create files but I'm getting weird letter output in the txt file. I've updated my code – Jason.R Sep 03 '19 at 00:38

1 Answers1

0

The code you posted does not output anything close to the sample output you provided. You should really pay some attention to what the methods you're invoking actually do.

  1. You are asking the user for the amount of sets but only scanning for 2 inputs, which are hardcoded. So the first Scanner input is completely redundant.
  2. The reason you're getting weird characters is because FileWriter.write(int) writes one single character as stated in the documentation. Otherwise you should be using strings.

Writes a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.

You need to parse the integer to a string with w.write(String.valueOf(t)) but it still wouldn't output more than one number because you need to loop your code first.

  1. The following code does not generate X amount of numbers but just gets a number firstSet from the user and sets it as the highest random number it will generate, which is also shown in the docs or any IDE for that matter:

    int firstSet = in.nextInt(); // if user enters 6
    int t = rnd.nextInt(firstSet); // generate random number from 0-6
    

So even if you did parse the a, t and k values then with the same inputs you provided (2, 5, 10) it would:

  • Ask for 2 sets regardless of the user input
  • Generate a random number between 0-5
  • Generate a random number between 0-10
  • Write them into a file with no spacing or line breaks, so you would end up with something like 245.

You want to ask the user for the amount of sets, store it as a variable and create a loop which runs as many times as the value in the variable and generates X amount of sets with Y amount of random numbers. In case of 2 sets with sizes 5 and 10 your code would need to loop 5 and 10 times respectively to generate 5 and 10 random numbers, and repeat the entire process 2 times.

The following code produces the result you're describing. Note how rnd.nextInt() is bound by 2500, how the 2nd for loop runs as many times as your user-provided set size and then runs everything again in a nested for loop for the provided amount of sets.

It also uses try-with-resources for FileWriter for handle the exception in the code rather than passing it back to JVM to just crash, and secondly for autoclosing the FileWriter stream.

public static void main(String[] args) {
    Random rnd = new Random();
    File file = new File("nums.txt");

    try (FileWriter w = new FileWriter(file)) {
        Scanner in = new Scanner(System.in);
        StringBuilder setResult = new StringBuilder();

        System.out.println("Please enter the number of sets: ");
        int n = in.nextInt();

        System.out.println("Please enter the size of each set: ");
        for (int j = 0; j < n; j++) {
            int setSize = in.nextInt();

            for (int k = 0; k < setSize; k++) {
                int randomNum = rnd.nextInt(2500);
                setResult.append(randomNum).append(" ");
            }
            setResult.append("\n");
        }
        w.write(setResult.toString());
        in.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

The generated file writes just fine and contains:

699 494 1611 1521 2042 
2478 500 177 1602 348 231 1191 
842 421 93 1229 1804 802 1845 2245 836 
cardouken
  • 120
  • 7