Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How to change a String value into a number and then back into a String.

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Cool How to change a String value into a number and then back into a String.

    I know it sounds easy but I'm rather tired right now and am, hopefully, nearly done with this assignment, another parser, and I need it to merely convert every time TokenHandler.getNextToken() equals some number and convert that and change it to "n".

    I'm trying to change all the numbers in the input string to "n"s.

    So instead of

    123+135*142$

    it will have

    n+n*n$

    The method "validate" should check every string against the input string (the improved input string with n's instead of numbers, and also the top of the state stack by calling checker(TokenHandler.getNextToken(getString()), states.peek()) and setting some boolean to true and returning false if checker ever returns false (Yes I'm aware that right now validate is currently declared as void. I'll fix that later.) and returning true if "tokens" stack has only E on it and "states" stack has 1 on top and 0 as the second value on the stack and nothing else below it.

    But how do I deal with converting the numbers in the original input String into "n"'s? Also, TokenHandler had to be reset to take in the value n, or else deleting an "n" would have thrown an error I think, but anyway, now I can use setString() I think to change the numbers into n's but how do I do that? Also, how do I make sure it stops executing and returns true once it reaches the "acceptable" condition mentioned earlier? The E and 0 1 situation.

    Here's the code:
     
       import java.util.StringTokenizer;
       import java.io.*;
       import java.util.*;
     
       public class LR1Parser
       {
          private Stack<String> tokens;
          private Stack<Integer> states;
          private String tokenString;
     
          private static class TokenHandler
          {
             private String str;
             private char nextchar;
     
     
             public static String getNextToken(String tokenStream)
             { 
                if (tokenStream == null)
                   return null;
                String numberString = "";
                int index = 0;
                char c = tokenStream.charAt(0);
                if ( c == '+')
                {
                   return "+";
     
     
                }
     
                else if (c == '*')
                {
                   return "*";
     
                }
     
                else if (c == '(')
                {
                   return "(";
                }
     
                else if (c == ')')
                {
                   return ")";
     
                }
     
                else if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4'|| c == '5' || c == '6' || c == '7'|| c == '8' || c == '9')
                {
                   char c2 = 'a';
                   numberString = numberString + String.valueOf(c);
                   if (tokenStream.length() == 1)
                   {
                      return numberString;
                   }
                   else
                   {
                      c2 = tokenStream.charAt(1);
                      if  (c2 == '0' || c2 == '1' || c2 == '2' || c2 == '3' || c2 == '4'|| c2 == '5' || c2 == '6' || c2 == '7'|| c2 == '8' || c2 == '9')
                      {
     
                         numberString = numberString  + getNextToken(tokenStream.substring(1));
     
                      }
                   }
                   return numberString;
                }
     
                else if ( c == '$')
                {
                   return "$";
                }
     
    				else if (c == 'n')
    				return 'n';
     
                else
                {
                   return ("Bad String!");
                }
     
             }
     
             public static String deleteToken(String tokenStream)
             {
                String temp = getNextToken(tokenStream);
                System.out.println(tokenStream);
                int number = temp.length();
                String temp2 = tokenStream.substring(number);
             //tokenStream = temp2;
                System.out.println(temp2);
                return temp2;
     
             }
     
     
     
          }
     
          public LR1Parser(String input)
          {
     
             tokens = new Stack<String>();
             states = new Stack<Integer>();     
             setString(input);
             states.push(0);
     
     
     
          }
     
          public void setString(String tokenString)
          {
             this.tokenString = tokenString;
          }
     
          public String getString()
          {
             return tokenString;
          }
     
          public String read(String input)
          {
     
          }
     
          public void validate(String str)
          {
     
     
     
          }
     
          public boolean checker (String token, Integer number)
          {
     
             if (token.equals("n") )
             {
     
                if (number == 0 || number == 4 || number == 6 || number == 7)
                {
                   states.push(5);
                   tokens.push("n");
                   setString(TokenHandler.deleteToken(getString()));
                   return true;
                }
     
                else
                   return false;
     
             }
     
             else if (token.equals("+"))
             {
     
                if (number == 1 || number == 8)
                {
                   states.push(6);
                   tokens.push("+");
                   setString(TokenHandler.deleteToken(getString()));
                   return true;
     
                }
     
                else if (number == 2)
                {
                   if (tokens.peek().equals("T"))
                   {
                      tokens.pop();
                      tokens.push("E");
                      states.pop();
     
                      if (states.peek() == 0)
                         states.push(1);
                      else if (states.peek() == 4)
                         states.push(8);
                      return true;
     
                   }
     
                   else
                   {
                      System.out.println("Error!");
                      return false;
     
                   }
     
     
                }
     
     
                else if (number == 3)
                {
     
                   if (tokens.peek().equals("F"))
                   {
                      tokens.pop();
                      states.pop();
                      if (states.peek() == 0 || states.peek() == 4)
                         states.push(2);
                      else if (states.peek() == 6)
                         states.push(9);
                      return true;
     
                   }
     
                   else
                   {
                      System.out.println("Error!");
                      return false;
     
                   }
     
                }
     
                else if (number == 5)
                {
     
                   if (tokens.peek().equals("n"))
                   {
                      tokens.pop();
                      states.pop();
                      if (states.peek() == 0 || states.peek() == 4 || states.peek() == 6)
                         states.push(3);
                      else if (states.peek() == 7)
                         states.push(10);
                      return true;
                   }
     
                   else
                   {
                      System.out.println("Error!");
                      return false;
     
                   }
     
     
                }
     
                else if (number == 8)
                {
                   states.push(6);
                   tokens.push("+");
                   setString(TokenHandler.deleteToken(getString()));
                   return true;
     
                }
     
                else if (number == 9)
                {
                   if (tokens.peek().equals("T"))
                   { // begin if
                      tokens.pop();
                      states.pop();
     
                      if (tokens.peek().equals("+"))
                      { // begin if
                         tokens.pop();
                         states.pop();
                         if (tokens.peek().equals("E"))
                         { // begin if
                            tokens.pop();
                            states.pop();
                            tokens.push("E");
                            if (states.peek() == 0)
                               states.push(1);
                            else if (states.peek() == 4)
                               states.push(8);
                            return true;
     
                         } // end if 
     
                         else
                         { // begin else
                            System.out.println("Error!");
                            return false;
                         } // end else
                      } // end if 
     
                      else
                      { // begin else
                         System.out.println("Error!");
                         return false;
                      } // end else
     
                   } // end if
     
                   else
                   { // begin else
                      System.out.println("Error!");
                      return false;
     
                   } // end else
     
     
                }
     
                else if (number == 10)
                {
                   if (tokens.peek().equals("F"))
                   {
                      tokens.pop();
                      states.pop();
                      if (tokens.peek().equals("*"))
                      {
                         tokens.pop();
                         states.pop();
                         if (tokens.peek().equals("T"))
                         {
                            tokens.pop();
                            states.pop();
                            tokens.push("T");
                            if(states.peek() == 0 || states.peek() == 4)
                               states.push(2);
                            else if (states.peek() == 6)
                               states.push(9);
                            return true;
     
                         }
     
                         else
                         { // begin else
                            System.out.println("Error!");
                            return false;
     
                         } // end else
     
                      }
     
                      else
                      { // begin else
                         System.out.println("Error!");
                         return false;
     
                      } // end else
     
     
                   }
     
                   else
                   { // begin else
                      System.out.println("Error!");
                      return false;
     
                   } // end else
     
                }
     
                else if (number == 11)
                {
                   if (tokens.peek().equals(")"))
                   {
                      tokens.pop();
                      states.pop();
                      if (tokens.peek().equals("E"))
                      {
                         tokens.pop();
                         states.pop();
                         if(tokens.peek().equals("("))
                         {
                            tokens.pop();
                            states.pop();
                            tokens.push("F");
                            if (states.peek() == 0 || states.peek() == 4 || states.peek() == 6)
                               states.push(3);
                            else if (states.peek() == 7)
                               states.push(10);
                            return true;
                         }
     
                         else
                         { // begin else
                            System.out.println("Error!");
                            return false;
     
                         } // end else
                      }
     
                      else
                      { // begin else
                         System.out.println("Error!");
                         return false;
     
                      } // end else
     
                   }
     
                   else
                   { // begin else
                      System.out.println("Error!");
                      return false;
     
                   } // end else
     
                }
     
                else
                { // begin else
                   System.out.println("Error!");
                   return false;
     
                } // end else
     
             }
     
             else if (token.equals("*"))
             { // begin else if
     
                if (number == 2 || number == 9)
                { // begin if
                   states.push(7);
                   setString(TokenHandler.deleteToken(getString()));
                   return true;
                } // end if
     
                else if (number == 3)
                { // begin else if
                   if (tokens.peek().equals("F"))
                   { // begin if
                      states.pop();
                      tokens.pop();
                      tokens.push("T");
                      if (states.peek() == 0 || states.peek() == 4)
                         states.push(2);
                      else if (states.peek() == 6)
                         states.push(9);
                      return true;
                   } // end if
     
                   else
                   { // begin else
                      System.out.println("Error!");
                      return false;
                   } // end else
     
                } // end else if
     
                else if (number == 5)
                { // begin else if
                   if (tokens.peek().equals("n"))
                   { // begin if
                      tokens.pop();
                      states.pop();
                      tokens.push("F");
                      if (states.peek() == 0 || states.peek() == 4 || states.peek() == 6)
                         states.push(3);
                      else if (states.peek() == 7)
                         states.push(10);
                      return true;
                   } // end if
     
                   else
                   {// begin else
                      System.out.println("Error!");
                      return false;
     
                   } // end else
     
                } // end else if
     
                else if (number == 10)
                { // begin else if
                   if (tokens.peek().equals("F"))
                   { // begin if
                      tokens.pop();
                      states.pop();
                      if (tokens.peek().equals("*"))
                      {// begin if
                         tokens.pop();
                         states.pop();
                         if (tokens.peek().equals("T"))
                         { // begin if
                            states.pop();
                            tokens.pop();
                            tokens.push("T");
                            if (states.peek() == 0 || states.peek() == 4)
                               states.push(2);
                            else if (states.peek() == 6)
                               states.push(9);
                            return true;
     
                         } // end if
     
                         else
                         { // begin else
                            System.out.println("Error!");
                            return false;
                         } // end else
     
     
     
                      } // end if
     
                      else
                      { // begin else
                         System.out.println("Error!");
                         return false;
     
                      } // end else
                   } // end if
     
                   else
                   { // begin else
                      System.out.println("Error!");
                      return false;
                   } // end else
     
                } // end else if
     
                else if (number == 11)
                { // begin else if
                   if (tokens.peek().equals(")"))
                   { // begin if
                      states.pop();
                      tokens.pop();
                      if (tokens.peek().equals("E"))
                      { // begin if
                         states.pop();
                         tokens.pop();
                         if (tokens.peek().equals("("))
                         { // begin if
                            states.pop();
                            tokens.pop();
                            tokens.push("F");
                            if (states.peek() == 0 || states.peek() == 4 || states.peek() == 6)
                               states.push(3);
                            else if (states.peek() == 7)
                               states.push(10);
                            return true;
                         } // end if
     
                         else
                         { // begin else
                            System.out.println("Error!");
                            return false;
                         } // end else
                      } // end if
     
                      else
                      { // begin else
                         System.out.println("Error!");
                         return false;
                      } // end else
                   } // end if
     
                   else
                   { // begin else
                      System.out.println("Error!");
                      return false;
                   } // end else
     
                } // end else if
     
                else
                { // begin else
                   System.out.println("Error!");
                   return false;
                } // end else
             } // end else if
     
             else if (token.equals("("))
             { // begin else if
     
                if (number == 2 || number == 4 || number == 6 || number == 7)
                { // begin if
                   states.push(4);
                   tokens.push("*");
                   setString(TokenHandler.deleteToken(getString()));
                   return true;
     
                } // end if
     
                else
                { // begin else
                   System.out.println("Error!");
                   return false;
                } // end else
     
             } // end else if
     
             else if (token.equals(")"))
             { // begin else if
     
                if (number == 2)
                { // begin if
                   if (tokens.peek().equals("T"))
                   { // begin if
                      states.pop();
                      tokens.pop();
                      tokens.push("E");
                      if (states.peek() == 0)
                         states.push(1);
                      else if (states.peek() == 4)
                         states.push(8);
                      return true;
                   } // end if
     
                   else
                   { // begin else
                      System.out.println("Error!");
                      return false;
     
                   } // end else
     
                } // end if
     
                else if (number == 3)
                { // begin else if
                   if (tokens.peek().equals("F"))
                   { // begin if
                      tokens.pop();
                      states.pop();
                      tokens.push("T");
                      if (states.peek() == 0 || states.peek() == 4)
                         states.push(2);
                      else if (states.peek() == 6)
                         states.push(9);
                      return true;
                   } // end if
     
                   else
                   { // begin else
                      System.out.println("Error!");
                      return false;
                   } // end else
     
                } // end else if
     
                else if (tokens.peek().equals("n"))
                { // begin else if
                   tokens.pop();
                   states.pop();
                   tokens.push("F");
                   if (states.peek() == 0 || states.peek() == 4 || states.peek() ==6)
                      states.push(3);
                   else if (states.peek() == 7)
                      states.push(10);
                   return true;
                } // end else if 
     
                else if (number == 8)
                { // begin else if
                   tokens.push(")");
                   states.push(11);
                   setString(TokenHandler.deleteToken(getString()));
                   return true;
                } // end else if
     
                else if (number == 9)
                {
                   if (tokens.peek().equals("T"))
                   { // begin if
                      tokens.pop();
                      states.pop();
     
                      if (tokens.peek().equals("+"))
                      { // begin if
                         tokens.pop();
                         states.pop();
                         if (tokens.peek().equals("E"))
                         { // begin if
                            tokens.pop();
                            states.pop();
                            tokens.push("E");
                            if (states.peek() == 0)
                               states.push(1);
                            else if (states.peek() == 4)
                               states.push(8);
                            return true;
     
                         } // end if 
     
                         else
                         { // begin else
                            System.out.println("Error!");
                            return false;
                         } // end else
                      } // end if 
     
                      else
                      { // begin else
                         System.out.println("Error!");
                         return false;
                      } // end else
     
                   } // end if
     
                   else
                   { // begin else
                      System.out.println("Error!");
                      return false;
     
                   } // end else
     
     
                }
     
                else if (number == 10)
                {
                   if (tokens.peek().equals("F"))
                   {
                      tokens.pop();
                      states.pop();
                      if (tokens.peek().equals("*"))
                      {
                         tokens.pop();
                         states.pop();
                         if (tokens.peek().equals("T"))
                         {
                            tokens.pop();
                            states.pop();
                            tokens.push("T");
                            if(states.peek() == 0 || states.peek() == 4)
                               states.push(2);
                            else if (states.peek() == 6)
                               states.push(9);
                            return true;
     
                         }
     
                         else
                         { // begin else
                            System.out.println("Error!");
                            return false;
     
                         } // end else
     
                      }
     
                      else
                      { // begin else
                         System.out.println("Error!");
                         return false;
     
                      } // end else
     
     
                   }
     
                   else
                   { // begin else
                      System.out.println("Error!");
                      return false;
     
                   } // end else
     
                }
     
                else if (number == 11)
                {
                   if (tokens.peek().equals(")"))
                   {
                      tokens.pop();
                      states.pop();
                      if (tokens.peek().equals("E"))
                      {
                         tokens.pop();
                         states.pop();
                         if(tokens.peek().equals("("))
                         {
                            tokens.pop();
                            states.pop();
                            tokens.push("F");
                            if (states.peek() == 0 || states.peek() == 4 || states.peek() == 6)
                               states.push(3);
                            else if (states.peek() == 7)
                               states.push(10);
                            return true;
                         }
     
                         else
                         { // begin else
                            System.out.println("Error!");
                            return false;
     
                         } // end else
                      }
     
                      else
                      { // begin else
                         System.out.println("Error!");
                         return false;
     
                      } // end else
     
                   }
     
                   else
                   { // begin else
                      System.out.println("Error!");
                      return false;
     
                   } // end else
     
                }
     
                else
                { // begin else
                   System.out.println("Error!");
                   return false;
     
                } // end else
     
     
             } // end else if
     
             else if (token.equals("$"))
             { // begin else if
     
                if (number == 1)
                { // begin if
                   System.out.println("Accepted!");
                   return true;
                } // end if
     
                else if (number == 2)
                { // begin  else if
                   if (tokens.peek().equals("T"))
                   { // begin if
                      states.pop();
                      tokens.pop();
                      tokens.push("E");
                      if (states.peek() == 0)
                         states.push(1);
                      else if (states.peek() == 4)
                         states.push(8);
                      return true;
                   } // end if
     
                   else
                   { // begin else
                      System.out.println("Error!");
                      return false;
     
                   } // end else
     
                } // end if
     
                else if (number == 3)
                { // begin else if
                   if (tokens.peek().equals("F"))
                   { // begin if
                      tokens.pop();
                      states.pop();
                      tokens.push("T");
                      if (states.peek() == 0 || states.peek() == 4)
                         states.push(2);
                      else if (states.peek() == 6)
                         states.push(9);
                      return true;
                   } // end if
     
                   else
                   { // begin else
                      System.out.println("Error!");
                      return false;
                   } // end else
     
                } // end else if
     
                else if (tokens.peek().equals("n"))
                { // begin else if
                   tokens.pop();
                   states.pop();
                   tokens.push("F");
                   if (states.peek() == 0 || states.peek() == 4 || states.peek() ==6)
                      states.push(3);
                   else if (states.peek() == 7)
                      states.push(10);
                   return true;
                } // end else if 
     
                else if (number == 9)
                {
                   if (tokens.peek().equals("T"))
                   { // begin if
                      tokens.pop();
                      states.pop();
     
                      if (tokens.peek().equals("+"))
                      { // begin if
                         tokens.pop();
                         states.pop();
                         if (tokens.peek().equals("E"))
                         { // begin if
                            tokens.pop();
                            states.pop();
                            tokens.push("E");
                            if (states.peek() == 0)
                               states.push(1);
                            else if (states.peek() == 4)
                               states.push(8);
                            return true;
     
                         } // end if 
     
                         else
                         { // begin else
                            System.out.println("Error!");
                            return false;
                         } // end else
                      } // end if 
     
                      else
                      { // begin else
                         System.out.println("Error!");
                         return false;
                      } // end else
     
                   } // end if
     
                   else
                   { // begin else
                      System.out.println("Error!");
                      return false;
     
                   } // end else
     
     
                }
     
                else if (number == 10)
                {
                   if (tokens.peek().equals("F"))
                   {
                      tokens.pop();
                      states.pop();
                      if (tokens.peek().equals("*"))
                      {
                         tokens.pop();
                         states.pop();
                         if (tokens.peek().equals("T"))
                         {
                            tokens.pop();
                            states.pop();
                            tokens.push("T");
                            if(states.peek() == 0 || states.peek() == 4)
                               states.push(2);
                            else if (states.peek() == 6)
                               states.push(9);
                            return true;
     
                         }
     
                         else
                         { // begin else
                            System.out.println("Error!");
                            return false;
     
                         } // end else
     
                      }
     
                      else
                      { // begin else
                         System.out.println("Error!");
                         return false;
     
                      } // end else
     
     
                   }
     
                   else
                   { // begin else
                      System.out.println("Error!");
                      return false;
     
                   } // end else
     
                }
     
                else if (number == 11)
                {
                   if (tokens.peek().equals(")"))
                   {
                      tokens.pop();
                      states.pop();
                      if (tokens.peek().equals("E"))
                      {
                         tokens.pop();
                         states.pop();
                         if(tokens.peek().equals("("))
                         {
                            tokens.pop();
                            states.pop();
                            tokens.push("F");
                            if (states.peek() == 0 || states.peek() == 4 || states.peek() == 6)
                               states.push(3);
                            else if (states.peek() == 7)
                               states.push(10);
                            return true;
                         }
     
                         else
                         { // begin else
                            System.out.println("Error!");
                            return false;
     
                         } // end else
                      }
     
                      else
                      { // begin else
                         System.out.println("Error!");
                         return false;
     
                      } // end else
     
                   }
     
                   else
                   { // begin else
                      System.out.println("Error!");
                      return false;
     
                   } // end else
     
                }
     
                else
                { // begin else
                   System.out.println("Error!");
                   return false;
     
                } // end else
     
             } // end else if
     
             else
             { // begin else
                System.out.println("Error!");
                return false;
             } // end else
     
          } // end method
          public static void main(String[] args)
          {
             LR1Parser parser;
             String stuffs = "123+5";
             String stuffs2 = TokenHandler.deleteToken(stuffs);
             System.out.println(stuffs2);
     
             try
             {
                if (!(args[0].equals("$")))
                {
                   parser = new LR1Parser(args[0] + "$");
                }
     
                else
                {
                   System.out.println("Is valid.");  // if it's only a $, used to avoid a problem of non-validity for args[0] = "$"
                }
                 // parser = new LL1Parser("+123");
             }
     
                catch(ArrayIndexOutOfBoundsException aioobe)
                {
                   System.out.println("args[0] doesn't exist!");
                   System.exit(0);
                }
     
     
     
     
          }
     
     
     
       }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to change a String value into a number and then back into a String.

    A regular expression and replaceAll should do it.
    Improving the world one idiot at a time!

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to change a String value into a number and then back into a String.

    How would you do that? I had thought of going through with a while loop and changing it.

    How would you change it with a regular expression?

    I'm not that familiar with regex.

    I'm not sure how to set the regex as any number

    it could be 1, 123, 934, 13024, etc. How do I get it to do all of those?

    regex only works for single characters and I only want 1 n for numbers like 1234.
    Last edited by javapenguin; November 17th, 2011 at 10:55 PM.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to change a String value into a number and then back into a String.

    Quote Originally Posted by javapenguin View Post
    How would you do that?
    I already told you.

    I had thought of going through with a while loop and changing it.
    You could but that would take a bit of coding, whereas my suggestion is a one liner.

    How would you change it with a regular expression?

    I'm not that familiar with regex.
    Then you should learn. There is an excellent tutorial to be found if you Google "regex tutorial".

    I'm not sure how to set the regex as any number

    it could be 1, 123, 934, 13024, etc. How do I get it to do all of those?
    The regex tutorial will teach you.

    regex only works for single characters and I only want 1 n for numbers like 1234.
    Who told you that? It is completely wrong.
    Improving the world one idiot at a time!

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How to change a String value into a number and then back into a String.

    @Junky: As far as i take this problem, OP means to keep record of Strings(characters) replaced by "n".

    Am i right javapenguin?
    Forexample:
    123+5*3
    will be converted to
    n+n*n

    And then again OP wants to give
    n+n*n
    and get
    123+5*3

  6. #6
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: How to change a String value into a number and then back into a String.

    That can be done with regex.\

    Here is my example program I made a while ago to test regexs.

    package regex.standalone;
     
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
     
    import java.util.Scanner;
    /**
     * Tests basic regex in java.
     * @author TJStretch
     */
    public class RegexTestHarness 
    {
      public static void main(String[] args)
      {
        Scanner in = new Scanner(System.in);
     
        System.out.println("Welcome to the first regex test by TJStretch");
        boolean cont = true;
        Pattern pattern;
        Matcher matcher;
        boolean found;
        String regex, toSearch;
        while(cont)
        {
          System.out.println("What is the regex pattern?");
          regex = in.nextLine();
     
          System.out.println("What is the string to be searched?");
          toSearch = in.nextLine();
     
          pattern = Pattern.compile(regex);
          matcher = pattern.matcher(toSearch);
          found = false;
          while(matcher.find())
          {
            System.out.printf("Text Found: \"%s\" starting at index %d, ending at index %d.", matcher.group(), matcher.start(), matcher.end());
            System.out.println();
            found = true;
          }
          if(!found)
          {
            System.out.println("No text found.");
          }
          System.out.println("Again?");
          cont = in.nextLine().toLowerCase().startsWith("y");
        }
      }
    }
    If you use \d+ for the regex, it finds all the numbers and there index, which you can replace and store.

    I entered \d+ for regex, and 123+5*3 for the string to search, and here is the output
     
    Text Found: "123" starting at index 0, ending at index 3.
    Text Found: "5" starting at index 4, ending at index 5.
    Text Found: "3" starting at index 6, ending at index 7.

    (I used http://download.oracle.com/javase/tu...t_harness.html to assist in making it)

    First search with regex and remember the starting index, because that will be where the n is when it is replaced, and save the old number to an array. When converting back find all the n's at the saved index's through the saved index+1 and replace them with the correct number. (If you do not want to change another old n's.

    If you know n's will never be in the original equation, then for the first one loop through all the numbers and save each number to an array, then when your looping through n's the second time, replace from the oldest in the array to the newest.
    Last edited by Tjstretch; November 18th, 2011 at 02:02 PM.

Similar Threads

  1. Replies: 5
    Last Post: April 22nd, 2013, 07:27 AM
  2. [SOLVED] Problem using the length of a string to bound the number of iterations of a for loop
    By dtitt3 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 3rd, 2011, 01:44 PM
  3. [SOLVED] difference between String Concatenation and String -Buffer/Builder .append(<value>)
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 3rd, 2011, 08:16 AM
  4. How to place a number in front of a string?!!
    By WantHelp in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 8th, 2011, 11:15 PM
  5. [SOLVED] How to string a decimal number in Java?
    By Lizard in forum Loops & Control Statements
    Replies: 6
    Last Post: May 14th, 2009, 03:59 PM