• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with a for loop  RSS feed

 
Alex Graham
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to be able to run this allowing 5 games to be added. When I run it I get:

Enter Name of game, if you would like to stop then type 'stop'
test 1
How many games in stock: 10
What is the games price: 10

This is what you entered

Name: test 1
Item Number: 0
Price of game: $10.0
Total games in stock: 10.0
Total vaule: $100.0

Name: test 1
Item Number: 0
Price of game: $10.0
Total games in stock: 10.0
Total vaule: $100.0

The value of the entire inventory is: $100.0

For some reason it is only going through the for loop 1 time and then ending even though counter is set to 5. Why is this?



 
Raja gopal Mani
Greenhorn
Posts: 13
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried executing the code and the loop executed fine without any changes

Results Obtained :

E:\JAVA\OOPS\Game>java Game
Enter Name of game, if you would like to stop then type 'stop'
Game1
How many games in stock: 2
What is the games price: 10

This is what you entered

Name: Game1
Item Number: 0
Price of game: $10.0
Total games in stock: 2.0
Total vaule: $20.0

Enter Name of game, if you would like to stop then type 'stop'
Game2
How many games in stock: 3
What is the games price: 20

This is what you entered

Name: Game2
Item Number: 1
Price of game: $20.0
Total games in stock: 3.0
Total vaule: $60.0

Enter Name of game, if you would like to stop then type 'stop'
Game3
How many games in stock: 4
What is the games price: 40

This is what you entered

Name: Game3
Item Number: 2
Price of game: $40.0
Total games in stock: 4.0
Total vaule: $160.0

Enter Name of game, if you would like to stop then type 'stop'
Game4
How many games in stock: 10
What is the games price: 20

This is what you entered

Name: Game4
Item Number: 3
Price of game: $20.0
Total games in stock: 10.0
Total vaule: $200.0

Enter Name of game, if you would like to stop then type 'stop'
Game5
How many games in stock: 5
What is the games price: 8

This is what you entered

Name: Game5
Item Number: 4
Price of game: $8.0
Total games in stock: 5.0
Total vaule: $40.0

Name: Game1
Item Number: 0
Price of game: $10.0
Total games in stock: 2.0
Total vaule: $20.0

Name: Game2
Item Number: 1
Price of game: $20.0
Total games in stock: 3.0
Total vaule: $60.0

Name: Game3
Item Number: 2
Price of game: $40.0
Total games in stock: 4.0
Total vaule: $160.0

Name: Game4
Item Number: 3
Price of game: $20.0
Total games in stock: 10.0
Total vaule: $200.0

Name: Game5
Item Number: 4
Price of game: $8.0
Total games in stock: 5.0
Total vaule: $40.0

The value of the entire inventory is: $480.0
E:\JAVA\OOPS\Game>
 
Alex Graham
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I closed out Eclipse and started it back up again and it worked. Thank you for a quick response. One more question. I can't find information anywhere that will tell me how to do: Create another method to sort the array items by the name of the product. Could anyone point me in the right direction please?
 
Raja gopal Mani
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try implementing Comparable interface to your class !

 
Lee Zh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public String[] sortByName(int counter) {
String[] productNames = new String[counter];
int i = 0;
for(GameDetails g : this) {
productNames[i++] = g.gameName;
}
Arrays.sort(productNames);
return productNames;
}

in main

String[] sorted = games.sortByName(5);


ps: i am new to Java, so if i am not right.. please correct me.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Graham wrote:I want to be able to run this allowing 5 games to be added...

Alex,

Please DontWriteLongLines. It makes your thread very hard to read,
and it's actually very bad coding practice too.
Unfortunately, you have too many (and I see that you have enormous "separators" in there as well)
for me to break up; otherwise I would have. But for future reference, please remember:
80 characters max.
(the SSCCE page actually recommends 62)
And that includes string literals AND comments (AND separators).

If you fancy doing it yourself, you can use the 'Edit' button.

Thanks.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!