permaculture playing cards*
The moose likes Beginning Java and the fly likes Loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Loops" Watch "Loops" New topic
Author

Loops

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
I have a String name comeing into a method.

public void storeName (String name)

These names are coming from another program.

I store these names:

Student [ ] studentNames = new Student[number];
for (int i = 0; i < number; i++)
{
studentNames[i] = (name);
}

When I display these names I get six names displayed similair to:

FRED 0
JO 1
PAUL 2
MIKE 3
HELEN 4
KELLY 5

However, I get the above SIX times? And I don't understand why? Please can someone help?
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
However, I get the above SIX times? And I don't understand why? Please can someone help?
Could you show us the code that prints out the names?

When you said six times, what does the output look like?
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
The output is:

FRED 0
JO 1
PAUL 2
MIKE 3
HELEN 4
KELLY 5

FRED 0
JO 1
PAUL 2
MIKE 3
HELEN 4
KELLY 5

FRED 0
JO 1
PAUL 2
MIKE 3
HELEN 4
KELLY 5

FRED 0
JO 1
PAUL 2
MIKE 3
HELEN 4
KELLY 5

FRED 0
JO 1
PAUL 2
MIKE 3
HELEN 4
KELLY 5

FRED 0
JO 1
PAUL 2
MIKE 3
HELEN 4
KELLY 5
Vj Kamath
Greenhorn

Joined: Nov 19, 2004
Posts: 15
I assume you put number=6
Each element in your array is again an array of size 6. That could explain it.

You need to declare the array and its index outside your method which loads it. And increment the index inside your method
Matt Fielder
Ranch Hand

Joined: Oct 27, 2004
Posts: 158


Student [ ] studentNames = new Student[number];
for (int i = 0; i < number; i++)
{
studentNames[i] = (name);
}



I'm actually gonna take a stab at this. Feel free to jump in if you ACTUALLY KNOW the answer.

your for loop tests i<number which causes it to iterate as many times as you have students, but the "studentNames[i] = (name);" will produce the entire array of names. so you really don't need the for loop.

can someone confirm this?
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
The argument to your method


takes a single String, so following along in your code if your loop worked correctly you would assign the same name to every student in your array. As this is not the case I am perplexed as to what is happening.
Please post more of you code so that, it is possible to track the fault.
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
I have a class called Storage test that is doing the following:
//Class to test the operation of the Storage class
public class StorageTest
{
public static void main(String [ ]args)
{
//Defines the number of Student objects
int number = 6;

//Make an instance of Storage class to store Student object references in
Storage store = new Storage (number);
store.storeStudent ("Joe Bloggs", "Java");
store.storeStudent ("Fred Smyth", "Visual Basic");
store.storeStudent ("Sally Collins", "History");
store.storeStudent ("JOHN DOE", "Java");
store.storeStudent ("JOE DOHN", "Visual Basic");
store.storeStudent ("Joe Dohn", "Visual C#");

I then have another class called Storage which has a method called storeStudent:



NOTICE I TOOK AWAY MY FOR LOOP. WHICH RESULTED IN THE FOLLOWING:

C:\java>java StorageTest

This is what is coming into method: Joe Bloggs Java
Student@11a698a Now stored

This is what is coming into method: Fred Smyth Visual Basic
Student@107077e Now stored

This is what is coming into method: Sally Collins History
Student@7ced01 Now stored

This is what is coming into method: JOHN DOE Java
Student@1ac04e8 Now stored

This is what is coming into method: JOE DOHN Visual Basic
Student@765291 Now stored

This is what is coming into method: Joe Dohn Visual C#

The PROBLEM with this (besides sorting the toSting( ) - another problem). I can't store everything in the position lp which is initialised to 0??
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

Originally posted by Maureen Charlton:
The PROBLEM with this (besides sorting the toSting( ) - another problem). I can't store everything in the position lp which is initialised to 0??


Then you should change the value of lp every time you invoke storeStudent().

It might help if you plan out what your classes/methods are responsible for.

Class: Storage
Responsibility: Keep instances of Students
Constructor: Storage(int n) - initialize storage for n Students
Method: storeStudent(Student s) - store Student s in the next available slot in this store. If no available slots, throw exception.

I notice that in storeStudent you are creating a new array. This means that every time you call storeStudent(), you are wiping out the storage area and losing the student that was stored in the previous call to storeStudent().

Here's some pseudo code to help you some more:

Constructor:
initialize storage array with N elements
initialize next available slot index, lp, to 0

storeStudent:
check if there is an available slot (compare lp to size of storage array)
if a slot is available, put a new Student in that slot
increment the lp variable

HTH


Junilu - [How to Ask Questions] [How to Answer Questions]
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Got it!! Thank you for your help everyone.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loops