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.
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 linebut I can't seem to get it working. number is a long.number = number + (num * (long) Math.pow(.1, decEnc)); // add to number in correct decimal place