0

I'm trying to make a program that lets you input any one-step equation, and solve for it. To make it so that you can enter it in any way, I have a for loop running that checks every character to see what it is. But then, I realized that I could not use double digit numbers for this. So I tried adding a little function that checks if there is a character one to the right, to test if it is a number. And if it is a number, combine that plus the one it found, to be a single number. Like if it sees 1, and checks to the right that there is a 0 there, it give you 10. Here is the code for this function

        Find = equation.charAt(i);

        if (Character.isDigit(Find))
        {
            if(Found == 0)
            {
                if(i < equation.length() - 1)
                {
                    FindNext = equation.charAt(i + 1);
                    if (Character.isDigit(FindNext))
                    {
                        one = (Character.toString(Find) + Character.toString(FindNext));
                        Found = 2;
                        One = Double.parseDouble(one);
                    }
                }
                else
                {
                    one = (Character.toString(Find));
                    Found = 2;
                    One = Double.parseDouble(one);
                }
            }
            else
            {
                if(i + 1 < equation.length() - 1)
                {
                    FindNext = equation.charAt(i + 1);
                    if (Character.isDigit(FindNext))
                    {
                        two = (Character.toString(Find) + Character.toString(FindNext));
                        Two = Double.parseDouble(one);
                    }
                }
                else
                {
                    two = (Character.toString(Find));
                    Two = Double.parseDouble(two);
                }
            }
        }

Does anyone see what I'm doing wrong?

Sam
  • 7,252
  • 16
  • 46
  • 65
The_Steve13
  • 119
  • 2
  • 11

1 Answers1

3

And if the number is 100? 1000? 10000? ...you get the point. You cannot simply check one character ahead. You should instead separate your parsing by operations you support (i.e. +, -, *, etc) and it will be much easier to obtain the numbers.

cklab
  • 3,761
  • 20
  • 29
  • Could you give me an example? I'm still new-ish to java. I don't know much about separation by parsing. – The_Steve13 Jun 18 '12 at 16:29
  • Is this homework? Look into String.indexOf() and String.split(). You want to find where your operation is in order to find what your numbers are. Keep in mind, this only works because you have such a simple case. Once you start introducing multiple operations, parenthesis, order of operations, etc, you will be much better off using a library or looking into parsers (see http://stackoverflow.com/questions/28256/equation-expression-parser-with-precedence). – cklab Jun 18 '12 at 17:05
  • Ok. Thanks. I'll look into that kinda stuff. And no, if you are wondering, this is not homework. I'm just bored. – The_Steve13 Jun 18 '12 at 18:31