Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Help

Options
  • 09-12-2008 11:15pm
    #1
    Registered Users Posts: 18,919 ✭✭✭✭


    This post has been deleted.


Comments

  • Registered Users Posts: 1,119 ✭✭✭Donald-Duck


    I think you ended your main bracket too late, you meant to end it after your if/else I assume? But its actually ended after the first function.

    = is for setting variables, while == is the comparison operator. To compare characters you have to do for example
    if(variable == 'A') for the letter A

    there is no else(condition) theres only
    else {
    }
    and
    if else () {
    }

    Format your closing brackets better - it'll save you a lot of time if you line them up correctly
    class calculator{
     public static void main(String[] args){
    
    char option;
    System.out.print("Please choose one of the following:");
    System.out.print("(A)	Add");
    System.out.print("(B)	Subtract");
    option=keyboard.readChar ();
    
    	if (option=A){	
    		add();
    	}
    
    	else (option=B) { 	
    		subtract();
    	}
    
    
    
    
    static void add(){
    	int number1, number2;
    
    	System.out.println("Please enter two numbers to add:   ");
    	number1 = Keyboard.readInt();
    	number2 = Keyboard.readInt();
    	System.out.println("Your answer is:");
    	System.out.println(number1 + number2);
    }
    
    
    } 
    
    static void subtract(){
    	int number3, number4,
    
    	System.out.println("Please enter two numbers to subtract:   ");
    	number3 = Keyboard.readInt();
    	number4 = Keyboard.readInt();
    	System.out.println("Your answer is:");
    	System.out.println(number3 - number4);
    
    }
    
    }
    

    Theres the brackets realigned in a clearer way - it should help show you were you have too many


  • Closed Accounts Posts: 16,095 ✭✭✭✭omb0wyn5ehpij9


    Are you in IT Tallaght by any chance?! I had that exact assignment in 1st year ;)


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    You forgot to close off your variables.
    int number3, number4,

    That should be a semi-colon instead of a comma.

    Also, use switch cases for menus, not if statements. :)


  • Registered Users Posts: 18,919 ✭✭✭✭Mimikyu


    This post has been deleted.


  • Registered Users Posts: 1,119 ✭✭✭Donald-Duck


    For comparing chars you use the apostraphe key instead of quotes and your main still ends in the wrong place, its ending after your add function instead of before


  • Advertisement
  • Registered Users Posts: 18,919 ✭✭✭✭Mimikyu


    This post has been deleted.


  • Registered Users Posts: 1,119 ✭✭✭Donald-Duck


    class calculator{
     public static void main(String[] args){
    
    char option;
    System.out.print("Please choose one of the following:");
    System.out.print("(A)	Add");
    System.out.print("(B)	Subtract");
    option=keyboard.readChar ();
    
    switch (option) {
    	case "A": add();
    	case "B" subtract();
    }
    
    
    [b] but I'm pretty sure you want it to end here[/b]
    
    	static void add(){
    		int number1, number2;
    
    		System.out.println("Please enter two numbers to add:   ");
    		number1 = Keyboard.readInt();
    		number2 = Keyboard.readInt();
    		System.out.println("Your answer is:");
    		System.out.println(number1 + number2);
    }
    
    } [b] main is ending here[/b]
    
    
    


  • Registered Users Posts: 6,240 ✭✭✭hussey


    switch (option) {
    	case "A": add();
    	case "B" subtract();
    }
    
    [B]should be[/B]
    switch (option) {
    	case 'A': add();
    	case 'B' subtract();
    }
    
    

    The double quotes make the input a string since you are using a char it needs to be single quotes


  • Closed Accounts Posts: 1,827 ✭✭✭Donny5


    Because switch statements don't break after it finds a correct case, you should break after each case.

    Also, putting the i/o in the functions isn't great practice, but it doesn't really matter.

    I presume you want the Sysouts to print to different lines, in which case, just have the one Sysout and pop in \n for a newline.

    Donal


  • Registered Users Posts: 18,919 ✭✭✭✭Mimikyu


    This post has been deleted.


  • Advertisement
  • Registered Users Posts: 1,119 ✭✭✭Donald-Duck


    Loko at your number5 and number6, you made a typo.

    By the way, you can re use number1/number2 in all the functions since the variable scope is only within the function


  • Closed Accounts Posts: 16,095 ✭✭✭✭omb0wyn5ehpij9


    You have a space in when you are declaring your variables. You either should have no space, or have an underscore.
    You should declare your variables as number5 and number6 to stay with the same naming convention you have been using throughout the programme


  • Registered Users Posts: 18,919 ✭✭✭✭Mimikyu


    This post has been deleted.


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    What you can do is use a while loop to take input prior to the switch statement. Use the same variable as the switch statement.

    So basically, you would have prior to the switch statement
    while(option != 4){ // while the input does not equal 4, then run the switch

    // switch case in here

    }

    Otherwise, it will skip past the switch case and end the program :) That way, you will have the menu looping until they select the 4th option. I'd reccommend using numbers over letters for simplicity, as in java you need to use .equals to compare strings instead of == or !=

    Best of luck.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    dlofnep wrote: »
    What you can do is use a while loop to take input prior to the switch statement. Use the same variable as the switch statement.

    So basically, you would have prior to the switch statement



    Otherwise, it will skip past the switch case and end the program :) That way, you will have the menu looping until they select the 4th option. I'd reccommend using numbers over letters for simplicity, as in java you need to use .equals to compare strings instead of == or !=

    Best of luck.
    Or a do while loop?


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    Didn't I just say that?


  • Registered Users Posts: 18,919 ✭✭✭✭Mimikyu


    This post has been deleted.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    dlofnep wrote: »
    Didn't I just say that?
    Not really no, plus <snip - i'm half asleep>

    do
    {
    //switch statement in here
    } while (option.charAt(0) != 'D');

    You are right about numbers though, better choice. Then you can do a range and say wrong option.


  • Registered Users Posts: 18,919 ✭✭✭✭Mimikyu


    This post has been deleted.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Since it numbers now and not strings:
    do{
    switch (option) {
    	case 1: add(); break;
    	case 2: subtract(); break;
    	case 3: multiply(); break;
    	case 4: quit(); break;
            default:  //invalid option. Print to screen
               System.out.println("Invalid Choice");
    
    	}
    	while (option < 1 && option > 3);
    
    }
    

    Edit - the quit() will have to exit the program or you will get the default case saying invalid choice again.


  • Advertisement
  • Registered Users Posts: 18,919 ✭✭✭✭Mimikyu


    This post has been deleted.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Oops closing brace wrong place. Half asleep
    do{
    switch (option) {
    	case 1: add(); break;
    	case 2: subtract(); break;
    	case 3: multiply(); break;
    	case 4: quit(); break;
            default:  //invalid option. Print to screen
               System.out.println("Invalid Choice");
    
    	}
    }	while (option < 1 && option > 3);
    


  • Registered Users Posts: 18,919 ✭✭✭✭Mimikyu


    This post has been deleted.


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    Webmonkey wrote: »
    Not really no, plus <snip - i'm half asleep>

    do
    {
    //switch statement in here
    } while (option.charAt(0) != 'D');

    You are right about numbers though, better choice. Then you can do a range and say wrong option.

    Yeah, do while would probably be cleaner.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Rather then answering all the mistakes for you, it would be better for you to just understand how the error messages work.

    So for example
    D:\College\Java Programming\Java Programming\Assignment 2\calculator.java:40: while expected
    }
     ^
    

    the 40 is the line number in your code where the compiler realised that you have made a mistake and it can't progress.

    The ^ character points to the item in the line that is causing the issue. In this case it is just after a bracket.

    So you would look for the opening bracket to see what it needed. In this case the error message explains: "while expected". If you trace back to the opening bracket for that then you will see it is "do {". So you need to finish the loop with a while to tell it at what point to exit the loop.

    Likewise for the other errors. If the error message doesn't make sense then it is more likely to proceeding line causing the error. In your case the first error is causing all the other errors.

    If your code is working but misbehaving then you can use a debugger. That is probably advanced for you. So another option is to put print statements in your code for example.
    do{
    System.out.println("Entered do. Option was: " + option );
    switch (option) {
    	case 1: 
                 System.out.println("Selected 1. Entering Add");
                 add(); 
                 System.out.println("Exiting Add");
                 break;
    

    So in this example if "Exiting Add" never appeared then you know there is a problem with the line before it.

    Once you have finished debugging you can remove the print lines.


  • Closed Accounts Posts: 1,827 ✭✭✭Donny5


    do{
    
    //put your i/o here
    
    switch (option) {
    	case 1: add(); break;
    	case 2: subtract(); break;
    	case 3: multiply(); break;
    	case 4: quit(); break;
            default:  //invalid option. Print to screen
               System.out.println("Invalid Choice");
    
    	}
    }	while (option < 1 && option > 3);
    

    Try (option >= 1 && option <= 3).


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Donny5 wrote: »
    do{
    
    //put your i/o here
    
    switch (option) {
    	case 1: add(); break;
    	case 2: subtract(); break;
    	case 3: multiply(); break;
    	case 4: quit(); break;
            default:  //invalid option. Print to screen
               System.out.println("Invalid Choice");
    
    	}
    }	while (option < 1 && option > 3);
    

    Try (option >= 1 && option <= 3).
    Correct, got them mixed up lastnight.


Advertisement