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 3 of 3

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

    Not sure if this is advanced or not but here goes:

    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();
    }
    }
    }


  2. #2
    Junior Member switcha's Avatar
    Join Date
    Sep 2011
    Location
    Australia
    Posts
    20
    My Mood
    Fine
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Adding to Array from JavaSpace problem

    Well, first of all. The IndexOutOfBoundsException is being thrown because your array contains only 1 value and the index is going further than 0 i.e. the index is out of range, hence the 'out of bounds' part of the exception. I'm not 100% certain I follow what your trying to do but here goes my interpretation:
    Extract all chatroom names from space;
    Extract each chatroom name, store individual names in a string array;
    Access string array[n] and use this value and all chatroom names to write to space;
    Please let me know if this is correct.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Adding to Array from JavaSpace problem

    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"
    Can you rewrite your requirements in java programming terms?
    Your question is all jargon about your app. Translate that to variables, classes and methods.

Similar Threads

  1. Adding to Array from JavaSpace problem
    By rtumatt in forum Collections and Generics
    Replies: 3
    Last Post: January 5th, 2010, 07:19 PM
  2. 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
  3. Problem with 2d array
    By Anyone in forum Collections and Generics
    Replies: 2
    Last Post: November 14th, 2009, 09:32 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