Results 1 to 3 of 3
  1. #1
    florentp is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default 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. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    13

    Default

    use regex to split it into tokens.
    OR (easier) use StringTokenizer class or StreamTokenizer.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    florentp is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default

    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

  1. Find java objects in a given string.
    By sarathi in forum New To Java
    Replies: 3
    Last Post: 03-06-2009, 08:42 AM
  2. Find all permutations of a number
    By matzahboy in forum New To Java
    Replies: 6
    Last Post: 12-02-2008, 03:59 AM
  3. Replies: 8
    Last Post: 04-22-2008, 03:21 AM
  4. Find nth root of a number
    By perito in forum New To Java
    Replies: 1
    Last Post: 03-03-2008, 06:51 AM
  5. Find and replace ( in a String
    By hamish10101 in forum New To Java
    Replies: 6
    Last Post: 01-17-2008, 05:51 AM

Posting Permissions

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