• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays - How do I show the array index postion?  RSS feed

 
Heather Carey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the program below, the output only displays the Transaction amount, but not the index position in my array. In this section immediately below, what can I do to not only show the amount of the transaction, but where it is in the index of the array.

i.e Transaction 5: 100.00.

Right now it just prints...

Transaction 100.0

//Present each transaction
for (int i=0; i<transactions.length; i++) {
System.out.println ("Transaction " + transactions[i]);


--------------------------------------…
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Remember i will start at 0.
 
Heather Carey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EDITED: Never mind. My post below doesn't work.

--------------------------------------------------
Could I change it to:

System.out.println ("Transaction " + i + transactions[i]);

Thanks,

Heather
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heather

Why doesn't it work?
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried + i + 1, even though I know that won’t work? You do need to explain the problem, otherwise we can’t halp.

And welcome to the Ranch
 
Heather Carey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure why my last suggestion wouldn't work. When I entered the code and ran it, it gave me numbers that didn't seem to be double. More like Hex numbers instead.

Next I have to figure out how to make an array sort numbers, but I wanted to try and invoke the sort method (away from the main), but I'll look through the forums to see if anyone had a similar problem.

Thanks,
Heather
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what did your hex numbers look like? Please show us the code which gave hex numbers? Are you trying to print the whole array? You get hex numbers if you do that, for reasons you will find out if you read about the toString() method.
 
Heather Carey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I ran it the way I wrote it, the numbers became four wide and had a letter and - in the set.

i.e. 24-23 or 1F45

I could try to reproduce what i did, but I already fixed it with the other response. I most likely typed it in wrong from what I thought I put.

Someone told me that I could have used () around my +i and increased it by 1 so that the transaction count started at 1 instead of 0 (1 +1).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!