# Thread: Find a number from a string

1. Member
Join Date
Mar 2009
Posts
6
Rep Power
0

## Find a number from a string

Hey guys I need ur expertise.. I'm new to Java and been assigned a homework to solve for two equations and get the value of x and y:

2x+3y=10
7x-8y=-2

something like that. The user would input the equations in a TextField.
So when I convert the getText() from the textfield to a string, how do I extract the numbers (say 2 from 2x or 3 from 3y) to use it to solve the equation? I tried using charAt but then the problem is when the user inputs x+3y=5then it wouldnt work anymore, or becomes more complicated. Im thinking that indexOf might help, but I dont really understand how it works, so please, any assistance would really help.

2. use regex to split it into tokens.
OR (easier) use StringTokenizer class or StreamTokenizer.

3. Member
Join Date
Mar 2009
Posts
6
Rep Power
0
String example = "32x+35y=10";
int dot = example.indexOf("x");
String lol = example.substring(0,dot);
System.out.println(lol);
int dota = example.lastIndexOf("y");
int doto = example.indexOf("+");
String lola = example.substring(doto+1,dota);
System.out.println(lola);

alright so i was able to printout 32 and 35 but now the problem is i want it to be either the indexOf("-") or the indexOf("+"); so that it doesnt matter if the operand is + or -, the coefficient of y will start at the end of the indexOf("operand") and before the indexOf("y"). I hope you guys got what i was trying to say :P

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•