I can study java code but i have a problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I can study java code but i have a problem

i can do individual code but how would i put for example a do while loop into a regular array? i have trouble putting code together without a bunch of errors. unfortunatley i don't know what the errors mean.

24th Jul 2017, 2:52 AM
Whitehat
Whitehat - avatar
7 Answers
+ 2
The best form of learning when it comes to coding is practice. Writing your own programs will help you study. As far as a do while in an array, that question is to vague. But i'll assume you just mean filling the array with values from, lets say, 0-9. This is with the do-while loop (Keep in mind, you should actually just use a for loop for this). int index = 0; int[] array = new int[10]; do{ array[index++] = index; // fills the array }while(index <= 9); Once you get the hang of these loops, it'll look like a piece of cake (only with far less calories). Keep Trying! 😀
24th Jul 2017, 3:05 AM
Rrestoring faith
Rrestoring faith - avatar
+ 2
Your code seems to work as it is, but I am assuming based on your post that you were trying to implement a do while loop. I would say a for:each loop looks a lot cleaner for what you are trying to do, so I implemented both that and a do while loop in the code below. public class Background { public static void main(String []args){ String [] mynames = {"A","B","C","D","E","F"}; /*System.out.println(mynames[3]); System.out.println(mynames[0]); System.out.println(mynames[3]);*/ for(String name : mynames){ System.out.println(name); } int x = 0; do{ System.out.println(mynames[x]); x++; } while(x < mynames.length); } } If you have any other question I would be happy to answer.
24th Jul 2017, 3:10 AM
Gabe Vogel
Gabe Vogel - avatar
+ 2
Here is code for creating an array in a do-while as well: public class Background { public static void main(String []args){ String [] mynames = {"A","B","C","D","E","F"}; String alpha = "abcdefghijklmnopqrstuvwxyz"; char[] mynames1 = new char[26]; int i = 0; do{ mynames1[i] = alpha.charAt(i); i++; } while(i < mynames1.length); /*System.out.println(mynames[3]); System.out.println(mynames[0]); System.out.println(mynames[3]);*/ for(char name : mynames1){ System.out.println(name); } int x = 0; do{ System.out.println(mynames[x]); x++; } while(x < mynames.length); } }
24th Jul 2017, 3:16 AM
Gabe Vogel
Gabe Vogel - avatar
+ 1
Could you please provide an example of the code you are having difficulties with? I am not quite sure what you mean by putting "a do while loop into a regular array", as an array is a data type and can not contain loops. If you post code, however, I would be happy to help sort out whatever errors you are running into.
24th Jul 2017, 3:02 AM
Gabe Vogel
Gabe Vogel - avatar
0
yes but how do i create a sprite
24th Jul 2017, 3:06 AM
Whitehat
Whitehat - avatar
0
whats your name restoring faith
24th Jul 2017, 3:10 AM
Whitehat
Whitehat - avatar