• Post Reply Bookmark Topic Watch Topic
  • New Topic

Output individual elements in an array  RSS feed

 
bean miller
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am new here and new to java (began two weeks ago).

I wanted to know if it is possible to have an array and print individual elements one at a time?

instead of printing every number, I can print one element in the list. I looked everywhere if I could do this but I couldn't find anything else. I am trying to build something where a user will enter an array index they want to be viewed and the number/name corresponding to the index will print.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.
An individual array element can be accessed with it's index.
e.g.

names[0] evaluates to "Tom"
names[2] evaluates to Harry
As you can see the index is 0 based (and not 1)

Does this help or did you mean something else?
 
bean miller
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant when a user selects a number and it outputs the corresponding array element. if I wanted to output an array like this: System.out.println(names[i]); but this output is not part of a for loop.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if I wanted to output an array like this: System.out.println(names[i]); but this output is not part of a for loop

You don't need any loop to access the element by it's index.

In pseudo code.
1) Construct/generate the array
2) Ask the user for the index
3) Check the index is valid (e.g. asking for the 20th element from an array of 10 elements is invalid). Inform the user accordingly and prompt for a new index
4) Retrieve the element from the index and print/process it
 
bean miller
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for #4, everytime it tries to print system.out.println(arrayname[i]); i receive an error stating it is out of bounds even though it is within bounds. I thought the i would correspond to the user's input unless I am suppose to declare it as so.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bean miller wrote:for #4, everytime it tries to print system.out.println(arrayname[i]); i receive an error stating it is out of bounds even though it is within bounds. I thought the i would correspond to the user's input unless I am suppose to declare it as so.

Did you skip #3?
 
bean miller
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure if I did. I did mines differently where I checked of the user input is equal to the input of the day. I will see how to do that
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to show us your code so we can help you figure out what's going wrong. Just dont forget to UseCodeTags
 
bean miller
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am trying create a code where a user will write an event for each of the seven days. and at the end, the user will enter a day and the output should be the event that corresponds with the day. The second part of the code is the part I am having trouble with
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is Console? Is it one of your custom class? The built on Console class in Java does not have any readString() or readInt() method
 
bean miller
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
console is the custom class. it's not supposed to have an readString(), method. How would I have the users enter information?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bean miller wrote:How would I have the users enter information?

You can use the Scanner class for that
http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
 
bean miller
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying everything but I am not sure how to check if index is valid. I put down


but of course, that is giving me a new error. I've been trying to solve this since monday. how sad.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bean miller wrote:
but of course, that is giving me a new error. I've been trying to solve this since monday. how sad.

Well, since we don't have a crystal ball, you need to TellTheDetails
 
bean miller
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After



the compiler stops running. But when I enter a number less than three like 2 it returns a java.lang.NumberFormatException: For input string: "2"
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry. Cant help you there since, like I mentioned before, I do not know what the Console class is.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume Console is a utility class using pre‑Java5 programming, using a BufferedReader. Why on earth you should get a NumberFormatException, I do not know.

Either the compiler stops running, or you get an Exception. You cannot do both. If those things happen, it would be at different times. Did you mean the JVM completes normally when you said compiler?
 
bean miller
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't worry about it, I solved the problem. I was busy learning the advanced stuff and not understanding the simple stuff first.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bean miller wrote:I am trying create a code...

bean,

Please DontWriteLongLines. It makes your thread very hard to read, and it's actually bad coding practice.
I've broken yours up this time, but for future reference, please remember:
80 characters max.
(the SSCCE page actually recommends 62)
And that includes string literals AND comments AND long method calls.

Thanks.

Winston
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how did you solve it? Please tell us.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!