Can someone help debug this Java program? It always does the 'else' branch. I'm an absolute Java n00b.
class Calculator {
public static void main (String args[]) {
double num1;
double num2;
String input;
input = args[0];
num1 = Double.parseDouble(input);
input = args[2];
num2 = Double.parseDouble(input);
input = args[1];
if (input.equals("+")) {
System.out.print(num1);
System.out.print(" + ");
System.out.print(num2);
System.out.print(" = ");
System.out.println(num1 + num2);
}
else if (input.equals("-")) {
System.out.print(num1);
System.out.print(" - ");
System.out.print(num2);
System.out.print(" = ");
System.out.println(num1 - num2);
}
else if ((input.equals("*")) || (input.equals("x"))) {
System.out.print(num1);
System.out.print(" * ");
System.out.print(num2);
System.out.print(" = ");
System.out.println(num1 * num2);
}
else if ((input.equals("/")) || (input.equals("÷"))) {
System.out.print(num1);
System.out.print(" / ");
System.out.print(num2);
System.out.print(" = ");
System.out.println(num1 / num2);
}
else if (input.equals("%")) {
System.out.print(num1);
System.out.print(" % ");
System.out.print(num2);
System.out.print(" = ");
System.out.println(num1 % num2);
}
else {
System.out.println("That is not a recognized operation.");
}
}
}I realize that it's probably something really obvious, like a misspelled word (I haven't bothered downloading a Java text editor; just using some Unix commands to compile a TextEdit (Notepad for Windows users) files). Help appreciated, and thanks in advance.
Edit: Thanks, guys! Testing this out right now.
Edit: It works!
Edit: Oh, wait, using the asterisk (*) and division (÷, opt+/ on a Mac) operators don't work. Which is weird, because their counterparts (x and /) work just fine. Is there something wrong with my || or operator?
Last edited by maxskywalker (2011-10-15 11:19:43)
Offline
veggieman001 wrote:
use a java editor :IIIIIIIIIIIIIII
I was thinking the same thing. Really, it makes things so much easier.
Anyway, I stuck your code in Eclipse and got the same problem as you. Oddly, when I manually defined input in the code, it works just fine. For some reason it doesn't like using the command line arguments. (I checked if the args[1] was being correctly defined too, and it was. Despite printing out "+", for example, it would still insist that args[1] != "+" if it was set through the command line arguments.)
Offline
You are using strings. To compare strings in java you need to use;
if (string1.equals(string2))
ie.
if (input.equals("+" ) ) {
You should at least download gedit works on Windows and Linux. All it does is highlight key words but it can handle about 100 different languages.
Last edited by what-the (2011-10-11 22:35:01)
My site Offline
what-the wrote:
You are using strings. To compare strings in java you need to use;
if (string1.equals(string2))
ie.
if (input.equals("+" ) ) {
Ah, you're correct. After some research, "==" compares reference objects as opposed to "*.equals(String)", which compares values. Since two strings can often have the same value but be referenced by different objects, "*.equals(String)" is usually better.
Well, I guess you learn something new every day. ^_^
Offline
Harakou wrote:
veggieman001 wrote:
use a java editor :IIIIIIIIIIIIIII
I was thinking the same thing. Really, it makes things so much easier.
![]()
Anyway, I stuck your code in Eclipse and got the same problem as you. Oddly, when I manually defined input in the code, it works just fine. For some reason it doesn't like using the command line arguments. (I checked if the args[1] was being correctly defined too, and it was. Despite printing out "+", for example, it would still insist that args[1] != "+" if it was set through the command line arguments.)
What do you mean be 'manually' setting input? Do you mean saying 'input = "+"' inside the code, or do you mean somehow setting it during runtime? And I don't really need a Java editor. For the most part, I spell words correctly, and when I don't, the command line tells me when I need to compile it. Also, sometimes when I'm feeling tired of TextEdit, I open it in the Flash Actionscript editor, which shares most of the keywords and general syntax.
Offline
Harakou wrote:
what-the wrote:
You are using strings. To compare strings in java you need to use;
if (string1.equals(string2))
ie.
if (input.equals("+" ) ) {Ah, you're correct. After some research, "==" compares reference objects as opposed to "*.equals(String)", which compares values. Since two strings can often have the same value but be referenced by different objects, "*.equals(String)" is usually better.
Well, I guess you learn something new every day. ^_^
Oh. Interesting. Thanks!
Offline
maxskywalker wrote:
Harakou wrote:
veggieman001 wrote:
use a java editor :IIIIIIIIIIIIIII
I was thinking the same thing. Really, it makes things so much easier.
![]()
Anyway, I stuck your code in Eclipse and got the same problem as you. Oddly, when I manually defined input in the code, it works just fine. For some reason it doesn't like using the command line arguments. (I checked if the args[1] was being correctly defined too, and it was. Despite printing out "+", for example, it would still insist that args[1] != "+" if it was set through the command line arguments.)What do you mean be 'manually' setting input? Do you mean saying 'input = "+"' inside the code, or do you mean somehow setting it during runtime? And I don't really need a Java editor. For the most part, I spell words correctly, and when I don't, the command line tells me when I need to compile it. Also, sometimes when I'm feeling tired of TextEdit, I open it in the Flash Actionscript editor, which shares most of the keywords and general syntax.
Yes, I meant just writing input = "+" into the code.
Offline
I am now convinced that my problem lies in my or (||) operator. Any help?
Offline
Mabye you need to download Java.
www.Java.com/download Java.
Last edited by maxamillion321 (2011-10-14 16:11:19)
Offline
Just make it this:
class Calculator {
public static void main (String args[]) {
double num1;
double num2;
String input;
input = args[0];
num1 = Double.parseDouble(input);
input = args[2];
num2 = Double.parseDouble(input);
input = args[1];
if (input.equals("+")) {
System.out.print(num1);
System.out.print(" + ");
System.out.print(num2);
System.out.print(" = ");
System.out.println(num1 + num2);
}
else if (input.equals("-")) {
System.out.print(num1);
System.out.print(" - ");
System.out.print(num2);
System.out.print(" = ");
System.out.println(num1 - num2);
}
else if (input.equals("x")) {
System.out.print(num1);
System.out.print(" * ");
System.out.print(num2);
System.out.print(" = ");
System.out.println(num1 * num2);
}
else if (input.equals("*")) {
System.out.print(num1);
System.out.print(" * ");
System.out.print(num2);
System.out.print(" = ");
System.out.println(num1 * num2);
}
else if (input.equals("÷")) {
System.out.print(num1);
System.out.print(" / ");
System.out.print(num2);
System.out.print(" = ");
System.out.println(num1 / num2);
}
else if (input.equals("/")) {
System.out.print(num1);
System.out.print(" / ");
System.out.print(num2);
System.out.print(" = ");
System.out.println(num1 / num2);
}
else if (input.equals("%")) {
System.out.print(num1);
System.out.print(" % ");
System.out.print(num2);
System.out.print(" = ");
System.out.println(num1 % num2);
}
else {
System.out.println("That is not a recognized operation.");
}
}
}What i did was just seperate it out into two different sections, so it doesn't use the || thing
I'm not quite sure how to use it either
Offline
gbear605 wrote:
Just make it this:
Code:
class Calculator { public static void main (String args[]) { double num1; double num2; String input; input = args[0]; num1 = Double.parseDouble(input); input = args[2]; num2 = Double.parseDouble(input); input = args[1]; if (input.equals("+")) { System.out.print(num1); System.out.print(" + "); System.out.print(num2); System.out.print(" = "); System.out.println(num1 + num2); } else if (input.equals("-")) { System.out.print(num1); System.out.print(" - "); System.out.print(num2); System.out.print(" = "); System.out.println(num1 - num2); } else if (input.equals("x")) { System.out.print(num1); System.out.print(" * "); System.out.print(num2); System.out.print(" = "); System.out.println(num1 * num2); } else if (input.equals("*")) { System.out.print(num1); System.out.print(" * "); System.out.print(num2); System.out.print(" = "); System.out.println(num1 * num2); } else if (input.equals("÷")) { System.out.print(num1); System.out.print(" / "); System.out.print(num2); System.out.print(" = "); System.out.println(num1 / num2); } else if (input.equals("/")) { System.out.print(num1); System.out.print(" / "); System.out.print(num2); System.out.print(" = "); System.out.println(num1 / num2); } else if (input.equals("%")) { System.out.print(num1); System.out.print(" % "); System.out.print(num2); System.out.print(" = "); System.out.println(num1 % num2); } else { System.out.println("That is not a recognized operation."); } } }What i did was just seperate it out into two different sections, so it doesn't use the || thing
![]()
I'm not quite sure how to use it either![]()
Yeah, I considered that. Right now, I'm really just more curious about what's wrong with it.
Offline
maxamillion321 wrote:
Mabye you need to download Java.
www.Java.com/download Java.
I don't think I need to. All Unix-based operating systems come with several programming languages including Java, and I have a Mac which qualifies as a Unix-based. And besides, if the rest of it worked, why shouldn't it work now?
Offline
Try adding more brackets. You need brackets around every statment in the if.
if (input.equals("/") || input.equals("÷")) {
to
if ( ( input.equals("/") ) || ( input.equals("÷") ) ) {
My site Offline
what-the wrote:
Try adding more brackets. You need brackets around every statment in the if.
if (input.equals("/") || input.equals("÷")) {
to
if ( ( input.equals("/") ) || ( input.equals("÷") ) ) {
Thanks! I'll try that now.
Offline
maxskywalker wrote:
what-the wrote:
Try adding more brackets. You need brackets around every statment in the if.
if (input.equals("/") || input.equals("÷")) {
to
if ( ( input.equals("/") ) || ( input.equals("÷") ) ) {Thanks! I'll try that now.
Hm. Not quite working.
Last edited by maxskywalker (2011-10-15 11:24:23)
Offline
Oh if you want to know
args[0] : This is the amount of input arguments.
In your case you need 3. So you should write an if statment like so.
if (args[0] == 3) {
also
args[1] = first number
args[2] = argument
args[3] = second number
Last edited by what-the (2011-10-15 21:50:17)
My site Offline
what-the wrote:
Oh if you want to know
args[0] : This is the amount of input arguments.
In your case you need 3. So you should write an if statment like so.
if (args[0] == 3) {
also
args[1] = first number
args[2] = argument
args[3] = second number
Oh. Really? I thought args[] was an array. Which means that args[0] is the first argument.
Offline
Vurb wrote:
IDE's really aren't that bad, why don't you just get Eclipse?
I just haven't bothered to. As of yet, I'm fine with TextEdit.
Offline
what-the wrote:
Oh if you want to know
args[0] : This is the amount of input arguments.
In your case you need 3. So you should write an if statment like so.
if (args[0] == 3) {
also
args[1] = first number
args[2] = argument
args[3] = second number
No. That can't be. My program works fine right now, so I must have the args[] right. Oh, and I know how to write an if statement.
Offline