# For loop syntax question

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 12-07-2010, 05:45 AM
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.

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();

}

}

• 12-07-2010, 06:04 AM
Bertstar
I didn't read your code that thoroughly lol, but I assume you could use a nested for loop here.
• 12-07-2010, 06:07 AM
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.
• 12-07-2010, 06:23 AM
Bertstar
I read your code this time and you can easily do what you want it to do with a nested for loop.
• 12-07-2010, 06:26 AM
Quote:

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.
• 12-07-2010, 06:30 AM
Bertstar
Quote:

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?
• 12-07-2010, 06:34 AM
Quote:

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.
• 12-07-2010, 06:38 AM
Bertstar
Quote:

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?
• 12-07-2010, 06:43 AM
pbrockway2
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.
• 12-07-2010, 06:44 AM
Quote:

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

Code:

for(int x = 0; x < numbers[0]; x++)
{
for(?; ?; ?)
}

Thanks
• 12-07-2010, 06:51 AM
Bertstar
Quote:

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

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-07-2010, 06:58 AM
Quote:

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?
• 12-07-2010, 07:01 AM
Bertstar
Quote:

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?
• 12-07-2010, 07:07 AM
Quote:

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.
• 12-07-2010, 07:08 AM
Bertstar
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.
• 12-07-2010, 07:11 AM
Bertstar
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
• 12-07-2010, 07:15 AM
So I got this so far:
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.
• 12-07-2010, 07:19 AM
Code:

while(userInput <=50 && userInput > 0);
I also changed it from < to <=, and 50 is now inclusive.
• 12-07-2010, 07:19 AM
Bertstar
Quote:

So I got this so far:
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
• 12-07-2010, 07:25 AM