I wonder how I can set up an String array in java. I need the array so I can store different items per line so each item will be like one record. I tried something like :
String x[] = new String[10];
but when it comes to storing the data and retrieve the is when I struggle. The problem is I know that I can use x.length or other methods available to traverse the array but I believe I could be doing something wrong. Thanks.
Well, once you've declared the String array, you could assign values to array elements individually. Or you could use a loop to do that.
And after all array elements have been assigned their values, you could use a for loop to print them.
In the following Java program, I use both methods - individual assignment as well as assignment inside a loop.
Assignment inside a loop would typically be based on your business logic. Since my program is just a demo, I generated a random string of 20 characters inside a loop and assigned that to each String array element.
$
$
$ cat -n TestStringArray.java
1 import java.util.Random;
2 class TestStringArray {
3 public static void main (String[] args) {
4 String[] x = new String[9];
5 // Now assign values to each array element
6 x[0] = "The ";
7 x[1] = "quick ";
8 x[2] = "brown ";
9 x[3] = "fox ";
10 x[4] = "jumps ";
11 x[5] = "over ";
12 x[6] = "the ";
13 x[7] = "lazy ";
14 x[8] = "dog.";
15 // and print them
16 for (int i=0; i<x.length; i++){
17 System.out.println("Element # "+i+" in string array x => "+x);
18 }
19 System.out.println();
20 // =========================================================================
21 // You could also assign values inside a loop
22 Random rnd = new Random();
23 char[] ch = new char[20];
24 for (int i=0; i<9; i++) {
25 // Create a string of 20 random characters
26 for (int j=0; j<20; j++) {
27 ch[j] = (char)(rnd.nextInt(94)+33);
28 }
29 // convert the character array element to a String and assign it
30 x = new String(ch);
31 }
32 // Now loop through String array and print each element
33 System.out.println("==========================================================\n");
34 for (int i=0; i<x.length; i++){
35 System.out.println("Element # "+i+" in string array x => "+x);
36 }
37 }
38 }
$
$ javac TestStringArray.java
$
$ java TestStringArray
Element # 0 in string array x => The
Element # 1 in string array x => quick
Element # 2 in string array x => brown
Element # 3 in string array x => fox
Element # 4 in string array x => jumps
Element # 5 in string array x => over
Element # 6 in string array x => the
Element # 7 in string array x => lazy
Element # 8 in string array x => dog.
==========================================================
Element # 0 in string array x => dl2q>|C3HMj>7$MH]6=e
Element # 1 in string array x => &zw.P[NO/VH/0:[*Ti&i
Element # 2 in string array x => wu%2<S``Kx6r+4Qx%@g@
Element # 3 in string array x => \?:TG3bqDEza>oA{XjS2
Element # 4 in string array x => jl,s(#'9A!1g2fvCh*D3
Element # 5 in string array x => iZx=(c*{ihef4tX`/[z&
Element # 6 in string array x => p"\$DImMBRK9'4Of*8X3
Element # 7 in string array x => n'"]JCt@^lm;2N5A5dD1]n',
Element # 8 in string array x => \$n#03&):Z7B%lKaf{=%
$
$