aspose file tools*
The moose likes Java in General and the fly likes URGENT!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "URGENT!!!" Watch "URGENT!!!" New topic
Author

URGENT!!!

asheet anand
Ranch Hand

Joined: Nov 15, 2000
Posts: 83
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

Joined: Nov 15, 2000
Posts: 83
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
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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.


"JavaRanch, where the deer and the Certified play" - David O'Meara
asheet anand
Ranch Hand

Joined: Nov 15, 2000
Posts: 83
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"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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

Joined: Nov 15, 2000
Posts: 83
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

Joined: Nov 15, 2000
Posts: 83
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

Joined: Nov 15, 2000
Posts: 83
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
Dave Turner
Ranch Hand

Joined: Mar 13, 2001
Posts: 60
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

Joined: Nov 15, 2000
Posts: 83
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

Joined: Mar 13, 2001
Posts: 60
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: URGENT!!!
 
Similar Threads
Strange Collections Question from OCP Java SE 6 Programmer Practice Exams book by K & B
why no error
Sun XML DOM DocumentBuilder Obj fails when called by a Servlet
Sorting-Java Comparator-predefined list
Collections.sort() does not sort my ArrayList