It's not a secret anymore!*
The moose likes Java in General and the fly likes charAt with longs? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "charAt with longs?" Watch "charAt with longs?" New topic
Author

charAt with longs?

Peter Shipway
Ranch Hand

Joined: Mar 25, 2004
Posts: 71
I am trying to make a program that validates ABN numbers however I have to check each number but i dont know how to do this from a long. Can i use a charAt method? Any help would be great ty all.
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
Australian Business Number?

If you are reading the ABN number as a String (with spaces), you can use charAt() easy enough.

If you are reading it as a long, you can write a recursive algorithm to recursively divide the number by 1000000000, 100000000, ... , 10. This will extract each individual digit and you can then process this digit however you want. Of course, you can write an iterative version easy enough, and since you are not dealing with a very complex solution, this may be the better way to go.
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
If you are reading it as a long, rather than using recursion you could use the method Long.toString(long) to get a String representation of the long and then do as Jeffrey advises above.

I wasn't sure from the original post if you meant you were indexing a String with a long (ie, using myString.charAt(somelong)). In this case, you can cast your long to an int, making sure to avoid overflow issues. That said, overflow shoudn't be a problem seeing the maximum value of an int is vastly greater than the length of the longest string you could store in memory.

Cheers,


--Tim
[ May 16, 2004: Message edited by: Tim West ]
Peter Shipway
Ranch Hand

Joined: Mar 25, 2004
Posts: 71
Ty for the help guyz as usal i have attempted to take the easiest way and everything appears to work except this;

while (num != null)

I am using this to handle the spaces i dont want them to go through the process at all, so how do i repesent this within a int? I havent been able to find an answer my full code is below (btw im using this as a serpate class to my gui) first attempt at a profesional program



Also whislt im at it I will describe the method to check for ABN (Australian Business Number)

For example 53 004 085 616

first take 1 from the first number
43 004 085 616
now apply a weigthing factor besides the first one the pattern is the old number plus 2 eg.

(4x10)+(3x1)+(0x3)+(0x5)+(4x7)+(0x9)+(8x11)+(5x13)+(6x15)+(1x17)+(6x19) =445
Then mod this by 89 and see if there is any remainder if there isnt then it its a valid ABN Number

445/89 = 5 remainder 0
[ May 18, 2004: Message edited by: Peter Shipway ]
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
You can check for spaces by using the isWhiteSpace(char c) method in the Character class. Check out the Java API for more details.
Peter Shipway
Ranch Hand

Joined: Mar 25, 2004
Posts: 71
How do i use isWhiteSpace in a while loop to show that it isnt white space? Im sorry but i dont fully understand, does isWhiteSpace only do something if the char is empty?
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
isWhiteSpace(char c) will return a Boolean if c is whitespace (as defined by Java). So, among other things, this will catch spaces, carriage returns, etc.

For example, using your existing code:


Change the inner while loop to a for, this way, you have one while loop responsible for controlling the iteration through the string. Remember, even though Java will not complain when mixing int and char datatypes, you have to be careful. For instance, if I read the character '4', and store it in an int, the int value will not be '4'. It will be the Unicode equivalent of the character '4' (52). For this reason, you need to get the integer value of the character (Ingeter.parseInt(...)).
[ May 18, 2004: Message edited by: Jeffrey Hunter ]
Peter Shipway
Ranch Hand

Joined: Mar 25, 2004
Posts: 71
Eh guyz im still getting error with the

!Character.isWhiteSpace(num)

line complier says it cant resolve symbol, any help this is really getting to me.
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
Your original code had:

But the new code has changed num to be a char. isWhiteSpace will only work on char, not int!


The nice thing about Standards is that there are so many to choose from!
Peter Shipway
Ranch Hand

Joined: Mar 25, 2004
Posts: 71
Here is my new code, still no go



Perhaps its in a package that im not importing? I have been using TerminalIO and BrezzySwing so far so i dont know what other pacakges there are any help would be awsome ty for the help soo far. I am soo close but soo far
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Perhaps if you use isWhitespace() rather than isWhiteSpace that would help. Note that the compiler's error message usually identifies which symbol it can't identify, and checking the spelling of the symbol you're using is one of the first things you should do in a case like this.


"I'm not back." - Bill Harding, Twister
Peter Shipway
Ranch Hand

Joined: Mar 25, 2004
Posts: 71
Yikes spelling error i feel like such a newb. Ty for the help all u all deserve a big java flavoured cookie . Hopefully i learn to spell check in future...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: charAt with longs?
 
Similar Threads
What does this error mean??
Compare() - from Master Exam
Symbol can't be found
First element in string
Need String program