File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Cattle Drive and the fly likes Evaluation of array positions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Evaluation of array positions" Watch "Evaluation of array positions" New topic

Evaluation of array positions

Herbert Morriss

Joined: May 21, 2005
Posts: 16
I am getting strang results when I use an expression to calculate the array position.

System.out.println( numbersTeens[numberIn % 10] ) ; Works Fine


System.out.println( numbersTens[( ( numberIn/100 ) % 10)]

always evaluates to numbersTens[0].
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11920

what kind of values are you getting for numberIn? if it's anything less that 100, then

numberIn / 100

WILL always be 0, and so the mod 10 will be 0.

are you using numbers > 100?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Herbert Morriss

Joined: May 21, 2005
Posts: 16
Maybe a little more code and output would help

[ I removed the code - Pauline ]

here's the output:

numberIn = 69

Sixty-Nine <---- right
Zero-Nine <---- wrong
Press any key to continue . . .

I hope the formatting comes out...

[Sorry to have to do that Herbert, it's just in case anyone working on this one would prefer not to see others' code before they figure it out themselves. -Pauline ]
[ April 12, 2006: Message edited by: Pauline McNamara ]
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Herbert,

Welcome to Cattle Drive!

The two expressions are different. One is ((numberIn / 10) % 10) and the other one is ((numberIn / 100) % 10).

In the second expression, (69 / 100) will result in 0.

[ April 10, 2006: Message edited by: Joyce Lee ]
Herbert Morriss

Joined: May 21, 2005
Posts: 16
Thanks Joyce, I should have caught that one...

Have a nice day..

subject: Evaluation of array positions
It's not a secret anymore!