Results 1 to 8 of 8
Thread: What am I doing wrong?
- 04-14-2012, 04:51 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
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
- 04-14-2012, 05:30 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-14-2012, 08:34 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
Re: What am I doing wrong?
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 ...
- 04-14-2012, 08:49 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: What am I doing wrong?
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-14-2012, 10:44 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
Re: What am I doing wrong?
- 04-15-2012, 09:03 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: What am I doing wrong?
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-15-2012, 08:07 PM #7
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
Re: What am I doing wrong?
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.
- 04-15-2012, 08:22 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: What am I doing wrong?
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 2Last Post: 11-22-2009, 03:48 PM -
what is wrong?
By pinguxxx in forum Advanced JavaReplies: 3Last Post: 07-15-2009, 12:33 PM -
what's wrong in here!!!
By Annatar in forum New To JavaReplies: 8Last Post: 11-14-2008, 02:55 AM -
right or wrong
By jot321 in forum New To JavaReplies: 7Last Post: 09-25-2008, 11:45 AM -
Can someone tell me what I did wrong??
By booter4429 in forum New To JavaReplies: 7Last Post: 08-13-2008, 08:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks