Results 1 to 3 of 3
Thread: Find a number from a string
- 03-20-2009, 08:11 PM #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.
- 03-20-2009, 08:36 PM #2
use regex to split it into tokens.
OR (easier) use StringTokenizer class or StreamTokenizer.USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 03-20-2009, 09:01 PM #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
Similar Threads
-
Find java objects in a given string.
By sarathi in forum New To JavaReplies: 3Last Post: 03-06-2009, 08:42 AM -
Find all permutations of a number
By matzahboy in forum New To JavaReplies: 6Last Post: 12-02-2008, 03:59 AM -
how to right a program that find kth number in two sorted array?
By fireball2008 in forum New To JavaReplies: 8Last Post: 04-22-2008, 03:21 AM -
Find nth root of a number
By perito in forum New To JavaReplies: 1Last Post: 03-03-2008, 06:51 AM -
Find and replace ( in a String
By hamish10101 in forum New To JavaReplies: 6Last Post: 01-17-2008, 05:51 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks