0

I know how to use substring() but why isn't this working, the user inputs an equation like "5t + 1" with a space before and after the "+". I want tVariable to hold the integer before it, in this case 5 and constant should hold the constant integer in this case 1, but I get an out of range error.

import java.util.*;
import javax.swing.JOptionPane;

public class project3030  {    
    public static void main(String[] args) {
        String L1x, tVariable, constant;
        L1x = JOptionPane.showInputDialog("This is the format (x=5t + 1)");

        int endIndex = L1x.indexOf("t");

        tVariable = L1x.substring(0, endIndex);

        int beginIndex = L1x.lastIndexOf(" ");
        int endIndex2 = L1x.indexOf("");            

        constant = L1x.substring(beginIndex, endIndex2);

        System.out.println(tVariable + constant);
    }
}

3 Answers 3

1

You need to change it to something more like

constant = L1x.substring(L1x.lastIndexOf(" ")).trim();

Then when you add the numbers, you have to Parse them before you add them.

int constantInt = Integer.parseInt(constant);

Or you could use this solution:

String[] input = L1x.split(" "); 

// remove the 't'
String tNum = input[0].substring(0, input[0].length() - 1);
int t = Integer.parseInt(tNum); 
int constant = Integer.parseInt(input[2]); 
String operator = input[1];

if (operator == "-")
    constant *= -1;
Sign up to request clarification or add additional context in comments.

10 Comments

I tried doing the parseInt and I declared a new variable = constantInt + 5 and printed that out but I got an error " For input string: " 5" I tried doing parseInt instead of constant as the argument but an actual string like "55" and that worked fine.
I parseInt tVariable and it works, so its only the constant that is having the problem
it looks like you have a space in your input string: " 5". You could try using the string's trim() method to remove the spaces. String trimmedInput = inputString.trim();
or int parsedInput = Integer.parseInt(constantString.trim());
@user3426759 i added the trim() method to the answer. I think having the space there was causing your error.
|
0

The reason you are getting this error is because your substring() range is off.

You are passing into substring() an invalid range because the first index is beginIndex which you set equal to lastIndexOf(" ") while endIndex2 you set equal to indexOf("") which will occur at the beginning of the string.

Thus, you have your ranges mixed up. When you make this statement:

constant = L1x.substring(beginIndex, endIndex2);

You're giving it an invaid range and will cause the specified error.

Comments

0

You could also do it different way. Let's say you create a JFrame and there you could put a textfield for every value, so for every "t" or constants, and then it would be much more simple to get those values. For characters like t or = you could use JLabels, so it would be a mixture of JTextFields and JLabels and I think it wouldn't be that ugly. I used this solution few times.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.