Results 1 to 13 of 13
  1. #1
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Question Setting up a String Array

    Hello. I am trying to set up a String that is an array called "linereturn".
    I have used the below two lines with no success:

    String[] linereturn;

    I then try to give linereturn[0] a value by doing:

    lnreturn[0]="Hello";

    However, it does not work when I try to compile the code and get the following error message:
    displayText.java:33: variable linereturn might not have been initialized.

    Can anyone help?

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    You never initialized your variable. You have to create the String array.

    Example:
    Java Code:
    String[] linereturn = new String[10];
    I suggest you go through the Array Tutorials, located here Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

  3. #3
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Cool

    Thank you for your post. I appreciate you are stating that one has to have information on the right hand side.

    String[] linereturn = new String[10];

    However, I want to create an array, but I don't know how big it will be so cannot commit to specifying the number in the square brackets (it depends on how many lines are being read from a file!). So, a file could have one line or many lines.

    Is there a way of initializing the array without having to specify a number or is there a way around this?

    Many thanks.

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Use a ArrayList<String> instead of an array.

    -Gary-

  5. #5
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Smile

    Could you please give an example?

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by AJArmstron@aol.com View Post
    Could you please give an example?
    Java Code:
    import java.util.*;
    ...
            List<String> lines = new ArrayList<String>();
            lines.add(thisString);
            lines.add(thatString);
            lines.add(someOtherString);
    
            String[] myArray = lines.toArray(new String[lines.size()]);
    ArrayList (Java 2 Platform SE 5.0)

    -Gary-

  7. #7
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Cool

    Thank you for that help.

    I have created an array list

    ArrayList arrayList = new ArrayList();

    and added by doing:
    arrayList.add( new String(line) );

    However I need to edit the first value in the list and set it to arrayList.size()

    Could you please tell me how to edit it. I have tried lines like:
    arrayList.set(0)=arrayList.size();

    But with no success.

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Read the link I gave you. First of all, you really don't want a raw ArrayList, but a generic ArrayList<String>. Read the tutorials and learn why. Second, if you look at the link I gave you, and find the set() method, you will see that it takes two parameters.
    Java Code:
            list.set(index, someString);
    If you are going to program in Java, you will certainly want to learn
    1. ArrayLists
    2. generics
    3. reading and understanding API documents

    It is confusing and frustrating when you are starting out, but be patient with yourself and keep trying.

    -Gary-

    EDIT: My advice presumes you are running Java 5 or later. If you are stuck on Java 4, you don't have generics -- consider updating.

  9. #9
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    little program with few examples of using ArrayList

    Java Code:
    import java.util.ArrayList;
    
    public class LineReturn {
    	public static void main(String[] args) {
    		// in the next line lineReturn is initialised
    		// something like: String[] linereturn = new String[10];
    		ArrayList<String> lineReturn = new ArrayList<String>();
    		
    		// now we add lines
    		// something like: lnreturn[0]="Hello";
    		lineReturn.add("Hello");
    		lineReturn.add("world");
    		lineReturn.add("This is lineReturn example");
    		lineReturn.add(":-)");
    		lineReturn.add("Hello");
    
    		// if we want to print for example second element of ArrayList
    		System.out.println(lineReturn.get(1));
    		
    		//if we want to compare two elements
    		if(lineReturn.get(0).equals(lineReturn.get(4)))
    			System.out.println("They are equal");
    		
    		// remove 5th element - "Hello" from ArrayList
    		lineReturn.remove(4);
    		
    		//print all elements
    		for(String oneLine : lineReturn)
    			System.out.println(oneLine + "\n");
    		
    	}
    
    }

  10. #10
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Thumbs up

    Thank you for your posts they have been extremely helpful.

  11. #11
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Cool

    I now want to print the contents of the ArrayList into a pane. I had been previously been using a JOptionPane using code along the following lines:

    JOptionPane.showMessageDialog(null, "**** Statistics of text ****"
    + "\nThe number of lines is " + data[0] + "\nThe number of As is " + data[1] + "\nThe number of Es is " + data[2] + "\nThe number of Is is " + data[3] + "\nThe number of Os is " + data[4] + "\nThe number of Us is " + data[5] + "\nThe number of vowels is " + data[6] + "\nThe number of consonants is " + data[7] + "\nThe number of spaces is " + data[8] + "\nThe number of printables is " + data[9] + "\nThe number of total characters is " + data[10] );

    This displayed a message box. Very simple, but effective for my needs.

    I want to do something very similar with my LineReturn ArrayList<String>. The problem is I don't know how how many variables will be in the LineReturn ArrayList. (I note this time I only need to print out the lines from LineReturn and no other messages accompanying it.)

    Can anyone offer some ideas as to how I can get around this problem? I don't believe I can use a for loop as it will not work with the JOptionPane coding. I suppose one alternative is to use a different display option?

    I would be grateful for any ideas.
    Many thanks

  12. #12
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    You do want to use a loop. Something like this:

    Java Code:
            StringBuilder sb = new StringBuilder();
            for (String line : lineReturn) {
                    sb.append(line);
                    sb.append('\n');
            }
            JOptionPane.showMessageDialog(parentComponent, sb.toString());
    -Gary-

    EDIT: Sorry about that -- showMessageDialog() is a static method, and its signature is different from what I indicated.
    Last edited by gcalvin; 04-16-2010 at 01:33 AM.

  13. #13
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Wink

    GCalvin - Thank you for your assistance! I got it working successfully.

    I note the java compiler didn't like the use of "parentComponent" which I got around by typing null.

    The only drawback with my current effort is when the JOptionPane message is displayed it can be very large (i.e. it goes off the screen display edges). Is there a way of controlling the size and say using a scroll system?

Similar Threads

  1. Compare Array and setting to temp
    By saqib15 in forum New To Java
    Replies: 0
    Last Post: 02-17-2010, 06:16 PM
  2. Replies: 5
    Last Post: 11-26-2009, 07:01 PM
  3. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 11:41 AM
  4. setting and getting array
    By jgonzalez14 in forum New To Java
    Replies: 6
    Last Post: 11-26-2008, 06:54 AM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 AM

Posting Permissions

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