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.
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 wrote:How would I have the users enter information?
You can use the Scanner class for that
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
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"
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 wrote:I am trying create a code...
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.