Grade 11 course, we use Java: Ready to Program.
This simple program is supposed to generate random numbers simulating the rolling of dice everytime it is loaded. This is just an example program to learn the basics of generated data.
I know this is probably a very simple problem, but I am completely new to Java, and our teacher simply does not teach.
This is what I have so far
// The "Chapter6" class.
import java.awt.*;
import hsa.Console;
import java.io.PrintWriter;
import java.io.FileWriter;
public class Chapter6
{
static Console c; // The output console
public static void main (String[] args)
{
c = new Console ();
int die1, die2, roll;
PrintWriter output;
output = new PrintWriter (new FileWriter ("dice"));
for (int count = 1; count <=300;count ++)
{
die1 = (int) (Math.random()*6)+1;
die2 = (int) (Math.random()*6)+1;
roll = die1 + die2;
output.println (roll);
{
output.close();
}
}
When it's run, I get this part highlighted
output = new PrintWriter (new FileWriter ("dice"));
and the error message is:
The constructor "FileWriter" can throw the checked exception "java.io.IOException", so the class creation must be enclosed in a try statement that catches the exception, or else the method must be declared to throw the exception.
Any help would be greatly appreciated.
This simple program is supposed to generate random numbers simulating the rolling of dice everytime it is loaded. This is just an example program to learn the basics of generated data.
I know this is probably a very simple problem, but I am completely new to Java, and our teacher simply does not teach.
This is what I have so far
// The "Chapter6" class.
import java.awt.*;
import hsa.Console;
import java.io.PrintWriter;
import java.io.FileWriter;
public class Chapter6
{
static Console c; // The output console
public static void main (String[] args)
{
c = new Console ();
int die1, die2, roll;
PrintWriter output;
output = new PrintWriter (new FileWriter ("dice"));
for (int count = 1; count <=300;count ++)
{
die1 = (int) (Math.random()*6)+1;
die2 = (int) (Math.random()*6)+1;
roll = die1 + die2;
output.println (roll);
{
output.close();
}
}
When it's run, I get this part highlighted
output = new PrintWriter (new FileWriter ("dice"));
and the error message is:
The constructor "FileWriter" can throw the checked exception "java.io.IOException", so the class creation must be enclosed in a try statement that catches the exception, or else the method must be declared to throw the exception.
Any help would be greatly appreciated.