Results 1 to 6 of 6
  1. #1
    Problemer is offline Member
    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

    Default Adding a Value to an Arraylist of String arrays.

    Hello ,

    i have made a try catch statement so the program wont crash if an index is empty when it tries to access it.
    What i then want to do if there is a "catch", is to set that index with a value of my choice. So when later on it
    is used in the program, it is atlest " ".


    Java Code:
     try {
           special1 = bookInfoSplitList.get(0)[7];
    	} catch (Exception e) {
            special1 = " ";
            bookInfoSplitList.add(0)[7] = special1;
            
            }
    and then the error message

    XML Code:
    ./Books.java:79: cannot find symbol
    symbol  : method add(int)
    location: class java.util.ArrayList<java.lang.String[]>
    bookInfoSplitList.add(0)[7] = special1;
    Best Regards
    P
    Last edited by Problemer; 11-01-2012 at 02:22 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Adding a Value to an Arraylist of String arrays.

    You have an ArrayList of Strings. You're trying to add a single int to that ArrayList, and then for some reason you're trying to use an array index on that. None of that makes much sense. What exactly are you trying to do here?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Problemer is offline Member
    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

    Default Re: Adding a Value to an Arraylist of String arrays.

    Sorry the variables are not delcared in the code here.

    But we are not talking INT , its strings.

    The arraylist is an arraylist where every index contains an array of strings. (arraylist)[stringarrays] , if for example position (0)[1] is empty when trying to write to a file the program will crash.

    Thats why i am trying to check if there is an value in it before , if not write something to it.

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Adding a Value to an Arraylist of String arrays.

    With line 2, you are assigning special1 the value of the item at index 7 in the array at position 0 in bookInfoSplitList.

    Assuming that you want to change the value of the item at index 7 in the array at position 0, you could create a String[] variable,
    and set it's value to be the String[] at position 0 in bookInfoSplitList.

    Once you have the String[], you can set the value at index 7 to be whatever you want it to be (looks like you want a single space).
    Then you would simply set the value at index 0 of bookInfoSplitList to be equal to your new String[], essentially overriding what was there.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Adding a Value to an Arraylist of String arrays.

    Quote Originally Posted by Problemer View Post
    But we are not talking INT , its strings.
    That's exactly my point. You need to add *arrays* of Strings to the ArrayList. But here you're adding a single int:

    Quote Originally Posted by Problemer View Post
    bookInfoSplitList.add(0)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Problemer is offline Member
    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

    Default Re: Adding a Value to an Arraylist of String arrays.

    Aight i will look through that later on.
    Thanks for the advice!

Similar Threads

  1. Converting Arraylist to String array
    By ankit1801 in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 10:07 AM
  2. Replies: 12
    Last Post: 11-26-2010, 08:33 AM
  3. Replies: 1
    Last Post: 11-04-2010, 06:53 PM
  4. Replies: 2
    Last Post: 03-16-2010, 07:01 AM
  5. Putting a string into ArrayList<String>
    By k4ff1n34dd1c7 in forum New To Java
    Replies: 5
    Last Post: 03-23-2009, 05:10 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
  •