Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Adding to Array from JavaSpace problem

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding to Array from JavaSpace problem

    I'm now stuck on such a simple task I have no idea what to do. I just want to take from the space all "chatRooms" then extract the chat room name which i will keep in an array, then write to the space supplying the extracted chat room name and "chatRooms". I have tried writing the object to array but no luck as it showed an object but couldn't extract name. I keep getting the following error from the code:

    Error: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

    public void threadchatRetrieval()
            {
     
             space = SpaceUtils.getSpace();
            if (space == null){
                System.err.println("Failed to find the javaspace");
                System.exit(1);}
           int count = 0;
           Username template = new Username(null,"chatRooms");
            do{
     
             try {
                                    temp = (Username)space.take(template, null,6000);
                                    System.out.println("output: " + temp.Name);
     
                                    if(temp.Name.equals(null)){
                                            System.out.print("null entry");
                                    } else{
                                            entry.add(0, temp.Name);
                                            count++;
                                            System.out.print("added at: " + count);
     
                                    }
     
                            } catch (RemoteException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                            } catch (UnusableEntryException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                            } catch (TransactionException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                            } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                            } catch (NullPointerException e){
                                    System.out.println("done");
     
                            }
     
     
                            //entry.add(temp.Name);
     
            }while(temp != null);
            System.out.println("output size: " + entry.size());
     
             for (int i=0; i<= entry.size();i++) {
     
                       System.out.println( "for: " + entry.get(i));
     
                      try {
                            String Username = entry.get(i);
                            Username template_new = new Username(Username,"chatRooms");
                            System.out.println( "write: " + Username);
                            space.write(template_new, null,8000);
     
                    } catch (RemoteException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                    } catch (TransactionException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                    }
                     }
    }
    Last edited by helloworld922; December 24th, 2009 at 11:55 PM. Reason: Please use [code] tags!


  2. #2
    Member
    Join Date
    Aug 2009
    Posts
    53
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: Adding to Array from JavaSpace problem

    Here is your problem:

    for (int i=0; i<= entry.size();i++) {
     
    System.out.println( "for: " + entry.get(i));

    Your ArrayList objects size is 1, but you put you string at the fixed index 0 here:
    entry.add(0, temp.Name);

    Your for loop runs through 2 times, one time where i = 0, and one where i = 1 (since size is 1)

    First time, get(0) will resolve and get you the string.
    Second time, get(1) will not resolve, and give your error message.

    Might be easier to see when compacted
    		ArrayList<String> myList = new ArrayList<String>();
    		myList.add(0, "stringZero");
    		for (int i = 0; i <= myList.size(); i++){
    			System.out.println(myList.get(i));
    		}

    tl;dr
    Solution, make i sharply smaller than size(); i < size();

    Please use the code brackets for you code, and also include a line number with the error message. Its much easier to read and find an error then :-).
    Last edited by Johannes; December 24th, 2009 at 08:44 PM.

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding to Array from JavaSpace problem

    thankyou for the reply. I have been reading alot on JavaSpaces, turns out to treat objects as if they were in an array you need to write a head and tail object to space to keep track, i thought this was a hack way of doing things and surley there was a better way but this is what is suggested and it works great.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Adding to Array from JavaSpace problem

    That is called a Linked list:

    Each element doesn't know where they are in the world, but they know who their neighbors are. The end elements obviously know that they are the end elements because they only have one (or none) neighbor.

    Arrays are kind of the opposite: They know where they are in the world, but could care less who their neighbors are. That's why inserting to arrays is so inefficient: you have to tell a lot of people they need to move so someone can fit into a certain spot.

Similar Threads

  1. Method Adding elements to an array with certain restrictions
    By Newoor in forum Collections and Generics
    Replies: 1
    Last Post: December 13th, 2009, 11:13 AM
  2. Problem with 2d array
    By Anyone in forum Collections and Generics
    Replies: 2
    Last Post: November 14th, 2009, 09:32 PM
  3. [SOLVED] Creation of objects of Array in Java
    By sadi_shihab in forum Collections and Generics
    Replies: 4
    Last Post: July 9th, 2009, 01:38 PM
  4. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 04:31 AM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM