Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Separating Digits in a String

  1. #1
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default 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!
    	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?
    Last edited by Staticity; October 3rd, 2011 at 12:41 AM.
    Simplicity calls for Complexity. Think about it.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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
    int newExponent = Integer.parseInt(string);
    it tries to parse an empty string and generates error.

  3. #3
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default [SOLVED]Re: Separating Digits in a String

    Quote Originally Posted by Mr.777 View Post
    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
    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!

    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.
    Last edited by Staticity; October 3rd, 2011 at 06:33 AM.
    Simplicity calls for Complexity. Think about it.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

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

Similar Threads

  1. Replies: 20
    Last Post: June 26th, 2011, 05:40 PM
  2. Problem with digits/letters
    By te09hn8 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 22nd, 2011, 07:30 PM
  3. Remove last digits of an IP address.
    By Jonathan_C in forum Algorithms & Recursion
    Replies: 1
    Last Post: November 15th, 2010, 11:55 AM
  4. Replies: 2
    Last Post: February 19th, 2010, 07:10 AM
  5. Replies: 2
    Last Post: February 4th, 2009, 11:24 AM