• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

URGENT!!!

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
my code is something like this
import java.util.*;
public class b
{static String a="aa";
static ArrayList list2=new ArrayList();
public static void main(String args[])
{list2.add("gh");
Iterator ix=list2.iterator();
while(ix.hasNext())
{Object o1=ix.next();
String go=(String)o1;
System.out.println(go);
}
}

}

import java.util.*;
import b;
class a
{
public static void main(String[] args)
{
Iterator ix=b.list2.iterator();
while(ix.hasNext())
{Object o1=ix.next();
String go=(String)o1;
System.out.println(go);
}
}
}
when i run a.java it does not show the value of the element of
list2.
if i put list2 in a method in class b and call the method
from class a it works fine.
import java.util.*;
public class b
{static String a="aa";
static ArrayList list2=new ArrayList();
static void show()
{list2.add(a);}}
import java.util.*;
import b;
class a
{
public static void main(String[] args)
{b.show();
Iterator ix=b.list2.iterator();
while(ix.hasNext())
{Object o1=ix.next();
String go=(String)o1;
System.out.println(go);
}
}
}

could you please tell me is there any way by which i can refer to list2 directly from a.java without calling the method show()
actually i am working on chat server application where on the server side elements in list2 are added dynamically.now i want
the list of all the elements currently present in list2
i hope u understand
thanks
ASHEET
 
asheet anand
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for ur reply.
i am running that program from the command prompt.
actually that is my server program in a chat-server application.
now even if i add an element in the list without a static initalizer it works and if i display all the elements of the list
after adding a new element it is also displaying that element in the server program.
but when i call a iterator of that list from another program it does not display all the elements why???
please help me
thanks
asheet
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that you missed Gareth's point.
If you are running class a from the prompt then only the main from class a gets executed. The main from class b is NEVER executed. Therefore list2 never gets loaded with anything.
When you put the loading in a method and call the method the show method is doing the loading so suddenly it works.
Do what Gareth said and load list 2 in a static initializer.
 
asheet anand
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
class b in my program is a server which starts running before
class a.when i am executing class a at that time also class b
is running and that's why it is showing the elements of list2.
now what do you say?
asheet
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition please show us how you are getting these two programs to run in the SAME JVM. It sounds like you might have 2 separate JVMs running concurrently, but of course they don't talk to each other without special code.
Class b is running on the server, yes? And you are starting
Class a from the command prompt on the client?? In which case you would need to use RMI. Or did you run it also on the server, but with an additional >java a command, in which case you started another JVM. I need clarification.
What your code shows is that you run a and when you reference b.list2.iterator(); class b is LOADED but not executed.
 
asheet anand
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i am asking the user to enter a nickname to connect to the
chat room in a applet which is invoked by using java -plugin.
now my server program is running on the dos prompt. what i want is when a user name already exists it should ask the user to re-enter with another name.
for this,i am taking a iterator of the arraylist of the server program which is storing all the names but it is not working
in that applet.
is this because of the jvm i am using to invoke the applet is
different or what?
i am really confused now?
my chat application is a simple socket program without using any servlets.
please help me!
asheet
 
asheet anand
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
infact the elements in the list are dynamically as the new users
joins the room.
but i noticed something interesting now if i add some elements to the list and then start the server program.then when i enter
the same name in the applet it asks the user to reenter the name
.
it simply means that it is able to extract the values from the server program and jvm is not playing a role.
asheet
 
asheet anand
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
it is a big code in three programs so it will confuse you guys.
here i am putting in short what i am doing and what i want to do
three programs.
1.chatapplet entry
it asks the user to enter a nickname and connect it to the main
chat applet.
2.main chat applet
it connects to the server through sockets and it can send and receive public and private messages to other connected users.
3.server program
it is connected to the main chat applet .
procedure
1.run the server program on the dos prompt.
2.run the chat applet entry,which in turns run the main chat applet.
now what i am doing is ,in my server program i am storing all
the names of the users who are chatting in a arraylist.
what i want is
in the chat applet entry i should compare the name entered by a new user ,say 'ash' by all the names which are in the arraylist.
now if it does not found any match to that name in the list it should go to main chat applet otherwise the user is asked to re enter his name.
problem
i am importing that server program in my chatapplet entry and taking a iterator on it.but this is not working.it even shows
the size of the list to be 0.
but if the display the same thing in the server program
it is working fine.
solution
what i think is happening is because elements in the list
are added in the main method of the server program,although
the program is running continously,it still makes a problem.
please tell me the solution.i hope you all have understood the
problem.
thanks
asheet
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by asheet anand:
problem
i am importing that server program in my chatapplet entry and taking a iterator on it.but this is not working.it even shows
the size of the list to be 0.
but if the display the same thing in the server program
it is working fine.


How is the applet retrieving the ArrayList of usernames from the server?
Static variables in a class are only persistent/accessible within the jvm that the class has been loaded in, and your clientapplet and server WILL each be running on seperate jvm's. this means that you MUST send the ArrayList using the existing socket connections, as no other way (expect RMI, etc) will work.
hope this helps
Dave
 
asheet anand
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but if i add say,2 elements to the arraylist before starting the server instead of adding the elements dynamically,if works fine.
why?
 
Dave Turner
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Asheet,
If you add items to your static ArrayList in the constructor or static initialisation code for your server, then when you check this ArrayList in your clients code, you will see these items, however you are not seeing the Servers ArrayList, but a completely seperate ArrayList that has been initialised in exactly the same way.
To achieve your goal of establishing a unique name for each new client, your server must send the list of usernames to your clients in the same way that it is sending public and private chat messages.
hope this helps
Dave
[This message has been edited by Dave Turner (edited April 20, 2001).]
 
Everyone is a villain in someone else's story. Especially this devious tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic