# Creating a number char by char

• September 22nd, 2013, 07:36 PM
Herah
Creating a number char by char
I am writing a method that takes in a string of characters, one character at a time. I am forming the combination of number, decimal point and exponent into a long and adding it to a stack. This has limits such as the max length of the number, exponent and dec portions. Right now, I have been staring at this so long that I feel like I am making it way more complicated than it needs to be, and the output isn't right. I send it 99.99 and end up with 9 0 9 as separate tokens. Not sure where my problem is and having trouble debugging. Any suggestions would be super appreciated.

Code :

``` public void number(char dig, boolean lc) { if (dig == '.') { isReal = true; return; } else if (dig == 'e') { isReal = true; exp = true; return; } num = Character.getNumericValue(dig); if (isReal) { if (exp) { if(String.valueOf(exponent).trim().length() <= zLength) // fits in exponent limit exponent = (int) (exponent * 10 + num); // add to exponent tracker } else { if(String.valueOf(number).trim().length() <= yLength){ // fits in decimal limit decEnc++; number = number + (num * (long) Math.pow(.1, decEnc)); // add to number in correct decimal place } } } else if (number == 0) { number = num; } else { number = number * 10 + num; } if (lc) { // last character that will be encountered for this token if (isReal) { if(Math.min(number, Math.pow(10, xLength)) == number){ // fits in number limit if (exp) { number = (long) Math.pow(number, exponent); // adds exponent to the number } tokenList.add(number); } } else if (String.valueOf(number).trim().length() <= intLength) {// was an int tokenList.add(number); } else { System.out.println("Error in number entry: " + number); } number = 0; num = 0; decEnc = 0; decimalEncountered = false; isReal = false; exponent = 0; exp = false; } }```

I think my error is in this line
Code :

` number = number + (num * (long) Math.pow(.1, decEnc)); // add to number in correct decimal place`
but I can't seem to get it working. number is a long.
• September 22nd, 2013, 07:47 PM
Norm
Re: Creating a number char by char
Quote:

the output isn't right. I send it 99.99 and end up with 9 0 9 as separate tokens.
what should the output be?

Quote:

I think my error is in this line
I'm not sure what the "error" is. Can you explain? What does that line do? Is the resulting value of number what you expect?

And post the full text of any error message.
• September 22nd, 2013, 07:49 PM
Herah
Re: Creating a number char by char
It's not an eclipse error, it's a math error.

I input 99.99 one char at a time into that method, then print the stack.
It should print 99.99
Instead I get 9 0 9
• September 22nd, 2013, 08:09 PM
Norm
Re: Creating a number char by char
Quote:

it's a math error.
If its a math error, do the equation manually and compare the results you get with what the equation in the program computes. You will need to print out the results of the equation so you can see it.
Quote:

I get 9 0 9
Where is the print statement that prints that?

Do you have testing code that calls the method with the value 99.99 and defines all the variables? Something short that doesn't require user input. Define the variables and call the method.
• September 23rd, 2013, 04:59 AM
syedbhai
Re: Creating a number char by char
Hello.