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?
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.