1

I'm still new to programming and I know that the code I have below is probably a mess but, how do I properly use a "throws clause"? I'm supposed to use it for an assignment(add a throws clause to the main method header) but when I try to run it,

I get the error: Exception in thread "main" java.lang.Error: Unresolved compilation problems: The target type of this expression must be a functional interface Syntax error on token "throws", delete this token.

What am I doing wrong? I'm also supposed to print mean and standard deviation to an output file, using a three decimal format, however, I don't understand how to go about that either.

import java.util.Random;
import java.util.Scanner;
import java.io.*;

public class DiceSimulation {
    public static void main(String[] args) {
        final int NUMBER = 10000; // the number of times to roll the dice

        Random generator = new Random();
        File myFile = new File("Results");
        Scanner inputFile = new Scanner(myFile);
        PrintWriter outputFile = new PrintWriter("Results")throws::IOException;
        outputFile.println("FileChooser.txt");
        outputFile.println("Filereader.txt");
        outputFile.println("FileWriter.txt");
        outputFile.close();
        Scanner keyboard = new Scanner(System.in);

        int die1Value; // number of spots on the first die
        int die2Value; // number of spots on the second die
        int count = 0; // number of times the dice were rolled
        int snakeEyes = 0; // number of times snake eyes is rolled
        int twos = 0; // number of times double two is rolled
        int threes = 0; // number of times double three is rolled
        int fours = 0; // number of times double four is rolled
        int fives = 0; // number of times double five is rolled
        int sixes = 0; // number of times double six is rolled

        do {
            new Random();
            System.out.println("You rolled: ");
            die1Value = keyboard.nextInt();

            new Random();
            System.out.println("You rolled: ");
            die2Value = keyboard.nextInt();

        } while (count <= 0);

        if (die1Value == 1)
            ;
        {
            ++snakeEyes;
        }
        if (die1Value == 2)
            ;
        {
            ++twos;
        }
        if (die1Value == 3)
            ;
        {
            ++threes;
        }
        if (die1Value == 4)
            ;
        {
            ++fours;
        }
        if (die1Value == 5)
            ;
        {
            ++fives;
        }
        if (die1Value == 6)
            ;
        {
            ++sixes;
        }
        ++count;
        {

            System.out.println("You rolled snake eyes " + snakeEyes
                    + " out of " + count + " rolls.");
            System.out.println("You rolled double twos " + twos + " out of "
                    + count + " rolls.");
            System.out.println("You rolled double threes " + threes
                    + " out of " + count + " rolls.");
            System.out.println("You rolled double fours " + fours + " out of "
                    + count + " rolls.");
            System.out.println("You rolled double fives " + fives + " out of "
                    + count + " rolls.");
            System.out.println("You rolled double sixes " + sixes + " out of "
                    + count + " rolls.");
        }
    }
}

I would really appreciate assistance, thank you in advance!

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • What do you think _add a throws clause to the main method header_ means? And have you looked at what a `throws` clause is? – Sotirios Delimanolis Oct 12 '14 at 02:55
  • Please remove the apologies at the end of your question. They are not relevant. – Sotirios Delimanolis Oct 12 '14 at 02:55
  • It just dawned on me after reading your comment... I feel so dense lol. Thank you though! – Joseph Shire Oct 12 '14 at 02:56
  • 1
    This may seem like I'm being a jerk, but read this:http://docs.oracle.com/javase/tutorial/essential/exceptions/ You need a fully fleshed-out discussion of exception handling, not a quick answer. – Jon Kiparsky Oct 12 '14 at 02:59
  • 1
    What is with your `if`s? They have semi-colons at the end and why do they precede a block-statement??? – Mr. Polywhirl Oct 12 '14 at 03:00
  • I'll take a look at those links. Thank you all very much – Joseph Shire Oct 12 '14 at 03:04
  • @Sotirios - keep in mind that OP is a beginner and Java does not have header files nor are *method headers* common in the Java venacular. So its easy to understand why he could become confused due to that error message. – jww Oct 12 '14 at 04:07

2 Answers2

2

Try this:

PrintWriter outputFile = new PrintWriter("Results");

You don't have to state that a constructor (or any other method for that matter) throws an exception when calling the constructor (or method). We use the throws keyword in the method declaration, not when actually invoking it. After fixing that, Java will complain that an exception is thrown, You can either:

  1. Declare that the main() method throws IOException
  2. Or surround the offending line with a try/catch statement
Óscar López
  • 232,561
  • 37
  • 312
  • 386
1

Change your first line to:

public static void main(String[] args) throws IOException {
Jason D
  • 8,023
  • 10
  • 33
  • 39