Separating Digits in a String

Printable View

• October 3rd, 2011, 12:21 AM
Staticity
Separating Digits in a String
So, as a personal project.. I'm making a Program to solve for Derivatives of any 1-variable equation.. Which includes (for those of you who remember Calculus) the power rule, product rule, quotient rule, chain rule... etc..

So here is my code for the getExponent Method!
Code :

``` public int getExponent(String userInput){ String string = "";   for(int a = getVariable(userInput).length(); a < userInput.length(); a++){ int digit = userInput.charAt(a)-48; if(digit <= 9 && digit >= 0) string += digit + ""; else break; }   int newExponent = Integer.parseInt(string); return newExponent; }```

Now, I have two other methods.. getCoefficient and getVariable... The getCoefficient method does the exact same as this method, however, it starts checking the characters after the variable (or the exponential portion)... Let me show you my predicament

Sample Input:
5x^2

Output:
5 - Coefficient
x^ - Variable
2 - Exponent

Now that's all nice and dandy, but when it comes to this...

Input:
5x^2 + 5x^3

Output:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:470)
at java.lang.Integer.parseInt(Integer.java:499)
at Input.getExponent(Input.java:51)
at Main.main(Main.java:16)

It says that I'm accepting a non integer value, and trying to parse that into an Integer.. However in my code, I clearly state that if the 'int digit' is not within the range of 0-9, to break from the loop... The getCoefficient method works just fine using extremely similar code (they start at different positions). Any tips please?

EDIT: I just checked out something, and if I change the "int a = getVariable(userInput).length()" to int a = 0... It works just fine and establishes the break whenever a non digit comes by... However, I want the variable a to start where the getVariable method ends.. How can I do this?
• October 3rd, 2011, 04:38 AM
Mr.777
Re: Separating Digits in a String
Can you kindly tell about getVariable(parameter) function? What does it do?

And the reason is, your string variable is actually empty. And when it breaks from the loop and comes here
Code :

`int newExponent = Integer.parseInt(string);`
it tries to parse an empty string and generates error.
• October 3rd, 2011, 06:31 AM
Staticity
[SOLVED]Re: Separating Digits in a String
Quote:

Originally Posted by Mr.777
Can you kindly tell about getVariable(parameter) function? What does it do?

And the reason is, your string variable is actually empty. And when it breaks from the loop and comes here
Code :

`int newExponent = Integer.parseInt(string);`
it tries to parse an empty string and generates error.

I don't believe that I would rudely tell about my getVariable(userInput) function :)

It basically does the same exact thing, but starts from where the getCoefficient(userInput)'s ends... So altogether in a visual:

userInput: 532x^3
getCoefficient(userInput) = **Start at index 0 and display digits until a non-digit character occurs**523
getVariable(userInput) = **Start at index getCoefficient(userInput).length() (which in this case is 3) and display non-digits until a digit occurs**
getExponent(userInput) = **Start at index getVariable(userInput).length() (which would be .........

Thanks a ton for guiding me to this error! :D

I started at the length of the getVariable(userInput) which would only be 2. Therefore, I would go back to the digit 2 of the Coefficient and not the actual exponent! So to fix my error, I made a new string, which adds the length of the coefficient and variable, then starts the For Loop at that integer.

What a silly mistake aha.
• October 3rd, 2011, 06:39 AM
Mr.777
Re: Separating Digits in a String
Why don't you simply place an if-else before the parsing statement. If string is null, do something you want to do and in else part the thing you want to return.
Anyways, good luck.