• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Loops

 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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?
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Sheriff
Posts: 17629
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it!! Thank you for your help everyone.
 
And then we all jump out and yell "surprise! we got you this tiny ad!"
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic