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 5 of 5

Thread: Creating a number char by char

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Stressed
    Thanks
    2
    Thanked 1 Time in 1 Post

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

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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating a number char by char

    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?

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Stressed
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating a number char by char

    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.
    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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Creating a number char by char

    Hello.
    Please post your entire code.
    Also, tell us what "lc" is doing in the method you posted? When is it true and when is it false?

    Syed.

Similar Threads

  1. Replies: 3
    Last Post: March 23rd, 2013, 07:20 PM
  2. Get int value from char, char pulled from String
    By Andrew Red in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2013, 10:04 AM
  3. char.toHexString();
    By SPACE MONKEY in forum What's Wrong With My Code?
    Replies: 16
    Last Post: February 28th, 2011, 08:46 AM
  4. Char cannot be dereferenced!! Please help
    By humdinger in forum Object Oriented Programming
    Replies: 5
    Last Post: February 14th, 2010, 03:18 PM
  5. char (toUppercase?)
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: December 12th, 2009, 10:01 AM