• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with loop  RSS feed

 
brent bynum
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am asking the user to input 11 grades(A, B, C, etc) and the I want them to be printed out. I am having an issue doing this.



This is only printing out the last one that is entered. What do I need to do to fix it?
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You only tell it to print once, outside of the loop.

move it inside the loop.

WP
 
brent bynum
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William P O'Sullivan wrote:You only tell it to print once, outside of the loop.

move it inside the loop.

WP


I have tried that but it isn't what I am looking for.

I am wanting it to print the grades out like this:

A
B
C
D
etc...
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
brent bynum wrote:
William P O'Sullivan wrote:You only tell it to print once, outside of the loop.

move it inside the loop.

WP


I have tried that but it isn't what I am looking for.

I am wanting it to print the grades out like this:

A
B
C
D
etc...



Each iteration of the loop simply replaces the letter grade from the previous iteration of the loop -- of course, this is not good as you need all the letter grades. One option to fix this is to use an array (or collection). Of course, this means that you will need a loop to print out the scores.

Henry
 
brent bynum
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
brent bynum wrote:
William P O'Sullivan wrote:You only tell it to print once, outside of the loop.

move it inside the loop.

WP


I have tried that but it isn't what I am looking for.

I am wanting it to print the grades out like this:

A
B
C
D
etc...



Each iteration of the loop simply replaces the letter grade from the previous iteration of the loop -- of course, this is not good as you need all the letter grades. One option to fix this is to use an array (or collection). Of course, this means that you will need a loop to print out the scores.

Henry


So, do I add to the loop I already have?
 
Panagiotis Kalogeropoulos
Rancher
Posts: 99
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easiest thing you can do is to simply use a System.out.println() inside the loop that you already have. This way you will see each time what grade was inserted. But if you want to do more with the grades, you will have to use an array or a List (array seems a good choice since you know beforehand how many items you will need), as Henry has already said.
 
brent bynum
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Panagiotis Kalogeropoulos wrote:The easiest thing you can do is to simply use a System.out.println() inside the loop that you already have. This way you will see each time what grade was inserted. But if you want to do more with the grades, you will have to use an array or a List (array seems a good choice since you know beforehand how many items you will need), as Henry has already said.


Ok, will have to look in my book. Not really sure how to tackle arrays right now.
 
Panagiotis Kalogeropoulos
Rancher
Posts: 99
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out here too:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

I hope this helps!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!