Two Laptop Bag*
The moose likes Beginning Java and the fly likes Array Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array Problem" Watch "Array Problem" New topic
Author

Array Problem

Iain Palmer
Ranch Hand

Joined: Jul 28, 2004
Posts: 56
i.ve created an array but am having trouble populating it the source for the population of the array is JOptionPane. I can input a name but it doesn't stay there. I've got it to output the name but the array still has a value of null. Then when I input the second name the second name appears but the first isn't there. and so on here is the code
import javax.swing.JOptionPane;
public class Storage
{
String name;
String course;
String studentArray[] = new String[5];
public Storage()
{ }
public Storage(String name,String course)
{
this.name=name;
this.course=course;
}
public void addStudent(String name)
{
int i = 0;
while (i<1)
{
studentArray[i] = name;
i++;
}
}
public void printArray()
{
for (int i=0;i<studentArray.length; i++)
System.out.println(studentArray[i]);
System.out.println(studentArray.length);
}
}
Iain Palmer
Ranch Hand

Joined: Jul 28, 2004
Posts: 56
also here is the test program that I'm using
import javax.swing.JOptionPane;
class Test
{
public static void main(String [] args)
{
for(int i =0;i<5;i++)
{
String name = JOptionPane.showInputDialog("Enter Your Name");
String course = JOptionPane.showInputDialog("Enter Your Course");
Storage st = new Storage(name,course);
st.addStudent(name);
st.printArray();
}
}
}
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
In your test class you are creating a new Storage object each time through the loop. Create your storage object outside of the loop, and use the generic constructor. Then when you call the addStudent method pass not only the name but also the index of the array (you'll have to change the method signature to accept an int) insert the name into the array using the index you've passed.
Iain Palmer
Ranch Hand

Joined: Jul 28, 2004
Posts: 56
Do you mean take the lop out of the test class or the storage class?
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
In your test class create your Storage object outside of the loop. Otherwise your creating a new Storage object each time through the loop and in turn creating a new array each time.

Try this:

Of course in a real world solution I wouldn't create my Storage object with the static modifier but it works for this example.
[ March 23, 2005: Message edited by: Hentay Duke ]
Iain Palmer
Ranch Hand

Joined: Jul 28, 2004
Posts: 56
It works better but i found that the course always seems to equal null
Iain Palmer
Ranch Hand

Joined: Jul 28, 2004
Posts: 56
also it still is only registering that there is one name in the array
John Dell'Oso
Ranch Hand

Joined: Apr 08, 2004
Posts: 130
Iain,

How about listening to some advice and actually posting your code using the code tags. You've been asked to do this on a number of times and yet you still don't.
Iain Palmer
Ranch Hand

Joined: Jul 28, 2004
Posts: 56
when i post my code on the left of the screen it says UBB Code is enabled i thought that that it what was meant
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
Here's the Storage class I used, along with the Test class posted earlier in the thread and it was filling the array correctly.



Again I repeat in the test class, normally I would pull that for loop out of the main method, but for this simple array exercise/test it's not hurting anything.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Iain Palmer:
when i post my code on the left of the screen it says UBB Code is enabled i thought that that it what was meant


This means that you can use UBB Code, which is very similar to HTML. You still have to actually type the tags you wish to use in order to take advantage of this feature. Thare are "Instant UBB Code" buttons below the message edit text area that you can use, if you don't want to type the tags. One of these buttons has the word CODE on it. When you click it it adds the tags that will preserve your indentation so that we can actually read your code.

Layne


Java API Documentation
The Java Tutorial
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Now to answer your question.

I'm wondering if you want an array that only holds the names? Or does each student also have a course that should be associated with the name? I strongly suspect that the later is the case. In order to do this, you should modify your Storage class to only hold a single name and course. (In fact, it may be more appropriate to call this Student instead of Storage in order to more accurately reflect what it represents.) Then you can create an array of Student objects in your test method.

I won't get into the exact syntax to implement this so that you can work on it yourself first. If you have trouble doing this, or if I'm totally off base from what you actually need to do, please come back with more questions. It will probably be helpful if you give us a description of what you are trying to do. If this is a homework assignment, feel free to post the exact description that your professor gave you.

Layne
Iain Palmer
Ranch Hand

Joined: Jul 28, 2004
Posts: 56
Thanks Henty for the problem solving I was staring at it most of the day trying to fathom out how I was going to add to the array.
Thanks also for clearing up the problem with me putting code on here i thougth it was the UBB Code at the side. And yes i do need to add the course to the storage class but i wanted to get one up and running properly first, will post my exact assignment later
Iain Palmer
Ranch Hand

Joined: Jul 28, 2004
Posts: 56
The assignment is
hashing problem
A program is required that uses a list of student objects.The student object needs to store the student's name and the course they are enrolled upon at a college. The hash index needs to be calculated within the student object. The hash index is calculated within the student name as a key.

To obtain user input use JOptionPane class and its showInputDialog method.

There is no inheritance between any of the 3 classes which are

The hashing assignment files, center around the student class which represents a single student who has a name and a course.This public class should be in a sourse code file

Student.java

Multiple instances of the student class are added to a single instance of the storage class.This public class should be in a source code file with the name

Storage.java

Both of the above classes should only be istatiated by the MainProgram class. This public class should be in a source file with the name

MainProgram.java

the source code file I've been given are



hope the code comes out rigth I've put them in beyween the 2 code names which appeared when i clicked on the link
Iain Palmer
Ranch Hand

Joined: Jul 28, 2004
Posts: 56
i've written the answer to the problem in one class because when it was in multiple classes. I couldn't remember how to get to a method in a different class. I thought it was variableName.methodName(); but it didn't work right
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Problem