This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-10-11 20:11:45

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Java Help

Can someone help debug this Java program?  It always does the 'else' branch.  I'm an absolute Java n00b.

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("*")) || (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

 

#2 2011-10-11 20:48:42

veggieman001
Scratcher
Registered: 2010-02-20
Posts: 1000+

Re: Java Help

use a java editor :IIIIIIIIIIIIIII


Posts: 20000 - Show all posts

Offline

 

#3 2011-10-11 20:57:40

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Java Help

veggieman001 wrote:

use a java editor :IIIIIIIIIIIIIII

I was thinking the same thing. Really, it makes things so much easier.  hmm

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.)


http://www.blocks.scratchr.org/API.php?action=random&return=image&link1=http://i.imgur.com/OZn2RD3.png&link2=http://i.imgur.com/duzaGTB.png&link3=http://i.imgur.com/CrDGvvZ.png&link4=http://i.imgur.com/POEpQyZ.png&link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#4 2011-10-11 22:34:22

what-the
Scratcher
Registered: 2009-10-04
Posts: 1000+

Re: Java Help

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)


http://imageshack.us/m/64/9034/ddfss.pngMy site
Find someone post count. Click posts under username. Find number of pages. Times that by 40 for min and 60 for max and you have a rough estimate of post count.

Offline

 

#5 2011-10-11 23:00:49

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Java Help

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. ^_^


http://www.blocks.scratchr.org/API.php?action=random&return=image&link1=http://i.imgur.com/OZn2RD3.png&link2=http://i.imgur.com/duzaGTB.png&link3=http://i.imgur.com/CrDGvvZ.png&link4=http://i.imgur.com/POEpQyZ.png&link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#6 2011-10-12 08:24:48

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Java Help

Harakou wrote:

veggieman001 wrote:

use a java editor :IIIIIIIIIIIIIII

I was thinking the same thing. Really, it makes things so much easier.  hmm

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

 

#7 2011-10-12 08:27:45

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Java Help

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

 

#8 2011-10-12 20:30:33

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Java Help

maxskywalker wrote:

Harakou wrote:

veggieman001 wrote:

use a java editor :IIIIIIIIIIIIIII

I was thinking the same thing. Really, it makes things so much easier.  hmm

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.


http://www.blocks.scratchr.org/API.php?action=random&return=image&link1=http://i.imgur.com/OZn2RD3.png&link2=http://i.imgur.com/duzaGTB.png&link3=http://i.imgur.com/CrDGvvZ.png&link4=http://i.imgur.com/POEpQyZ.png&link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#9 2011-10-14 13:44:53

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Java Help

I am now convinced that my problem lies in my or (||) operator.  Any help?

Offline

 

#10 2011-10-14 16:10:03

maxamillion321
Scratcher
Registered: 2011-06-17
Posts: 500+

Re: Java Help

Mabye you need to download Java.
www.Java.com/download Java.

Last edited by maxamillion321 (2011-10-14 16:11:19)

Offline

 

#11 2011-10-14 16:20:50

gbear605
Scratcher
Registered: 2008-03-06
Posts: 1000+

Re: Java Help

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  tongue

I'm not quite sure how to use it either  tongue


Yeah, I'm mostly inactive.  I check in once in a while though.  If you want to contact me, I have a contact form at my website, http://escratch.org

Offline

 

#12 2011-10-14 18:19:32

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Java Help

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  tongue

I'm not quite sure how to use it either  tongue

Yeah, I considered that.  Right now, I'm really just more curious about what's wrong with it.

Offline

 

#13 2011-10-14 18:23:50

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Java Help

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

 

#14 2011-10-14 22:05:53

what-the
Scratcher
Registered: 2009-10-04
Posts: 1000+

Re: Java Help

Try adding more brackets. You need brackets around every statment in the if.

if (input.equals("/") || input.equals("÷")) {


to

if (  ( input.equals("/") )  || ( input.equals("÷")  ) ) {


http://imageshack.us/m/64/9034/ddfss.pngMy site
Find someone post count. Click posts under username. Find number of pages. Times that by 40 for min and 60 for max and you have a rough estimate of post count.

Offline

 

#15 2011-10-15 11:18:31

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Java Help

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

 

#16 2011-10-15 11:24:11

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Java Help

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

 

#17 2011-10-15 21:46:49

what-the
Scratcher
Registered: 2009-10-04
Posts: 1000+

Re: Java Help

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)


http://imageshack.us/m/64/9034/ddfss.pngMy site
Find someone post count. Click posts under username. Find number of pages. Times that by 40 for min and 60 for max and you have a rough estimate of post count.

Offline

 

#18 2011-10-16 02:40:27

Vurb
Scratcher
Registered: 2010-05-09
Posts: 100+

Re: Java Help

IDE's really aren't that bad, why don't you just get Eclipse?

Offline

 

#19 2011-10-17 09:32:42

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Java Help

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

 

#20 2011-10-17 09:41:51

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Java Help

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

 

#21 2011-10-17 09:44:16

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Java Help

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

 

Board footer