String Array in java


 
Thread Tools Search this Thread
Top Forums Programming String Array in java
# 1  
Old 08-09-2010
String Array in java

Hi ,

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.
# 2  
Old 08-13-2010
Quote:
Originally Posted by arizah
...
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. ...
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.

Code:
$
$
$ 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[i]);
    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[i] = 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[i]);
    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 => JCt@^lm;2N5A5dD1]n',
Element # 8 in string array x => \$n#03&):Z7B%lKaf{=%
$
$

HTH,
tyler_durden

Last edited by durden_tyler; 08-13-2010 at 02:10 PM..
This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 08-16-2010
Thanks Tyler really appreciate your good comments. Much appreciated.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk Associative Array and/or Referring to Field by String (Nonconstant String Value)

I will start with an example of what I'm trying to do and then describe how I am approaching the issue. File PS028,005 Lexeme HRS # M # PhraseType 1(1:1) 7(7) PhraseLab 501 503 ClauseType ZYq0 PS028,005 Lexeme W # L> # BNH # M #... (17 Replies)
Discussion started by: jvoot
17 Replies

2. Shell Programming and Scripting

How to use string in array name?

Hi, I'm the beginner in bash scripting and I just want to create loop, which will create a few tables with string in name, and each time i try to do this in the following way, I receive: a=1; while do echo "Plik a=$a" for m in {1..4} do echo "Plik m=$m" ... (7 Replies)
Discussion started by: masterqu
7 Replies

3. Homework & Coursework Questions

passing letters from an array into a string for string comparison

attempting the hangman program. This was an optional assignment from the professor. I have completed the logical coding, debugging now. ##I have an array $wordString that initializes to a string of dashes ##reflecting the number of letters in $theWord ##every time the user enters a (valid)... (5 Replies)
Discussion started by: lotsofideas
5 Replies

4. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

5. Programming

Link array to class java

Hi, I need help to Link array from one class to another class Firstly CSVParser Class what it did is load csv file and store into array Secondly WarehouseItem where each record is store How can I get a list of array that I load to CSVParser Class and store them to WarehouseItem and... (0 Replies)
Discussion started by: guidely
0 Replies

6. Programming

How to prevent incorrect string using reg expr in Java?

Hi All, I need your input on how to mask out / ignore a string that does not match a working regular expression (continually refining) pattern in Java. Below is the code snippet which is picking up all the lines with the correct regular expression string except one known so far: public... (0 Replies)
Discussion started by: gjackson123
0 Replies

7. Shell Programming and Scripting

how to convert array into the string

Hi I have a line/string as follows: A=" 3498 NORDEA - INDX LINKED NORY" which was converted into an array of characters: p321$ echo "${ARR}" 3 4 9 8 N O R D E A - I N D X L I N K E D N O R Y When I am trying print this array there are blank... (4 Replies)
Discussion started by: aoussenko
4 Replies

8. Shell Programming and Scripting

Pass string from shell script to java

Hi, I,m writing a program in shell script and currently this script is calling a java program. I have a problem to pass string variable from my shell script to the java program. I don't know on how to pass it and how the java program can call what I have pass from the shell script. This is... (3 Replies)
Discussion started by: badbunny9316
3 Replies

9. UNIX for Dummies Questions & Answers

String Array

I am having a string with the value: str="name1, name2, name3, " I want to traverse the list of elements. How do I loop them? (6 Replies)
Discussion started by: ravikirankethe
6 Replies

10. UNIX for Dummies Questions & Answers

string to array

Hi Experts, I'm new to Shell Scripting world and need a little help from you "Scripting Gurus". I want to convert a string (entered by a user) to array in csh. I want something like this: echo "Enter the Numbers: " set num = $< Suppose, user enters: 1 2 3 4 5 6 7 This... (6 Replies)
Discussion started by: sumitgarg
6 Replies
Login or Register to Ask a Question