I don't understand why this program example from a Java book won't compile - Overclock.net - An Overclocking Community

Forum Jump: 

I don't understand why this program example from a Java book won't compile

 
Thread Tools
post #1 of 5 (permalink) Old 10-18-2017, 09:48 PM - Thread Starter
New to Overclock.net
 
CleverFox137's Avatar
 
Join Date: Oct 2017
Posts: 1
Rep: 0
Sorry... I fixed it, it was the brackets. If the moderators wish to delete this thread they can.

This isn't homework or anything, in fact this is a program in the book Java: A beginner's Guide Sixth Edition. I was trying to play around with this piece of code, copied it but it won't even compile... I have JDK 8 installed, and am using Netbeans. The only difference in the code below and from the book is the "package help;" line at the top. Other than that it is the exact same (also except for the comments). The two lines with errors are marked with comments. I checked it more than once and did not find any typing errors.
Code:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package help;

/**
 *
 * @author Nathan
 */
public class Help {

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        // TODO code application logic here
        throws java.io.IOException{ //error here
        char choice;
        
        System.out.println("Help on:");
        System.out.println(" 1. if");
        System.out.println(" 1. switch");
        System.out.println("Choose one: ");
        choice = (char) System.in.read();//another error here
        
        System.out.println("/n");
        
        switch(choice){
            case '1':
                System.out.println("The if:/n");
                System.out.println("if(condition) statement;");
                System.out.println("else statement;");
                break;
            case '2':
                System.out.println("The switch:/n");
                System.out.println("switch(expression){");
                System.out.println("    case constant:");
                System.out.println("    statement sequence");
                System.out.println("    break;");
                System.out.println("    //...");
                System.out.println("}");
                break;
            default:
                System.out.print("Selection not found.");
        }
    }
    }
    
}
CleverFox137 is offline  
Sponsored Links
Advertisement
 
post #2 of 5 (permalink) Old 10-20-2017, 08:03 AM
New to Overclock.net
 
Yogi420's Avatar
 
Join Date: Oct 2017
Posts: 7
Rep: 0
Your line here looks wrong to me so currently you have:
Code:
throws java.io.IOException{ //error here

Then it looks like you're closing the curly brace at end of your class.

Try replacing it like this
Code:
throws java.io.IOException{ 
//error here 
} 

Ensuring you don't put your curly brace on the same line as the comment otherwise it will be commented out
Yogi420 is offline  
post #3 of 5 (permalink) Old 07-02-2018, 04:28 PM
New to Overclock.net
 
Join Date: Jun 2018
Location: Hendersonville, NC
Posts: 100
Rep: 0
Tabbing and spacing doesn't really matter in Java - it is good practice to properly indent your code...

But yes, if you quickly glanced at the code it looks like you have an extra }, but that isn't the case if you count - but there actually are too many { and }s.. But the other issue is when using 'throws' to define the type of exceptions a method can throw, you need to define it with the method.

You can throw an exception inside of that class method, or one can be thrown by a method called from within the method using throw. But throws is reserved for defining what type of exceptions you can expect a method to throw / spit-out...


So, you'd define the function / method and at the end of the line before { you would add throws <EXCEPTIONS>

Then, inside of the method / function, you can throw new Exception( 'args' );


So, the issue with the code you have is you add a { right after the function definition but before the throws definition is added to it. Now I haven't used Java in a few years, but the following should work:

Code:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package help;

/**
 *
 * @author Nathan
 */
public class Help {

    /**
     * 
@param
 args the command line ( variable ) arguments - note: Variable Arguments are when you can have a dynamic number of arguments returned to a reference.. in Lua ... is used and you can then do local _table = { ... } to then iterate through, in JavaScript there is an arguments variable which exists to use, here you're naming it args, as a table of String data-types.
     */
    // Define the function scope, type / data-type to return, name, args and add throws definitions so the compiler knows what exceptions can be expected - and you can create your own for simple error reporting - they're incredibly useful...
    public static void main( String args [ ] ) throws java.io.IOException 
    {
        // Start defining the method / function contents - so here we declare a variable we'll use later and it should contain the data-type char otherwise there will be an issue...
        char choice;
        
        System.out.println("Help on:");
        System.out.println(" 1. if");
        System.out.println(" 1. switch");
        System.out.println("Choose one: ");

        // This error was most likely because throws wasn't defined in the right space and IO can throw the exception above and because it wasn't defined and there is no try / catch it probably gave an unchecked exception error or something along those lines.. I'd also recommend using a loop to accept a value and if it isn't within the range you are looking for you can immediately ask again... Casting, which is what you did with (char) forces the value returned by System.in.read( ) to be converted to a char regardless of data-type but you may also end up with an error here regarding that...
        choice = (char) System.in.read();//another error here
        
        System.out.println("/n");
        
        switch(choice){
            case '1':
                System.out.println("The if:/n");
                System.out.println("if(condition) statement;");
                System.out.println("else statement;");
                break;
            case '2':
                System.out.println("The switch:/n");
                System.out.println("switch(expression){");
                System.out.println("    case constant:");
                System.out.println("    statement sequence");
                System.out.println("    break;");
                System.out.println("    //...");
                System.out.println("}");
                break;
            default:
                System.out.print("Selection not found.");
        }
    }    
}

Last edited by Acecool; 07-02-2018 at 04:32 PM.
Acecool is offline  
Sponsored Links
Advertisement
 
post #4 of 5 (permalink) Old 07-02-2018, 05:30 PM
Overclocker
 
JackCY's Avatar
 
Join Date: Jun 2014
Posts: 9,751
Rep: 330 (Unique: 237)
Works just fine the way you have it once you fix your cosmetic issues.

https://ideone.com/PK16FF

I know it's a necro thread but I checked it and it works.
JackCY is offline  
post #5 of 5 (permalink) Old 08-06-2018, 11:40 PM
New001
 
Join Date: Feb 2016
Posts: 2,668
Quote: Originally Posted by JackCY View Post
Works just fine the way you have it once you fix your cosmetic issues.

https://ideone.com/PK16FF

I know it's a necro thread but I checked it and it works.

wasnt really a cosmetic issue. there was just an extra set of curly brackets that shouldn't have been there, halfway through the method declaration. - or is that considered a cosmetic issue.
spinFX is offline  
Reply

Quick Reply
Message:
Options

Register Now

In order to be able to post messages on the Overclock.net - An Overclocking Community forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.
User Name:
If you do not want to register, fill this field only and the name will be used as user name for your post.
Password
Please enter a password for your user account. Note that passwords are case-sensitive.
Password:
Confirm Password:
Email Address
Please enter a valid email address for yourself.
Email Address:

Log-in



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page


Forum Jump: 

Posting Rules  
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off