The Artima Developer Community
Sponsored Link

Java Answers Forum
Counting Numbers instead of strings

4 replies on 1 page. Most recent reply: Aug 7, 2003 2:33 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Philip Pompili

Posts: 12
Nickname: ppompili
Registered: Jul, 2003

Counting Numbers instead of strings Posted: Aug 6, 2003 10:01 AM
Reply to this message Reply
Advertisement
Hello, I was wondering how to count how many characters are in a number. I have tried converting the number into a string but it does wierd stuff(like removing zero's in front and stuff) What I need to do is take a row of numbers for example

456841654564

and make sure that when it gets used trhat only the first 8 numbers get used. If this were a string I would use substring, but I cant with this because the number I am dealing with is a long.

Thanks

Phil


Mr Plow

Posts: 18
Nickname: mrplow
Registered: Jun, 2003

Re: Counting Numbers instead of strings Posted: Aug 6, 2003 3:59 PM
Reply to this message Reply
You are on the right track converting the number into a String in order to extract the first eight characters. However I am wondering if you are running into problems, because you are entering numbers in Octal format without realizing it since you refer to the fact that the program is removing a lead zero.

For example of how octal (base 8) works here is a simple program:
public class OctalOutput
{
  public static void main(String[] args)
  {
    long l = 011;
	System.out.println(l);
  }
  
};


If you aren't aware that a lead 0 causes java to interpret the number as an octal number, then you may expect that the output of this program would be 11. But since java interprets 011 as being octal, the out put is 9 [(8 to the 0 power times 1) + (8 to the first power times 1)]

Philip Pompili

Posts: 12
Nickname: ppompili
Registered: Jul, 2003

Re: Counting Numbers instead of strings Posted: Aug 6, 2003 5:01 PM
Reply to this message Reply
I am sorry I am really confused now. I know that I said eight characters but I am not sure that I want octal output. I just want 8 characters to be displayed. so for example if a user enters
01234567890

I want only 01234567 to be displayed.

When I try to convert to a string however the lead zero is taken off, but I need that zero in my program.

Thank You

Mr Plow

Posts: 18
Nickname: mrplow
Registered: Jun, 2003

Re: Counting Numbers instead of strings Posted: Aug 6, 2003 10:01 PM
Reply to this message Reply
Ok, I will do my best to explain but to get a good understanding you will probably have to do some reading on your own.

First of all, it isn't "octal output" that you are getting. Rather the opposite, by assigning a value to a numeric variable that begins with a '0' you are signifying to java that the input is in octal format.

All octal format means is that the number system uses a base 8 counting system. The number system we usually use is a base 10 system. That is to say the number 137 is normally represented as (10 raised to the 2nd power * 1) + (10 raised to the 1st power * 3) (10 raised to the zero power * 7) which is (100 * 1) + (10 * 3) + (1 * 7) which is 100 + 30 + 7 or 137.

Now if we were going to represent the number 137 in octal format (i.e. base 8) then we would use (8 raised to the 2nd power * 2) + (8 raised to the 1st power * 1) + (8 raised to the zero power * 1) which is (64 * 2) + (8 * 1) + (1 * 1). Therefore in octal format the number 137 can be represented as 211.

So the issue becomes how do you signify to java that the number 211 is in octal and is actually the number 137 (in base 10). The way you do this is with a preceding '0' character. Note that there are other bases you can use. For instance hexadecimal is common and it uses a base 16 and the prefix 0x.

Therefore you aren't losing the '0' when you try to convert the long into a String. Rather the '0' is being "used" when you assign the value to the long.

I guess the real issue is why are you bothering with treating your input as a long at all? Wouldn't it make much more sense to treat it as a String, since that is really what it is?

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Counting Numbers instead of strings Posted: Aug 7, 2003 2:33 PM
Reply to this message Reply
If a "user input" some string of numbers, then it is already a String, not a long. Therefore all you need to do is keep the first 8 characters of that string (and convert that to long, or not, as you wish). You may want to do other things like remove spaces, commas, etc., as well, by the way.

Maybe you could show a little of your code to demonstrate exactly what your problem is. If you do, please use the java tags explained in the gray Formatting Your Post box, while you are composing the post.

Flat View: This topic has 4 replies on 1 page
Topic: Forms Previous Topic   Next Topic Topic: SQL Question

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use