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

#1 2012-03-31 15:35:45

slayerrobe7
Scratcher
Registered: 2011-06-24
Posts: 500+

java problems

i am going threw the java tutorials and on of the projects was to use the computer to find the square root of a number

my code is

Code:

class Root {
    private static Object math;
    public static void main(String[] args) {
       int number = 225;
       System.out.println("The square root of"
               + number
               + "is"
               + math.sqrt(number)
      );        
    }
}

it is not working and i don't know why please help


!!!When it comes to Scratch2.0 I am totally like freaking out!!!

Offline

 

#2 2012-03-31 15:39:03

Jwosty
Scratcher
Registered: 2009-12-19
Posts: 500+

Re: java problems

I don't think this really belongs in Scratch, maybe try SO?


http://i39.tinypic.com/18ert5.png Google it.  smile

Offline

 

#3 2012-03-31 15:40:03

ZeroLuck
Scratcher
Registered: 2010-02-23
Posts: 500+

Re: java problems

This can't work!
Just do this:

Code:

public class Root {
    public static void main(String[] args) {
       int number = 225;
       System.out.println("The square root of"
               + number
               + "is"
               + Math.sqrt(number)
        );
    }
}

The "math" Object you made is null.
(=> NullPointerException when you use it)
And java.lang.Math is final, you can't make an instance of it.

Last edited by ZeroLuck (2012-03-31 15:41:02)


http://3.bp.blogspot.com/-oL2Atzp0Byw/T465vIQ36dI/AAAAAAAAADo/1vqL4PvhkM0/s1600/scratchdachwiki.png

Offline

 

#4 2012-03-31 15:43:06

slayerrobe7
Scratcher
Registered: 2011-06-24
Posts: 500+

Re: java problems

thank you


!!!When it comes to Scratch2.0 I am totally like freaking out!!!

Offline

 

#5 2012-03-31 15:53:35

slayerrobe7
Scratcher
Registered: 2011-06-24
Posts: 500+

Re: java problems

if i have anymore problems ill post here


!!!When it comes to Scratch2.0 I am totally like freaking out!!!

Offline

 

#6 2012-03-31 19:17:24

nickbrickmaster
Scratcher
Registered: 2010-02-02
Posts: 500+

Re: java problems

That's because you re-defined math, which actually should have been Math, which you shouldn't have redefined..


Ask me what I'm doing, wait an hour than roll a die, if it's 4-6, I'm playing Skyrim, if it's 1, I'm eating, if it's 2-3 I'm programming.
Steam: nickbrickmaster | RotMG: PwnThemAll | Minecraft: nickbrickmaster | League Of Legends: BaneOfTitans

Offline

 

Board footer