Loop not doing what I expected

for (i = 0; i < TributeAlbum[i]; i++)

{

//prints required data

System.out.println();//prints blank line

System.out.printf("\nThe item number is %d", item);

//System.out.printf("\nThe product name is %s", productName);

System.out.printf("\nThe number of units is %d", numberofUnits);

System.out.printf("\nThe price per unit is $%.2f", price);

System.out.printf("\nThe total value is $%.2f", value);

System.out.println();

}

Why does this loop only print out once? I put the array as size 2 to test the loop. It only prints the last input.

Thanks,

d

[914 byte] By [Back2Schoola] at [2007-11-27 6:47:14]
# 1
I have a bad feeling that TributeAlbum is still an array of ints and not an array of Albums or TributeAlbums.
cotton.ma at 2007-7-12 18:19:57 > top of Java-index,Java Essentials,New To Java...
# 2

for (i = 0; i < TributeAlbum[i]; i++)

Do you see ? you are saying that if i=0 true and if i<tributeAlbun which is 0 so it will print only once. try this

for(i=0; i><TributeAlbum.length; i++)

>

lrngjavaa at 2007-7-12 18:19:57 > top of Java-index,Java Essentials,New To Java...
# 3
I think I figured part of it out. I have to associate the variables somehow with the array. How would I do that? I thought the dot method would be used, but found from this forum that that might not be the case.d
Back2Schoola at 2007-7-12 18:19:57 > top of Java-index,Java Essentials,New To Java...
# 4

> I think I figured part of it out. I have to

> associate the variables somehow with the array. How

> would I do that? I thought the dot method would be

> used, but found from this forum that that might not

> be the case.

>

Can we rewind a bit here?

Where is the code where you (a) initialize your array, (b) create the objects in the array and set their values.

cotton.ma at 2007-7-12 18:19:57 > top of Java-index,Java Essentials,New To Java...
# 5
nevermindMessage was edited by: paulcw
paulcwa at 2007-7-12 18:19:57 > top of Java-index,Java Essentials,New To Java...
# 6

Yes, I see. Yes, and to cotton, for now I did not change it. I am only needing a simple inventory for this project.Not dissing you...at all. I may even change it to CDs, much simpler than the albums...see? So I am just trying to get the basics and then go back and change the variables in a little bit.

I am just beginning to understand that you can use an object for a type. That is still making my head swim, so until I get my brain around that, I am just going to go with the basics until I get a good handle on those. Your input helped tremendously though...made me want to go back tonight and read on using objects as a type.

Thanks,

d

Back2Schoola at 2007-7-12 18:19:57 > top of Java-index,Java Essentials,New To Java...
# 7

Here is what I have so far...like I said, I am very new at this, and basically teaching myself, so bear with me...

//declaration of instance variables

int item = 0;

int numberofUnits = 0;

double price = 0;

double value = 0;

double total = 0;

//declaration of array

int[] TributeAlbums;

//declaration of array variable

int i = 0;

d

Back2Schoola at 2007-7-12 18:19:57 > top of Java-index,Java Essentials,New To Java...
# 8

> Yes, I see. Yes, and to cotton, for now I did not

> change it. I am only needing a simple inventory for

> this project.Not dissing you...at all. I may

> even change it to CDs, much simpler than the

> albums...see? So I am just trying to get the basics

> and then go back and change the variables in a

> little bit.

>

Okay that's fine.

But this is an important basic and your code won't work without it. You need an array of whatever object you want. Not an array of ints.

As your code stands right now you create an array of ints. You read a bunch of values collected from the user. You throw away almost all these values. You then go and print and only see the last values (because they are carried over in what I had previously thought were temporary variable but turns out aren't).

And you never use your class at all.

So let's try something simple.

public class Person{

private String name;

private int age;

public Person(String aName, int anAge){

this.name = aName;

this.age = anAge;

}

public String getName(){

return this.name;

}

public int getAge(){

return this.age;

}

public static void main(String args[]){

Person[] people;

people = new Person[2];

people[0] = new Person("John Smith", 37);

people[1] = new Person("Jane Doe",26);

for(int i=0;i<people.length;i++){

System.out.println("Person "+i+" name ="+people[i].getName());

System.out.println("Person "+i+" age ="+people[i].getAge());

}

}

}

Please note here how we used an array of objects.">

cotton.ma at 2007-7-12 18:19:57 > top of Java-index,Java Essentials,New To Java...
# 9
I am printing this and going to go study and digest it. Thank you so much for your help.d
Back2Schoola at 2007-7-12 18:19:57 > top of Java-index,Java Essentials,New To Java...
# 10
> I am printing this and going to go study and digest> it. Thank you so much for your help.> > dPlease make sure to copy the version that is there now. I had a couple of minor mistakes which I fixed. Now it will compile and run and is correct.
cotton.ma at 2007-7-12 18:19:57 > top of Java-index,Java Essentials,New To Java...
# 11
Will do.
Back2Schoola at 2007-7-12 18:19:57 > top of Java-index,Java Essentials,New To Java...
# 12

back2school, got a suggestion that may rain on your parade a bit, but maybe you're going about learning java the wrong way. Possibly what you need to do is to get a good book or two on java and go through the examples first, get familiar with the building blocks and how they're constructed, learn more advanced coding techniques, and then and only then start doing some more complex personal projects. As it is, you're stumbling through an unfamiliar room with a blindfold on asking for directions. I think you have chosen a very inefficient and frustrating way to start.One that may discourage you in the long run. Think about it. You wouldn't start to learn to play piano by trying to play Rachmanenoff's prelude in c# minor, would you? I could be wrong (and would love it if I were), but right now, that's how I see it.

Just my two shekels worth. I wish you luck, regardless, and hope that there are no hard feelings.

/Pete

petes1234a at 2007-7-12 18:19:57 > top of Java-index,Java Essentials,New To Java...