Results 1 to 8 of 8
  1. #1
    PhilK is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default What am I doing wrong?

    Ok this a homework. I tried looking in books, asking the prof, no luck. and I am stuck. New to this and it seems too confusing for me to continue so I just want to pass the course. I know this is simple to all of you but not if you are starting. The examples in the books are not very helpful. May I get help of why the syntax error at the end and to complete the program?

    // Arithmetic.java - This program performs arithmetic, ( +. -, *. /, % ) on two numbers
    // Input: Interactive.
    // Output: Result of arithmetic operation

    import javax.swing.*;

    public class Arithmetic
    {
    public static void main(String args[])
    {
    double numberOne, numberTwo;
    String numberOneString, numberTwoString;
    String operation;
    double result;

    numberOneString = JOptionPane.showInputDialog("Enter the first number: ");
    numberOne = Double.parseDouble(numberOneString);
    numberTwoString = JOptionPane.showInputDialog("Enter the second number: ");
    numberTwo = Double.parseDouble(numberTwoString);
    operation = JOptionPane.showInputDialog("Enter an operator (+.-.*,/,%): ");

    // Call performOperation method here

    result = performOperation(numberOne, numberTwo, operation);
    System.out.format("%.2f",numberOne);
    System.out.print(" " + operation + " ");
    System.out.format("%.2f", numberTwo);
    System.out.print(" = ");
    System.out.format("%.2f", result);

    System.exit(0);

    } // End of main() method.

    public static double performOperation(double numberOne, double numberTwo, String operation)

    // Write performOperation method here.

    {


    double result;

    if (operation .equals("+"))
    result = numberOne + numberTwo;
    else if (operation .equals("-"))
    result = numberOne - numberTwo;
    else if (operation .equals("/"))
    result = numberOne / numberTwo;
    else if (operation .equals("%"))
    result = numberOne % numberTwo;

    return double result;

    System.out.println("The result is " + result);


    } // End of

    } // End of Arithmetic class.

    THE ERROR CODE

    Arithmetic.java:53: variable result might not have been initialized
    return result;
    ^
    Arithmetic.java:55: unreachable statement
    System.out.println("The result is " + result);
    ^
    Arithmetic.java:59: missing return statement
    } // End of
    ^
    3 errors

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: What am I doing wrong?

    What if 'operation' has a nonsensical value like "foo"? None of your if-tests are true and variable 'result' doesn't get an initial value; that's what your compiler complains about. After returning variable 'result' your method stops executing so the print statement will never be exected and your compiler complains again.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    PhilK is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: What am I doing wrong?

    Quote Originally Posted by JosAH View Post
    What if 'operation' has a nonsensical value like "foo"? None of your if-tests are true and variable 'result' doesn't get an initial value; that's what your compiler complains about. After returning variable 'result' your method stops executing so the print statement will never be exected and your compiler complains again.

    kind regards,

    Jos
    Thanks .. Your part of "foo" makes sense, but the rest is not ... Since I ll for this not type foo but just one of the 4 operations symbols, I just don't see why it does not execute .. that is my problem, I don't have examples for it to guide me through this "maze"

    Again thanks ...

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: What am I doing wrong?

    Quote Originally Posted by PhilK View Post
    Thanks .. Your part of "foo" makes sense, but the rest is not ... Since I ll for this not type foo but just one of the 4 operations symbols, I just don't see why it does not execute .. that is my problem, I don't have examples for it to guide me through this "maze"
    Maybe you know that you're only going to type one of +, -, * or / but the compiler doesn't believe you (or anybody else for that matter). Variable 'result' can be uninitialized as far as the compiler is concered; the remedy is simple: initialize the variable when you define it: double result= 0;

    Oh, and fix the other errors I mentioned and compile often.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    PhilK is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: What am I doing wrong?

    Quote Originally Posted by JosAH View Post
    Maybe you know that you're only going to type one of +, -, * or / but the compiler doesn't believe you (or anybody else for that matter). Variable 'result' can be uninitialized as far as the compiler is concered; the remedy is simple: initialize the variable when you define it: double result= 0;

    Oh, and fix the other errors I mentioned and compile often.

    kind regards,

    Jos

    Thanks Jos .. defining the variable to 0 was my big mistake .. something brought up in class but did not sink in .. But you have saved an old person from an evening of stress and worry ... Thanks Thanks and Thanks.

    You should come over and teach this class

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: What am I doing wrong?

    Quote Originally Posted by PhilK View Post
    Thanks Jos .. defining the variable to 0 was my big mistake .. something brought up in class but did not sink in .. But you have saved an old person from an evening of stress and worry ... Thanks Thanks and Thanks.

    You should come over and teach this class
    You're welcome of course; does it run now? Is it 'industrial strength'? i.e. what if a totally idiotic user (like me) types foo bar baz for the two numbers and the operator. Can your program handle it 'gracefully' or does it raise en exception, dumps its stack and die? Remember: if a use can do something wrong s/he will do something wrong (or worse).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    PhilK is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: What am I doing wrong?

    Quote Originally Posted by JosAH View Post
    You're welcome of course; does it run now? Is it 'industrial strength'? i.e. what if a totally idiotic user (like me) types foo bar baz for the two numbers and the operator. Can your program handle it 'gracefully' or does it raise en exception, dumps its stack and die? Remember: if a use can do something wrong s/he will do something wrong (or worse).

    kind regards,

    Jos


    Jos ... I realize there is lot to do to make sure that a dodo like me when imputing something different it will "dump it's stack and die". I know programing is not an easy thing to do. It takes a lot of thought and writing of code.

    The program runs well, and as a homework, I was just to use those 5 operations. I am to turn in my homework and prove that it works. which it does with your help..

    I took this course to keep busy after retiring, and was hoping that the course would be a lot more hands on but it required trying to write programs that one had to figure out away from class. Lurking in this form I now know that far more knowledgeable persons needed help.

    Again Thanks.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: What am I doing wrong?

    Quote Originally Posted by PhilK View Post
    Jos ... I realize there is lot to do to make sure that a dodo like me when imputing something different it will "dump it's stack and die". I know programing is not an easy thing to do. It takes a lot of thought and writing of code.

    The program runs well, and as a homework, I was just to use those 5 operations. I am to turn in my homework and prove that it works. which it does with your help..

    I took this course to keep busy after retiring, and was hoping that the course would be a lot more hands on but it required trying to write programs that one had to figure out away from class. Lurking in this form I now know that far more knowledgeable persons needed help.

    Again Thanks.
    No need to worry, you're doing fine. Maybe you can get some bonus points if your program refuses to do a division or modulo operation when the denominator is zero? I'd give you bonus point already for separating the actual operator execution in a separate method; most beginners continue 'knitting' all their code in a huuuuuge main method ;-) Keep up the good work.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. what is wrong?
    By pinguxxx in forum Advanced Java
    Replies: 3
    Last Post: 07-15-2009, 12:33 PM
  3. what's wrong in here!!!
    By Annatar in forum New To Java
    Replies: 8
    Last Post: 11-14-2008, 02:55 AM
  4. right or wrong
    By jot321 in forum New To Java
    Replies: 7
    Last Post: 09-25-2008, 11:45 AM
  5. Can someone tell me what I did wrong??
    By booter4429 in forum New To Java
    Replies: 7
    Last Post: 08-13-2008, 08:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •