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

#1 2012-05-26 16:46:34

matthew8092001
Scratcher
Registered: 2010-09-12
Posts: 100+

[Java] Simple derivative calculator help?

Code:

package Stuff;
import java.util.*;

public class Derivative {

  
    public static void main(String[] args) {
        Scanner yay = new Scanner (System.in);
        System.out.println("Enter the ax^b function.");
        String get = yay.nextLine();
        int a = get.charAt(1).toInt;
        int b = get.charAt(4).toInt;
        System.out.println("The derivative is: "+a*b+"x^"+(b-1));
    }
}

It says: "char cannot be dereferenced." Any ideas?

Last edited by matthew8092001 (2012-05-27 11:23:19)


http://1.bp.blogspot.com/_GVA115I1I8Y/TMGwbJ056kI/AAAAAAAABHs/kpvg0oCKV2g/s1600/happy.gif
lol

Offline

 

#2 2012-05-26 18:15:16

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

Re: [Java] Simple derivative calculator help?

Sounds like you've got a case where the index of the String you're referencing doesn't exist. Make sure that the string you enter is long enough (at least 5 characters long in this case.) If that's not it, could you describe the problem any further?


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

 

#3 2012-05-26 18:35:09

poopo
Scratcher
Registered: 2009-09-20
Posts: 1000+

Re: [Java] Simple derivative calculator help?

Code:

package Stuff;
import java.util.*;

public class Derivative {

  
    public static void main(String[] args) {
        Scanner yay = new Scanner (System.in);
        System.out.println("Enter the ax^b function.");
        String get = yay.nextLine();
        int a = get.charAt(0).toInt;
        int b = get.charAt(3).toInt;
        System.out.println("The derivative is: "+a*b+"x^"+(b-1));
    }
}

that should work.  wink  remember it starts with 0 not 1.


http://i45.tinypic.com/28rnqki.jpg

Offline

 

#4 2012-05-27 11:29:29

matthew8092001
Scratcher
Registered: 2010-09-12
Posts: 100+

Re: [Java] Simple derivative calculator help?

poopo wrote:

Code:

package Stuff;
import java.util.*;

public class Derivative {

  
    public static void main(String[] args) {
        Scanner yay = new Scanner (System.in);
        System.out.println("Enter the ax^b function.");
        String get = yay.nextLine();
        int a = get.charAt(0).toInt;
        int b = get.charAt(3).toInt;
        System.out.println("The derivative is: "+a*b+"x^"+(b-1));
    }
}

that should work.  wink  remember it starts with 0 not 1.

I just realized that and fixed that, but it still isn't working  sad
@Harakou
Like I said, it says "char cannot be dereferenced."
I seriously have no idea what's wrong.


http://1.bp.blogspot.com/_GVA115I1I8Y/TMGwbJ056kI/AAAAAAAABHs/kpvg0oCKV2g/s1600/happy.gif
lol

Offline

 

#5 2012-05-30 15:36:03

scratchisthebest
Scratcher
Registered: 2009-02-08
Posts: 500+

Re: [Java] Simple derivative calculator help?

What line number?
Did you try adding () after toInt?
I'm not too familiar with Scanner, so these are just guesses, but toInt looks like a function to me.


bye 1.4, we all loved you. but we all outgrew the site. 2.0 is a welcome change.
http://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.png

Offline

 

Board footer