Home > categories > Machinery & Equipment > Crusher > How can I print an array of strings to the console? 10 PTS!!!!!?
Question:

How can I print an array of strings to the console? 10 PTS!!!!!?

I want to print String[] bowlers = {quot;Rockyquot;, quot;Strikerquot;,quot;Alley Catquot;, quot;Splitserquot;, quot;Crusherquot;, quot;Spare Bearquot;};an array of 5 elements as you can see to the console using a for loop but what I'm doing gives me an arrayindexoutofboundsexception error....how can I fix it? I want to print each element on a separate lineHere's my code for (i=0; ilt;bowlers.length; i++);names =bowlers[0];System.out.print(names);

Answer:

my sought after time of day is first easy. it is so exceptionally, and that's a beginning of an afternoon. and waking up early is my forte. Plus, i'm on my own, so i continually stand infront of my abode and in simple terms watch the solar for a jointly as. My sought after season is late fall. this is extremely cool, and not too chilly or warm. and the leaves are extremely exceptionally. luv happening walks that factor of three hundred and sixty 5 days. plus, there is halloween! :) My sought after climate is approximately 5-10 levels, and somewhat raining. Luv the rain, yet while this is pouring, i'm getting way too moist. and that i hate the nice and snug, yet i do no longer elect to positioned on a great coat interior the chilly the two. at 5-10 levels, i will flow exterior in denims and a t-shirt.
You need to get rid of the semicolon at the end of the for line and enclose the two statements below in {} braces to group them as a block to be the for loop body. You should use that index variable i to select the string to be printed, too, otherwise you;ll just print Rocky over and over. Finally, I'd declare the names variable as a String inside the loop. That makes it local to the {} braced block and won't interfere with any other use of the variable names in your program: //doofus code deleted...see below ...and that should be indented, of course, but Y!A won't preserve spaces. Edit: Yikes! Sorry, I somehow misread Java as C++, and then didn't even wake up when copy/pasting the System.out.println() call!? Sorry, Make that loop: for (int i=0; i<bolwers.length; ++i) { String name = bowlers[i]; System.out.println(name); } ...or, with Java 6 or later you can use the for each loop form: for (String name : bowlers) { System.out.println(name); } That steps through the array (or other collection) one item at a time, so you don't have to do the counting and indexing. On each iteration of the loop, the loop variable name gets assigned to a different array element, and they will be processed in array index order. I'd recommend that any time you don't need the array index for some other purpose, and the natural ordering of the collection is okay. (To display the entries in reverse order, you're back to using an index variable.) Sorry about the Java/C++ mixup.
Here is how (where you can change the YourClassName for your purposes). Hope it helps: public class YourClassName { public static void main(String[] args) { String[] bowlers = {Rocky, Striker,Alley Cat, Splitser, Crusher, Spare Bear}; for (int i=0; i<bowlers.length; i++) { System.out.println(bowlers[i]); } } }

Share to: