Hello i'm having a problem with a program. Let me tell you first what program should do.

The program should allow the tutor to enter in the various marks which the students have been awarded, until the tutor enters in a mark exceeding 100. At this point the program should display a histogram. Each star represents a student who achieved a module mark in the range shown.
0-29 *** (3 students received a mark between 0-29)
30-39 *****
40-69 ********
70-100 ****
20 students in total.
 As the tutor enters each mark, a counter should count the number of student’s marks which have been entered.
 Use the same 4 category ranges shown here.
 Make sure the display is neatly formatted as above.

this is the problem to be solved.

package cw23rdattempt;

import java.util.Scanner;

/**
 *
 * @author Boss
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        int counter = 1;
        int marks = 0;
        
        
        while( counter <= 20)
        {
        Scanner input = new Scanner(System.in);
        System.out.println("enter student marks");
        marks = input.nextInt();

        if(marks <=29)
            {
            System.out.println("0-29 *");
            }
        if(marks >= 30 && marks <=39)
            {
            System.out.println("30-39 *");
            }
        if(marks >= 40 && marks <=69)
            {
            System.out.println("40-69 *");
            }
        if(marks >= 70 && marks <=100)
            {
            System.out.println("70-100 *");
            }
        if(marks >100)
            {
            System.out.println("invalid mark \nmarks can not exceed 100");
            }
        counter++;
        }//end while
    }//end class
    }//end main

it sort of works, but it print answer separately for every entry, and i want to print it in the format as above in description.
could you please help me out?. ps i'm very new to programing so dont get harsh on me ;p

what you are doing is printing out a single line, depending on what the last mark the user input is, you will have to store all the marks the user has input (maybe into an array?) and then loop through to find out how many are <29 etc etc printing out however many in terms of stars

What you have looks close to what you need. Just some things are a bit out of order.
1. Move line 22 to come before line 20.
2. You need more variables to keep track of your data:
- 1 variable to store the total number of marks entered. I think you are calling that counter.
- 1 variable to store the individual mark entered by the user. I think you are calling that marks.
- 1 variable to store the number of marks between 0-29. Let's call that one low.
- 1 variable to store the number of marks between 30-39. Call that one midLow.
- 1 variable to store the number of marks between 40-69. Call that one midHigh.
- 1 variable to store the number of marks between 70-100. Call that one high.
3. Initialize all these number variables to 0.
4. Inside your while loop, you will read the input from the user like you are already doing. Then you will test the input to see which number variable should be incremented. You will do this with some if/else statements. After that you will end the while loop.
5. After the while loop finishes, you will have 4 more loops to print the results. Before each of these loops you will print the beginning of the line, like "0-29" for example. Then inside each loop, you will print the appropriate number of stars.
6. Finally, after the 4 loops mentioned above, you will print the final number of marks entered by the tutor.

Try to re-write your program based on these instructions. If you get stuck, ask another question.

i'm a bit lost here " Then you will test the input to see which number variable should be incremented " what exactly should i do ?, also how do i print appropriate number of starts when each star represent one student.. i know that if i want to print 3 * i would do

System.out.println("***")

but this is sort of final printout and its not modified when the tutor enters different marks. do you get what i mean :P ?

Do you know how to write an if/else statement?
Do you know how to write a for loop?

You need to understand both of those things.

Unless I'm missing something, there's an easier to do it. (I've got a cold today, so I might have missed something)

Make an array grades[0..maxgrade]. As you get a score, increment grades[score]. Now you have data organized so you can use it.

Simplest way (conceptually) to use it is:
iterate the array. For each element of grades, print that many of *. Add formatting, ie, line breaks and line headers, at appropriate spots.

A more organized method would be to create a method which returns an appropriate set of Strings, nicely formatted, and you just print those. Hide your work. That method's black box might get pretty intricate, depending on how much you want to build into it, but simplest would be to again, iterate the array and build Strings.

A tempting method (the first one the C coder in me reached for) would be to make an array scores[0..maxgrade/10] and for each score entered, increment score/10. This makes my processing much easier. Why don't I want to do it?
(this question directed at the original poster, please allow them a little time to answer!)

jon.kiparsky, your solution does not fit the assignment.

Hrm?
Teacher enters grades, program displays histogram of input. Was there something else?

There are only 4 categories of grades. You suggested creating an array[0..maxgrade]. I'm not even sure the OP knows how to deal with arrays. Since the assignment is only concerned with 4 categories, I'm guessing the teacher hasn't gone over arrays yet.

I'm guessing the teacher hasn't gone over arrays yet.

Well, no time like the present. They're not that hard.
You agree, though, that this does solve the problem that was asked for, right?

No, it doesn't. The assignment asks for 4 categories of grades, not 100.

Read the rest of the post.

Get the data into an array.
Iterate the array of grades, printing line headers and line breaks where appropriate.

So:

for (m from 1 to 100)
{
for (int i from 1 to grades[m]) print "*"
if m is at a break point (30, 60, etc) print a linefeed and line header
}


or, if you'd rather:

print a header, ie "0-29: "
(for i from 1 to first boundary)
{ myTempVariable +=grades;
}
print myTempVariable asterixen,
print newline, header for next line, lather rinse repeat.

More clear now?
Best,
-jpk

since i'm quite new to programming i dont really understand what you just did, also i haven't learned arrays yet.

ok so what i did so far.. =/

i have declared varables

int counter = 1;
        int marks = 0;
        int low = 0;
        int midlow = 0;
        int midhigh = 0;
        int high = 0;

then i have moved the scanner above while statement

Scanner input = new Scanner(System.in);
        System.out.println("enter student marks");

        while( counter <= 5)
        {        
        marks = input.nextInt();

and now what i tried is to make an if/else statement to see if specific condition was met

if(marks <=29)
            {
            low = (int)marks;
            }
            else if(marks >= 30 && marks <= 39)
            {
            midlow = (int)marks;
            }
            else if(marks >= 40 && marks <= 69)
            {
            midhigh = (int)marks;
            }
            else if(marks >= 70 && marks <= 100)
            {
            high = (int)marks;
            }
            else
            {
            System.out.println("invalid mark \nmarks can not exceed 100");
            }

if i write a code like

high = (int)marks;

is this correct in terms of, if a specific condition was met ie. the marks entered was 80, it stores this value in variable named "high" ??

i have a feeling that either i've made it worst then it was, or actually i haven't done anything useful ...

Your if/else conditions are all correct - very good! But what you need to do inside each condition is just increment (add 1 to) the correct variable. For example,

else if(marks >= 30 && marks <= 39)
{
  midlow++;  // or midlow = midlow + 1;
}
high = (int)marks;

This line takes the value stored in "marks", attempts to cast it as an int, and stores the result of that cast as "high". If it's within an if() block, it'll do that if the condition of the if statement evaluated to "true". So, in your case, if marks holds 80, this line would fire, and high would equal 80.
If you don't get a numberFormatException at the input stage, your input value will come as an int, so I don't know that you need the cast there, but it won't do any harm.
Since you're new to this, I won't give you any grief about not checking your input, but try running this and entering something like "e" for one of the grades. This is why we have exception handling, which you'll come to presently.


Now, what does your loop do? This is a good chance to practice reading code.
Pretend you're the computer, and run through your loop, with the values 27, 56, 45, 78, 100, 101, 71. Just use a pencil and paper - make sure there's an eraser on your pencil.
What are the values in low, midlow, mighigh, and high when you're done?

As for arrays, they're a handy way to deal with data like this, which is inherently numerically ordered.
Briefly, an array is a collection of variables referred to by the array name and an index number: if grades[] is an array of ints, then grades[76] is a single int. The index must be an integer, but this can be a literal int (ie, 76) or a variable (n, which maybe equals 76, or equals something else) or anything else that evaluates to an int. This means that you now have data which you can manipulate systematically, instead of in an ad hoc fashion.
so, instead of
if (someInt == 1)

{ do something to variable1 }

else if (someInt == 2)

{ do something to variable2 }
else if (someInt == 3)

{ do something to variable3 }


you have

for (int i = 0; i < [length of array]; i++)
{
do something to array;
}

The advantages of this should be pretty obvious. There are some limitations to arrays as well, but for an application like this, they're indispensible.
For example, you'll notice that in your solution to the problem, you destroy the original data on the way in. There's no way to re-analyze anything without re-entering everything. If you use an array, you preserve the data (number of students receiving a given mark), so later on you could generate a similar histogram for letter grades or to move the boundaries to see if patterns show up.

@ jon.kiparsky
1st. its quite hard to pretend that i'm computer :P
2nd. i can now see why these arrays are handy... less code, and as you said it preserves the data for later use. i will have a look at arrays soon maybe in couple of days, until i'll get confident with the easier stuff before

also i think i sort of cracked it ....

package cw23rdattempt;

import java.util.Scanner;
import java.util.*;
/**
 *
 * @author Boss
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        int counter = 1;
        int marks = 0;
        int low = 0;
        int midlow = 0;
        int midhigh = 0;
        int high = 0;

        Scanner input = new Scanner(System.in);
        System.out.println("enter student marks");

        while( counter <= 20)
        {        
        marks = input.nextInt();

        if(marks <=29){
            low++;
            }else if(marks >= 30 && marks <= 39){
            midlow++;
            }else if(marks >= 40 && marks <= 69){
            midhigh++;
            }else if(marks >= 70 && marks <= 100){
            high++;
            }else{
            System.out.println("invalid mark \nmarks can not exceed 100");
            }
        counter++;
        }//end while
        System.out.print("0-29 ");        
            if(low == 1){
                System.out.println("*");
            } else if (low == 2) {
                System.out.println("**");
            } else if (low == 3) {
                System.out.println("***");
            } else if (low == 4) {
                System.out.println("****");
            } else if (low == 5) {
                System.out.println("*****");
            }else if (low == 6) {
                System.out.println("******");
            } else if (low == 7) {
                System.out.println("*******");
            } else if (low == 8) {
                System.out.println("********");
            } else if (low == 9) {
                System.out.println("*********");
            }else if (low == 10) {
                System.out.println("**********");
            } else if (low == 11) {
                System.out.println("***********");
            } else if (low == 12) {
                System.out.println("************");
            } else if (low == 13) {
                System.out.println("*************");
            }else if (low == 14) {
                System.out.println("**************");
            } else if (low == 15) {
                System.out.println("***************");
            } else if (low == 16) {
                System.out.println("****************");
            } else if (low == 17) {
                System.out.println("*****************");
            }else if (low == 18) {
                System.out.println("******************");
            } else if (low == 19) {
                System.out.println("*******************");
            } else if (low == 20) {
                System.out.println("********************");
            } //end if(low)
        System.out.print("30-39 ");            
            if(midlow == 1){
                System.out.println("*");
            } else if (midlow == 2) {
                System.out.println("**");
            } else if (midlow == 3) {
                System.out.println("***");
            } else if (midlow == 4) {
                System.out.println("****");
            } else if (midlow == 5) {
                System.out.println("*****");
            }else if (midlow == 6) {
                System.out.println("******");
            } else if (midlow == 7) {
                System.out.println("*******");
            } else if (midlow == 8) {
                System.out.println("********");
            } else if (midlow == 9) {
                System.out.println("*********");
            }else if (midlow == 10) {
                System.out.println("**********");
            } else if (midlow == 11) {
                System.out.println("***********");
            } else if (midlow == 12) {
                System.out.println("************");
            } else if (midlow == 13) {
                System.out.println("*************");
            }else if (midlow == 14) {
                System.out.println("**************");
            } else if (midlow == 15) {
                System.out.println("***************");
            } else if (midlow == 16) {
                System.out.println("****************");
            } else if (midlow == 17) {
                System.out.println("*****************");
            }else if (midlow == 18) {
                System.out.println("******************");
            } else if (midlow == 19) {
                System.out.println("*******************");
            } else if (midlow == 20) {
                System.out.println("********************");
            }//end if(midlow)
        System.out.print("40-69 ");
            if(midhigh == 1){
                System.out.println("*");
            } else if (midhigh == 2) {
                System.out.println("**");
            } else if (midhigh == 3) {
                System.out.println("***");
            } else if (midhigh == 4) {
                System.out.println("****");
            } else if (midhigh == 5) {
                System.out.println("*****");
            }else if (midhigh == 6) {
                System.out.println("******");
            } else if (midhigh == 7) {
                System.out.println("*******");
            } else if (midhigh == 8) {
                System.out.println("********");
            } else if (midhigh == 9) {
                System.out.println("*********");
            }else if (midhigh == 10) {
                System.out.println("**********");
            } else if (midhigh == 11) {
                System.out.println("***********");
            } else if (midhigh == 12) {
                System.out.println("************");
            } else if (midhigh == 13) {
                System.out.println("*************");
            }else if (midhigh == 14) {
                System.out.println("**************");
            } else if (midhigh == 15) {
                System.out.println("***************");
            } else if (midhigh == 16) {
                System.out.println("****************");
            } else if (midhigh == 17) {
                System.out.println("*****************");
            }else if (midhigh == 18) {
                System.out.println("******************");
            } else if (midhigh == 19) {
                System.out.println("*******************");
            } else if (midhigh == 20) {
                System.out.println("********************");
            }//end if(midhigh)
        System.out.print("70-100 ");
            if(high == 1){
                System.out.println("*");
            } else if (high == 2) {
                System.out.println("**");
            } else if (high == 3) {
                System.out.println("***");  
            } else if (high == 4) {
                System.out.println("****");
            } else if (high == 5) {
                System.out.println("*****");
            }else if (high == 6) {
                System.out.println("******");
            } else if (high == 7) {
                System.out.println("*******");
            } else if (high == 8) {
                System.out.println("********");
            } else if (high == 9) {
                System.out.println("*********");
            }else if (high == 10) {
                System.out.println("**********");
            } else if (high == 11) {
                System.out.println("***********");
            } else if (high == 12) {
                System.out.println("************");
            } else if (high == 13) {
                System.out.println("*************");
            }else if (high == 14) {
                System.out.println("**************");
            } else if (high == 15) {
                System.out.println("***************");
            } else if (high == 16) {
                System.out.println("****************");
            } else if (high == 17) {
                System.out.println("*****************");
            }else if (high == 18) {
                System.out.println("******************");
            } else if (high == 19) {
                System.out.println("*******************");
            } else if (high == 20) {
                System.out.println("********************");
            }// end if(high)
    }//end class
    }//end main

Oh, dear.
Do you know about for loops yet? They're a specialized case of the while loop. You need them badly here.

A for loop takes this while loop:

int i = 0;
while (i<n)
{
  // do some stuff
  
  ++ i;
}

and rewrites it as

for (int i = 0; i <n; i++)
{
  // do some stuff
}

In both cases, i is initialized to zero, then as long as i is less than n (which has some value, in this example we don't know where it came from) the body of the loop will be executed and then i will be incremented.


One of the things you can do with this is to do a thing n number of times. For example, you could print a single "*"... without a line break... Are you seeing what I'm getting at?

its quite hard to pretend that i'm computer

You need to be able to execute code in your head, and you learn to do that by executing it on paper.
Go back to that loop and try it. Take a piece of paper, and write down the name of each variable. What is its value when you enter the loop? If you haven't assigned it to anything, only declared it, what is its value? If you don't know, how would you find out?
Okay, now you know the initial states. Then, walk through the loop with the values I gave you, and when you set a value, write it down. Probably you already know what you'll see when you do this, but do it anyway, you need the practice.
Anytime a piece of code isn't doing what you want it to do, this is your first resort. The computer is doing what you've told it to do, so go figure out what you told it to do. You might be surprised.

no i havent got to the for loop yet, but i'll read about it tomorrow perhaps and then i'll try to modify my program :). and yes i can see what you mean...but since i don't know YET how to do it i'll leave this code for now as it is :)
also i'll work through the code on paper tomorrow since i'm done for today and its time for me ;>, so have a good night and anyway thanks for all help very appreciate it

My pleasure, good luck with that.

@ jon.kiparsky
1st. its quite hard to pretend that i'm computer :P
2nd. i can now see why these arrays are handy... less code, and as you said it preserves the data for later use. i will have a look at arrays soon maybe in couple of days, until i'll get confident with the easier stuff before

also i think i sort of cracked it ....

package cw23rdattempt;

import java.util.Scanner;
import java.util.*;
/**
 *
 * @author Boss
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        int counter = 1;
        int marks = 0;
        int low = 0;
        int midlow = 0;
        int midhigh = 0;
        int high = 0;

        Scanner input = new Scanner(System.in);
        System.out.println("enter student marks");

        while( counter <= 20)
        {        
        marks = input.nextInt();

        if(marks <=29){
            low++;
            }else if(marks >= 30 && marks <= 39){
            midlow++;
            }else if(marks >= 40 && marks <= 69){
            midhigh++;
            }else if(marks >= 70 && marks <= 100){
            high++;
            }else{
            System.out.println("invalid mark \nmarks can not exceed 100");
            }
        counter++;
        }//end while
        System.out.print("0-29 ");        
            if(low == 1){
                System.out.println("*");
            } else if (low == 2) {
                System.out.println("**");
            } else if (low == 3) {
                System.out.println("***");
            } else if (low == 4) {
                System.out.println("****");
            } else if (low == 5) {
                System.out.println("*****");
            }else if (low == 6) {
                System.out.println("******");
            } else if (low == 7) {
                System.out.println("*******");
            } else if (low == 8) {
                System.out.println("********");
            } else if (low == 9) {
                System.out.println("*********");
            }else if (low == 10) {
                System.out.println("**********");
            } else if (low == 11) {
                System.out.println("***********");
            } else if (low == 12) {
                System.out.println("************");
            } else if (low == 13) {
                System.out.println("*************");
            }else if (low == 14) {
                System.out.println("**************");
            } else if (low == 15) {
                System.out.println("***************");
            } else if (low == 16) {
                System.out.println("****************");
            } else if (low == 17) {
                System.out.println("*****************");
            }else if (low == 18) {
                System.out.println("******************");
            } else if (low == 19) {
                System.out.println("*******************");
            } else if (low == 20) {
                System.out.println("********************");
            } //end if(low)
        System.out.print("30-39 ");            
            if(midlow == 1){
                System.out.println("*");
            } else if (midlow == 2) {
                System.out.println("**");
            } else if (midlow == 3) {
                System.out.println("***");
            } else if (midlow == 4) {
                System.out.println("****");
            } else if (midlow == 5) {
                System.out.println("*****");
            }else if (midlow == 6) {
                System.out.println("******");
            } else if (midlow == 7) {
                System.out.println("*******");
            } else if (midlow == 8) {
                System.out.println("********");
            } else if (midlow == 9) {
                System.out.println("*********");
            }else if (midlow == 10) {
                System.out.println("**********");
            } else if (midlow == 11) {
                System.out.println("***********");
            } else if (midlow == 12) {
                System.out.println("************");
            } else if (midlow == 13) {
                System.out.println("*************");
            }else if (midlow == 14) {
                System.out.println("**************");
            } else if (midlow == 15) {
                System.out.println("***************");
            } else if (midlow == 16) {
                System.out.println("****************");
            } else if (midlow == 17) {
                System.out.println("*****************");
            }else if (midlow == 18) {
                System.out.println("******************");
            } else if (midlow == 19) {
                System.out.println("*******************");
            } else if (midlow == 20) {
                System.out.println("********************");
            }//end if(midlow)
        System.out.print("40-69 ");
            if(midhigh == 1){
                System.out.println("*");
            } else if (midhigh == 2) {
                System.out.println("**");
            } else if (midhigh == 3) {
                System.out.println("***");
            } else if (midhigh == 4) {
                System.out.println("****");
            } else if (midhigh == 5) {
                System.out.println("*****");
            }else if (midhigh == 6) {
                System.out.println("******");
            } else if (midhigh == 7) {
                System.out.println("*******");
            } else if (midhigh == 8) {
                System.out.println("********");
            } else if (midhigh == 9) {
                System.out.println("*********");
            }else if (midhigh == 10) {
                System.out.println("**********");
            } else if (midhigh == 11) {
                System.out.println("***********");
            } else if (midhigh == 12) {
                System.out.println("************");
            } else if (midhigh == 13) {
                System.out.println("*************");
            }else if (midhigh == 14) {
                System.out.println("**************");
            } else if (midhigh == 15) {
                System.out.println("***************");
            } else if (midhigh == 16) {
                System.out.println("****************");
            } else if (midhigh == 17) {
                System.out.println("*****************");
            }else if (midhigh == 18) {
                System.out.println("******************");
            } else if (midhigh == 19) {
                System.out.println("*******************");
            } else if (midhigh == 20) {
                System.out.println("********************");
            }//end if(midhigh)
        System.out.print("70-100 ");
            if(high == 1){
                System.out.println("*");
            } else if (high == 2) {
                System.out.println("**");
            } else if (high == 3) {
                System.out.println("***");  
            } else if (high == 4) {
                System.out.println("****");
            } else if (high == 5) {
                System.out.println("*****");
            }else if (high == 6) {
                System.out.println("******");
            } else if (high == 7) {
                System.out.println("*******");
            } else if (high == 8) {
                System.out.println("********");
            } else if (high == 9) {
                System.out.println("*********");
            }else if (high == 10) {
                System.out.println("**********");
            } else if (high == 11) {
                System.out.println("***********");
            } else if (high == 12) {
                System.out.println("************");
            } else if (high == 13) {
                System.out.println("*************");
            }else if (high == 14) {
                System.out.println("**************");
            } else if (high == 15) {
                System.out.println("***************");
            } else if (high == 16) {
                System.out.println("****************");
            } else if (high == 17) {
                System.out.println("*****************");
            }else if (high == 18) {
                System.out.println("******************");
            } else if (high == 19) {
                System.out.println("*******************");
            } else if (high == 20) {
                System.out.println("********************");
            }// end if(high)
    }//end class
    }//end main

Good! you got it!

I think this way of doing it (storing it as low, high etc and increasing them by one) is a better implementation than using arrays, which is the first way I thought of doing it.

I think the next thing to do, (as you already know) is to use a for loop to print out the stars. You could also attempt at doing the same assignment with arrays - its more versatile, say the user might want to know the average mark? (you could work this out as you go along I suppose...)

ook, so i've been looking at the for loop statement, and i've been trying to play around with the code, but i'm a bit stuck.

for(low = 0;condition ?;low++)
        {
            System.out.print("*");
        }

i'm trying to print out a number of starts relevant to number of people that has gained a specific mark... but i'm a bit unsure what to put in the condition... couse last time i tried to put number of things lots of stars where printed out ;/

for(low = 0;condition ?;low++)
        {
            System.out.print("*");
        }

This loop will do two things repeatedly, as long as [condition] is true. It will print one more "*" to the output, without a line feed, and it will increment the variable low. What happens if the condition specifies that low is below a certain value?
Suppose you use "low < 5" as the condition - run through that in your head before you plug it in to the program.

You should also keep in mind that this code will never print an end of line character, so if it produces (say) "*****" and you follow it with another loop, like

for(middle= 0;condition ?;middle++)
        {
            System.out.print("*");
        }

which produces (for example) "***" the output will be

********

You'd have to put a blank println() or a print("\n") - same thing - between the two to get separate rows:
*****
***

which might be what you'd mean.

ok so looking back at my previous code ( the looong one)
we have 20 students

public static void main(String[] args) {
        int marks = 0;
        int counter = 0;
        int low = 0;
        int midlow = 0;
        int midhigh = 0;
        int high = 0;
        int total = 0;
        int average = 0;
        int star = 0;

        Scanner input = new Scanner(System.in);
        System.out.println("enter student marks");

        marks = input.nextInt();

        while( counter <= 20)
        {
        marks = input.nextInt();

        if(marks <=29){
            low++;
            }else if(marks >= 30 && marks <= 39){
            midlow++;
            }else if(marks >= 40 && marks <= 69){
            midhigh++;
            }else if(marks >= 70 && marks <= 100){
            high++;
            }else{
            System.out.println("invalid mark \nmarks can not exceed 100");
            }
        total = total + marks;
        counter++;        
        
        }//end while loop
        average = total/20;

        System.out.print("0-29 ");
        for(low = 0;low <=20;low++)
        {
            System.out.print("*");
        }
        System.out.println();
        System.out.print("30-39");
        for(midlow = 0;midlow <=20;midlow++)
        {
            System.out.print("*");
        }
        System.out.println();
        System.out.print("40-69");
        for(midhigh = 0;midhigh <=20;midhigh++)
        {
            System.out.print("*");
        }
        System.out.println();
        System.out.print("70-100");
        for(high = 0;high <=20;high++)
        {
            System.out.print("*");
        }
            
    }

and let say i enter marks such as 10,10,20,20,30,30,40,40,50,50,60,60,70,70,80,80,90,90,100,100
the histogram that i got from this code is not what i want... it prints out the same number of stars for every mark range.

Yep. It prints out 20 of them for each.

Now, how would you get it to print out "low" number of stars?

(hint: you'll have to use a different variable for the loop index)

honestly i dont know ;/

Fair enough - best way to learn something is to start from "I don't know".

So: A for loop is a while loop.

for(middle= 0;middle<=20;middle++)
        {
            System.out.print("*");
        }

is equivalent to

middle= 0;
while(middle <= 20)
        {
            System.out.print("*");
            middle++;
        }

Now, this does a constant number of loops. If I want to use a variable number of loops, I have to use a variable in the "condition" part. In this case, you want to print, for example, "middle" stars - whatever the value of "middle", that's how many stars you want to print.
So you need some variable (not middle!) to range from 0 to middle-1 (that's the way we write loops, normally: for (int i = 0; i<limit; i++) ).


That is, as long as i is still less than limit, keep doing whatever's in the body of the loop statement, and then increment i and check it again. So if your limit is the value of "low", what does your loop statement look like?

well if my limit is "low" then the loop would look like

for(bla=0;bla<=low;bla++)

?? and then i replace the word bla with anything such as star or any other word...? ;> would that be correct ?

That's right. And in the body, you'd have "System.out.print("*"); Watch the condition, though: this will be true low+1 times. Suppose low==5, then it'll go 0, 1, 2, 3, 4, 5, done - six times through.
You want less than in the condition, not less-equals.


Your bla can be any int variable. (or a short or long, technically) Typically, it's declared in the loop declaration, so it only has scope in the loop:
for (int bla = 0; bla < low; bla++)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.