Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

    Exclamation Help with array of strings

    I'm not sure if I did this right but I don't get what it is asking me to do on c. Could someone explain it to me. Am I suppose to do a for loop to print or did I messed it up with b. I am confused.

    Write a complete program which does the following:
    a. Declares an array which holds 4 character strings.
    b. Use a for loop and place 4 character strings in the array.
    c. Use a separate for loop to print out the character strings.


    Java Code:
    class Array {
    	public static void main( String[] args ) {
    
    	char[] array = { 'v', 'e', 'i', 'l' };
    	
    		for(int i = 0; i < array.length; i++) 
    		System.out.println(array[i]);
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default Re: Help with array of strings

    ad a) you didn't define an array that can hold Strings; you defined an array that can hold a single character in each of its elements.
    ad b and c) I can see only one loop; you should have two loops.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

    Default Re: Help with array of strings

    Okay. I'm not sure if this is right.
    How do I add the second loop?

    Java Code:
    class Array {
    	public static void main( String[] args ) {
    
    	String[] array = new String [4];
    	
    		array[0]="beware";
    		array[1]="of";
    		array[2]="magical";
    		array[3]="unicorn";
    
    		for(int i = 0; i < array.length; i++) 
    		System.out.println(array[i]);
    	}
    }

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,661
    Rep Power
    5

    Default Re: Help with array of strings

    Since (b) didn't state they had to be different strings you could do it like this.

    Java Code:
    for (int j = 0; j < array.length; j++) {
       array[j] = "A string";
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

    Exclamation Re: Help with array of strings

    so will it look like this?

    Java Code:
    class Array {
    	public static void main( String[] args ) {
    
    	String[] array = new String [4];
    	
    		array[0]="beware";
    		array[1]="of";
    		array[2]="magical";
    		array[3]="unicorn";
                    
    		for(int i = 0; i < array.length; i++) 
    		System.out.println(array[i]);
                    
                    for (int j = 0; j < array.length; j++) {
                     array[j] = "A string";
                    }
    	}
    }

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,661
    Rep Power
    5

    Default Re: Help with array of strings

    The assignment said allocate an array to hold four strings, populate the array using a loop, and print out the array using a loop. So based on that you don't need the first four assignments to the array. And then you need to reverse the positions of the first and second loops. Also recommend you check with your instructor to make certain this is what was intended.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Strings and Array!!!
    By Georgino in forum New To Java
    Replies: 4
    Last Post: 01-08-2013, 12:37 PM
  2. How to convert array of Objects into array of Strings
    By elenora in forum Advanced Java
    Replies: 1
    Last Post: 06-10-2011, 03:48 PM
  3. array containing int strings
    By ggalan in forum New To Java
    Replies: 19
    Last Post: 04-22-2011, 04:40 PM
  4. Strings as array help please..
    By crazygurl in forum New To Java
    Replies: 1
    Last Post: 12-01-2009, 08:23 PM
  5. Sorting an array of Strings
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •