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.
- 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.
- 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.
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