Hello everyone, I am AP COMPSCI A for my school and I trouble with this program. In the program you have to enter numbers from 1-100. And print a chart of how many numbers were between 1-10, 11-20 and so on. Like this:
1 - 10 |*
11- 20 |****
22- 30 |**
etc,
but for some reason my code doesn't work

/**
 * @(#)NumChart.java
 *
 * NumChart application
 *
 * @author 
 * @version 1.00 2008/12/26
 */
import java.util.Scanner; 
 
public class NumChart {
    
    public static void main(String[] args) {
    Scanner scan = new Scanner (System.in);
    int[] num= new int[15];
    for(int i =0; i>15;i++)
    {
    	System.out.print("Enter a number between 1-100: ");
    	num[i]= scan.nextInt();
    	while(i<1||i>100)
    	{
    		System.out.print("1-100 only please, Enter again: ");
    		num[i]= scan.nextInt();
    	}	
    }	
    int[] star= new int[10];
    for(int j=0;j>10;j++)
    	star[j]=0;
    	
    for(int p=0;p<15;p++)
    {
    if(num[p]>=1&&num[p]<=10)
    	star[0]++;
    else if(num[p]>=11&&num[p]<=20)
    	star[1]++;
    else if(num[p]>=21&&num[p]<=30)
    	star[2]++;
    else if(num[p]>=31&&num[p]<=40)
    	star[3]++;
    else if(num[p]>=41&&num[p]<=50)
    	star[4]++;
    else if(num[p]>=51&&num[p]<=60)
    	star[5]++;	
    else if(num[p]>=61&&num[p]<=70)
    	star[6]++;
    else if(num[p]>=71&&num[p]<=80)
    	star[7]++;
    else if(num[p]>=81&&num[p]<=90)
    	star[8]++;
    else if(num[p]>=91&&num[p]<=100)
    	star[9]++;
    }
    System.out.print("1  - 10 |  ");
    for(int s=0;s<star[0];s++)
    {
    	System.out.print('*');
    }
    	
    }
}

Recommended Answers

All 4 Replies

The line

for(int i =0; i>15;i++)

is incorrect. It says, start at i=0, and while i>15 do the following code and add one to i. If i starts at 0 it is never greater than 15, so the code will never execute.

You have a similar error with the line

for(int j=0;j>10;j++)

The line

for(int i =0; i>15;i++)

is incorrect. It says, start at i=0, and while i>15 do the following code and add one to i. If i starts at 0 it is never greater than 15, so the code will never execute.

You have a similar error with the line

for(int j=0;j>10;j++)

Thank you so much! You saved me!

I think you could also save yourself a lot of the if statements if you used a counter to index the correct part of the array along with modulus and/or division.

Indeed; the entire point of the exercise was to make use of basic math tricks to come up with a compact solution and to exercise the basics of OOP.

Create a class which bears the responsibility of accepting a number and maintaining the state of which range appears how many times.

Like previously mentioned, use an array to maintain the occurrence count and calculate the range based on the number inputted rather than having thousands of conditionals.

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.