Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Too many loops!

  1. #1
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Too many loops!

    Hey.
    I have this method that is way to repetitive and i've tried to make it shorter by only one for loop but it didn't really work out for me.
    The for loops take some values between, for ex, 0 and 11 and then prints the amout of numbers between those with asterisks.
    Any ideas anyone? Not in a panic atm because i feel like doing it in this way is okay but i would much rather have a smaller code than this.

    static void histogram() {
    		// This is not an effective way of writing everything out!
    		// Tried using a for loop to update all the values and using multiple parameters
    		// for such. But it ended up not working so made it in this way.
     
    		// Each for loop checks for specific whole integers between, example: 0 and 11
    		// but
    		// really looks for 0 and 10.
    		System.out.println("Histogram :");
    		System.out.print(" 1  - 10  | ");
    		for (int k : list) {
    			if (k > 0 && k < 11) {
    				System.out.print("* ");
    			}
     
    		}
    		System.out.println("");
     
    		System.out.print(" 11 - 20  | ");
    		for (int k : list) {
    			if (k > 10 && k < 21) {
    				System.out.print("* ");
    			}
     
    		}
    		System.out.println("");
     
    		System.out.print(" 21 - 30  | ");
    		for (int k : list) {
    			if (k > 20 && k < 31) {
    				System.out.print("* ");
    			}
     
    		}
    		System.out.println("");
     
    		System.out.print(" 31 - 40  | ");
    		for (int k : list) {
    			if (k > 30 && k < 41) {
    				System.out.print("* ");
    			}
     
    		}
    		System.out.println("");
     
    		System.out.print(" 41 - 50  | ");
    		for (int k : list) {
    			if (k > 40 && k < 51) {
    				System.out.print("* ");
    			}
     
    		}
    		System.out.println("");
     
    		System.out.print(" 51 - 60  | ");
    		for (int k : list) {
    			if (k > 50 && k < 61) {
    				System.out.print("* ");
    			}
     
    		}
    		System.out.println("");
     
    		System.out.print(" 61 - 70  | ");
    		for (int k : list) {
    			if (k > 60 && k < 71) {
    				System.out.print("* ");
    			}
     
    		}
    		System.out.println("");
     
    		System.out.print(" 71 - 80  | ");
    		for (int k : list) {
    			if (k > 70 && k < 81) {
    				System.out.print("* ");
    			}
     
    		}
    		System.out.println("");
     
    		System.out.print(" 81 - 90  | ");
    		for (int k : list) {
    			if (k > 80 && k < 91) {
    				System.out.print("* ");
    			}
     
    		}
    		System.out.println("");
     
    		System.out.print(" 91 - 100 | ");
    		for (int k : list) {
    			if (k > 90 && k < 101) {
    				System.out.print("* ");
    			}
     
    		}
    		System.out.println("");
    	}
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Too many loops!

    The for loops take some values between, for ex, 0 and 11 and then prints the amout of numbers between those with asterisks.
    Can you describe in a little detail what the code is supposed to do and how it is going to do it?
    Also post an example output.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Too many loops!

    Quote Originally Posted by Norm View Post
    Can you describe in a little detail what the code is supposed to do and how it is going to do it?
    Also post an example output.
    Yes. The code is supposed to read some integers, one line by line, from a txt file and then trim it to get rid of whitespaces and after that just put them in an array.
    So the txt looks like this :
    34
    25
    12
    5675
    -23
    24
    etc..

    The array is then used to show how many numbers are between, as said, 0 and 11 and then 10 and 21 etc.

    Output looks like this :
    Reads from : C:\Users\me\Desktop\java\X(2).txt
    Between the interval of [0, 100] there are : 53 values. 
    And a rest of : 27 values.
    Histogram :
     1  - 10  | * * * * * 
     11 - 20  | * * * * * 
     21 - 30  | * * * * 
     31 - 40  | * * * * * * * * * 
     41 - 50  | * * * * * * * 
     51 - 60  | * * * 
     61 - 70  | * * * * * 
     71 - 80  | * * * * * * 
     81 - 90  | * * * * * * 
     91 - 100 | * * *
    "Tick, tack"

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Too many loops!

    Ok the numbers range from 1 to 100 and should be counted into 10 groups depending on their values. The count for each group could be saved in an array of 10 elements.
    Is there a simple math formula that will compute which group a number goes into instead of having 10 different if statements?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Too many loops!

    Might be wrong here but.. modulus?
    "Tick, tack"

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Too many loops!

    Try writing a simple program to test the results of some math operators to see how to compute index values from 0 to 9 (or 1 to 10)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: March 12th, 2014, 12:43 PM
  2. Need Help! For loops and if/else
    By kram in forum Loops & Control Statements
    Replies: 2
    Last Post: February 11th, 2012, 06:22 PM
  3. For Loops
    By Shyamz1 in forum Loops & Control Statements
    Replies: 3
    Last Post: September 27th, 2011, 11:54 AM
  4. For loops
    By JCTexas in forum Loops & Control Statements
    Replies: 4
    Last Post: September 21st, 2011, 05:43 PM
  5. help with loops
    By kidza12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 26th, 2011, 11:42 PM