Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    salad90 is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default 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. #2
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    I didn't read your code that thoroughly lol, but I assume you could use a nested for loop here.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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. #4
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    I read your code this time and you can easily do what you want it to do with a nested for loop.

  5. #5
    salad90 is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    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. #6
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    Quote Originally Posted by salad90 View Post
    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. #7
    salad90 is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Bertstar View Post
    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. #8
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    Quote Originally Posted by salad90 View Post
    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. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

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

  10. #10
    salad90 is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Bertstar View Post
    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. #11
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    Quote Originally Posted by salad90 View Post
    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. #12
    salad90 is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Bertstar View Post
    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. #13
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    Quote Originally Posted by salad90 View Post
    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. #14
    salad90 is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Bertstar View Post
    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. #15
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    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. #16
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    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. #17
    salad90 is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    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. #18
    salad90 is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    Java Code:
    while(userInput <=50 && userInput > 0);
    I also changed it from < to <=, and 50 is now inclusive.

  19. #19
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    Quote Originally Posted by salad90 View Post
    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. #20
    salad90 is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    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 LastLast

Similar Threads

  1. Syntax question
    By rockeater in forum New To Java
    Replies: 3
    Last Post: 08-31-2010, 11:52 PM
  2. A question about syntax.
    By mr_o in forum New To Java
    Replies: 2
    Last Post: 09-19-2009, 08:30 PM
  3. Question about for loop..
    By sivakumar_sakam in forum New To Java
    Replies: 4
    Last Post: 05-15-2009, 11:23 PM
  4. simple loop syntax( ; ;), ( something : somthing)
    By xcallmejudasx in forum New To Java
    Replies: 4
    Last Post: 12-13-2008, 12:19 PM
  5. Replies: 13
    Last Post: 07-06-2008, 09:11 AM

Posting Permissions

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