# Thread: For loop syntax question

1. Member
Join Date
Dec 2010
Posts
9
Rep Power
0

## For loop syntax question

I have to make a program that will count the number of times a user enters a numbers in a number group. The groups are in 10's, so 1-10, 11-20, and so on (up until 50).

You can see that the program works, but is there a better way to implement my for loop to make it less repetitive?

You can see that my code will print out * for the number of times you enter a number in a certain number range.

I've done most of the work, but I'm stumped as to how to make it simpler.

Java Code:
```import java.util.Scanner;

public class PP7_2 {

public static void main(String[] args)

{
Scanner myScan = new Scanner(System.in);

int[] numbers = new int[5];
int userInput;
System.out.println("This program will have the user enter numbers between 1-50" +
", and a * will be printed out for everytime you enter a numbers in groups of 10's");

do
{
System.out.print("enter a number [1 - 50] any other to exit");
userInput = myScan.nextInt();
if(userInput <= 10 && userInput > 0)
++numbers[0];
else if(userInput <= 20 && userInput > 10)
++numbers[1];
else if(userInput <= 30 && userInput > 20)
++numbers[2];
else if(userInput <= 40 && userInput > 30)
++numbers[3];
else if(userInput <= 50 && userInput > 40)
++numbers[4];
}
while(userInput <50 && userInput > 0); //10 points on the final extra credit for making
//this a for loop
System.out.print(" 1-10: ");
for(int x = 0; x < numbers[0]; x++)
System.out.print("*");
System.out.println();

System.out.print("11-20: ");
for(int x = 0; x < numbers[1]; x++)
System.out.print("*");
System.out.println();

System.out.print("21-30: ");
for(int x = 0; x < numbers[2]; x++)
System.out.print("*");
System.out.println();

System.out.print("31-40: ");
for(int x = 0; x < numbers[3]; x++)
System.out.print("*");
System.out.println();

System.out.print("41-50: ");
for(int x = 0; x < numbers[4]; x++)
System.out.print("*");
System.out.println();

}

}```

2. Member
Join Date
Dec 2010
Posts
45
Rep Power
0
I didn't read your code that thoroughly lol, but I assume you could use a nested for loop here.

3. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
In the do loop consider the integer division operator (15/10 is 1, 39/10 is 3 etc) and how it might be used as the index of your numbers array. There is a slight problem because the range goes from, eg, 1 to 10 rather than 0 to 9: but that's nothing a little subtraction can't fix.

4. Member
Join Date
Dec 2010
Posts
45
Rep Power
0
I read your code this time and you can easily do what you want it to do with a nested for loop.

5. Member
Join Date
Dec 2010
Posts
9
Rep Power
0
Originally Posted by pbrockway2
In the do loop consider the integer division operator (15/10 is 1, 39/10 is 3 etc) and how it might be used as the index of your numbers array. There is a slight problem because the range goes from, eg, 1 to 10 rather than 0 to 9: but that's nothing a little subtraction can't fix.
Thanks for the input. I would never ask someone to write code for me, but it would be nice if I could see an example of what your talking about. I'm not exactly understanding what your saying. Its hard for me to put words into context without seeing a little bit of code. Thank you for your help, its greatly appreciated.

6. Member
Join Date
Dec 2010
Posts
45
Rep Power
0
Originally Posted by salad90
Thanks for the input. I would never ask someone to write code for me, but it would be nice if I could see an example of what your talking about. I'm not exactly understanding what your saying. Its hard for me to put words into context without seeing a little bit of code. Thank you for your help, its greatly appreciated.
Are you trying to make the do while loop into a for loop or simplify the 5 for loops at the bottom?

7. Member
Join Date
Dec 2010
Posts
9
Rep Power
0
Originally Posted by Bertstar
Are you trying to make the do while loop into a for loop or simplify the 5 for loops at the bottom?
I'm trying to simplify the 5 for loops at the bottom. The program runs fine, but my teacher hates repeated code if it can be simplified. I am stumped :confused:, and my book hasn't helped with something this specific.

8. Member
Join Date
Dec 2010
Posts
45
Rep Power
0
Originally Posted by salad90
I'm trying to simplify the 5 for loops at the bottom. The program runs fine, but my teacher hates repeated code if it can be simplified. I am stumped :confused:, and my book hasn't helped with something this specific.
Like i said, you can use a nested for loop to simplify those 5 for loops. Do you know what a nested for loop is?

9. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
Java Code:
```public class Test {
public static void main (String[] args) {
int[] data = {2010, 1815, 1066, 1848, 1871};
// index i will hold the number of i-th century years
int[] centuryCounts = new int[22];
for(int year :data) {
System.out.println(year + " is in the " + (year / 100 + 1) + "-th century");
centuryCounts[year / 100 + 1]++;
}
System.out.println(centuryCounts[19] + " were in the 19th century.");
}
}```

Now finding the century for a given year isn't quite the same thing as finding the "group" of a user supplied number, so you won't use "year/100 + 1" exactly.

This addresses the slightly clumsy if-else-if-... chain in the do loop at the start.
Last edited by pbrockway2; 12-07-2010 at 06:50 AM.

10. Member
Join Date
Dec 2010
Posts
9
Rep Power
0
Originally Posted by Bertstar
Like i said, you can use a nested for loop to simplify those 5 for loops. Do you know what a nested for loop is?
Thank you for the suggestion. I do know what a nested for loop is, but I'm not sure of the code I need to use to get it to do what I need it to do

Java Code:
```for(int x = 0; x < numbers[0]; x++)
{
for(?; ?; ?)
}```
Thanks

11. Member
Join Date
Dec 2010
Posts
45
Rep Power
0
Originally Posted by salad90
Thank you for the suggestion. I do know what a nested for loop is, but I'm not sure of the code I need to use to get it to do what I need it to do

Java Code:
```for(int x = 0; x < numbers[0]; x++)
{
for(?; ?; ?)
}```
Thanks
You are somewhat on the right track for your outer loop. However, you want to iterate through all of the ints in numbers not just the first one. Your inner loop will then print out the number of * that are at a certain index in numbers.

12. Member
Join Date
Dec 2010
Posts
9
Rep Power
0
Originally Posted by Bertstar
You are somewhat on the right track for your outer loop. However, you want to iterate through all of the ints in numbers not just the first one. Your inner loop will then print out the number of * that are at a certain index in numbers.
Okay... So in the current field where it says numbers[0], What could I change the 0 to so I could reference all of the ints?

13. Member
Join Date
Dec 2010
Posts
45
Rep Power
0
Originally Posted by salad90
Okay... So in the current field where it says numbers[0], What could I change the 0 to so I could reference all of the ints?
You want to loop through all of the numbers in numbers array, so what do you think you should change the terminating condition to?

14. Member
Join Date
Dec 2010
Posts
9
Rep Power
0
Originally Posted by Bertstar
You want to loop through all of the numbers in numbers array, so what do you think you should change the terminating condition to?
I was thinking either [0, 1, 2, 3, 4] or [numbers], but I am just making an educated guess.

15. Member
Join Date
Dec 2010
Posts
45
Rep Power
0
Also, you might want to look at the terminating condition in your do while loop. Currently, if the user enters 50 the loop will exit.

16. Member
Join Date
Dec 2010
Posts
45
Rep Power
0
Notice that numbers has 5 indexes (0,1,2,3,4), so you want to iterate the length of numbers, which is simply numbers.length

17. Member
Join Date
Dec 2010
Posts
9
Rep Power
0
So I got this so far:
Java Code:
```			for(int x = 0; x < numbers[numbers.length]; x++)
{
for(int x = 0; x < ???; ?)
System.out.println("???");
}```
I really appreciate the time. I feel like I'm understanding it more, but I just need a little help.

18. Member
Join Date
Dec 2010
Posts
9
Rep Power
0
Java Code:
`while(userInput <=50 && userInput > 0);`
I also changed it from < to <=, and 50 is now inclusive.

19. Member
Join Date
Dec 2010
Posts
45
Rep Power
0
Originally Posted by salad90
So I got this so far:
Java Code:
```			for(int x = 0; x < numbers[numbers.length]; x++)
{
for(int x = 0; x < ???; ?)
System.out.println("???");
}```
I really appreciate the time. I feel like I'm understanding it more, but I just need a little help.
The terminating condition still isn't right. Currently, you are just trying to access numbers[5] (which doesnt exist since the greatest index is 4) instead of just using the length of numbers as the terminating condition. So, it should be just numbers.length

20. Member
Join Date
Dec 2010
Posts
9
Rep Power
0
Java Code:
```		for(int x = 0; x < numbers.length; x++)
{
for(int x = 0; ??; ??)
System.out.println("??");
}```
Okay... I hope that looks right.

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•