Program does not work

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 25
Reputation: Alishaikh is an unknown quantity at this point 
Solved Threads: 0
Alishaikh Alishaikh is offline Offline
Light Poster

Program does not work

 
0
  #1
Dec 26th, 2008
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('*');
    }
    	
    }
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 807
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Practically a Posting Shark

Re: Program does not work

 
0
  #2
Dec 26th, 2008
The line
  1. 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
  1. for(int j=0;j>10;j++)
Last edited by darkagn; Dec 26th, 2008 at 8:04 pm. Reason: Found a second error
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 25
Reputation: Alishaikh is an unknown quantity at this point 
Solved Threads: 0
Alishaikh Alishaikh is offline Offline
Light Poster

Re: Program does not work

 
0
  #3
Dec 26th, 2008
Originally Posted by darkagn View Post
The line
  1. 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
  1. for(int j=0;j>10;j++)
Thank you so much! You saved me!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,657
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Program does not work

 
0
  #4
Dec 26th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,651
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Program does not work

 
0
  #5
Dec 27th, 2008
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.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 413 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC