Results 1 to 2 of 2
  1. #1
    Warcrea is offline Member
    Join Date
    Feb 2012
    Rep Power

    Default Searching for tags in a string

    Hi everyone, sorry that my first post here is a question but I've been struggling with this for a while so any help would be greatly appreciated.
    I'm working on a project that requires my program to read a question from a text file, and then convert this question into a mathematical equation that can be calculated.
    An example from this questions.txt is

    What is [int] [+] [int]?
    As you can see certain areas have been surrounded by parentheses. These parts will need to be identified as mathematical objects and then added to the equation, while the areas outside these tags are stored in another string. The ints will be replaced by randomly generated integers after the question is read through.

    In short, after scanning through a question, there should be two strings. One containing a question written in english ("What is 2 + 2?"), and another written as a pure equation(2+2).

    My issue is getting the program to recognize these tagged areas and convert them into the equation friendly characters I need. So far it seems the best way to do this is to convert the entire string into a char list and scan through one char at a time, but I'm struggling to keep it simple and I'm reaching a ridiculous amount of nested loops.

    If anyone has any ideas it'd really be appreciated. Sorry for such a specific and wordy question, I'm sure it could've been explained better

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Tucson, AZ
    Rep Power

    Default Re: Searching for tags in a string

    Regular expressions might be just what you need. They allow you to find sequences of characters that follow a specified pattern.

    Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)

    If you start getting into mathematical expressions with nested parentheses and stuff, it will become much more complicated. But a regexp could easily identify a string like "2+2".
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Searching for a string in different log files
    By nitin kishore in forum Advanced Java
    Replies: 6
    Last Post: 09-14-2011, 09:32 PM
  2. searching for a word in a string
    By Brian-82 in forum New To Java
    Replies: 1
    Last Post: 05-11-2011, 07:19 PM
  3. Searching for a String within an linked list
    By phil128 in forum Advanced Java
    Replies: 5
    Last Post: 04-13-2009, 02:18 AM
  4. [SOLVED] Searching a string in a vector
    By coolFrenzi in forum Advanced Java
    Replies: 5
    Last Post: 02-22-2009, 12:53 AM
  5. Searching In a String Array - Problem
    By DillMan in forum New To Java
    Replies: 4
    Last Post: 12-07-2008, 09:12 PM

Posting Permissions

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