Pages: 1
Topic closed
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)
Offline
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?
Offline
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. remember it starts with 0 not 1.
Offline
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.
remember it starts with 0 not 1.
I just realized that and fixed that, but it still isn't working
@Harakou
Like I said, it says "char cannot be dereferenced."
I seriously have no idea what's wrong.
Offline
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.
Offline
Topic closed
Pages: 1