javainputswitch-statement

Nooby Java programmer confused at output (Constructors)


I am a fairly new Java programmer and I am currently following an online tutorial to improve my skills. I found the following code example on the tutorial which all looks like it should run however I am getting a number of errors when I run the code in Eclipse.

This is my first program using cases however I am pretty sure I have the correct syntax maybe somebody can point out my error? Also I was surprised that the compiler complains about the line "System.out.println ("Are you sure (y - yes, n - no)?");"

I am getting errors on lines 3,4,7,8,10,11,15. Please can somebody tell me why my program will not run?

class Certainty  
{
    System.out.println ("Are you sure (y - yes, n - no)?");
    int ch = System.in.read ();
    switch (ch)
    {
       case 'Y':
       case 'y': System.out.println ("I am sure.");
                break;
       case 'N':
       case 'n': System.out.println ("I am not sure.");
                 break;
       Default : System.out.println ("Incorrect choice.");
    }
}

/* Thank you for all of the helpful responses I'm slowly starting to get my head around Java and I'm really enjoying it and loving how quickly my question was answered you guys are awsome.**/


Solution

  • This is my first program using cases however I am pretty sure I have the correct syntax maybe somebody can point out my error?

    Most of the actual syntax around cases is correct, except that it's default: not Default: (capitalization matters).

    But your class has step-by-step code immediately inside the class. You can't do that. It has to be inside initializers or (more commonly) constructors and/or methods.

    Separately, System.in.read() may throw an IOException, which you have to either declare that your method throws or catch. In the example below, I catch it and just say it happened. Normally you'd do something more useful than that, but it's fine for this kind of quick test.

    import java.io.IOException;  // <=== Tell the compiler we're going to use this class below
    class Certainty  
    {
        public static final void main(String[] args) {
            try {                      // `try` starts a block of code we'll handle (some) exceptions for
                System.out.println ("Are you sure (y - yes, n - no)?");
                int ch = System.in.read ();
                switch (ch)
                {
                   case 'Y':
                   case 'y': System.out.println ("I am sure.");
                            break;
                   case 'N':
                   case 'n': System.out.println ("I am not sure.");
                             break;
                   default : System.out.println ("Incorrect choice.");
                }
            }
            catch (IOException e) {    // <== `catch` says what exceptions we'll handle
                System.out.println("An exception occurred.");
            }
        }
    }
    

    There, I've moved your code into the standard main method used with command-line Java apps, and fixed the Default thing.