Results 1 to 4 of 4
  1. #1
    supernowlin is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Histogram java problem

    My assignment is

    8.3: Histogram
    Design and implement an application that creates a histogram that allows you to visually inspect the frequency distribution of a set of values . The program should read in an arbitrary number of integers that are in the range 1 to 100 inclusive; then produce a chart similar to the one below that indicates how many input values fell in the range 1 to 10, 11 to 20, and so on. Print one asterisk for each value entered.

    No input prompt
    Terminate input by typing CTRL/Z (two keys typed at the same time) on a separate input line (use CTRL/D on Linux/UNIX systems)
    Use hasNextInt() to terminate your input
    Format as below (slightly different from the text example)
    Z:\dbraffitt\Week10> javac Histogram.java
    Z:\dbraffitt\Week10> java Histogram
    10 10 10 10 10 20 20 20 20 20 20
    20 25 35 45 55 65 75 85 95
    ctrl/z
    1 - 10 | *****
    11 - 20 | *******
    21 - 30 | *
    31 - 40 | *
    41 - 50 | *
    51 - 60 | *
    61 - 70 | *
    71 - 80 | *
    81 - 90 | *
    91 - 100 | *

    What I can not figure out is how to use hasNextInt() to terminate the loop. How to not have an input prompt. How to use ctrl z to terminate the input. Or to make it where it doesn't involve range values like -1. The problem is due today at 8pm and I really need help.

    Java Code:
    import java.util.Scanner;
    
    public class Histogram
    {
    	public static void main (String[] args)
    	{
    		Scanner scan = new Scanner (System.in);
    		
    	int [] nums = new int[101];
    	
    	System.out.println("Numbers between 1 and 100: ");
    	int num = scan.nextInt();
    	
    	int varb = 0;
    	
    	while (num !=0)
    	{
    		nums[num]++;
    		num = scan.nextInt();
    	}
    	
    	for (int count = 1; count <= 100;count+=10)
    	{
    		System.out.print (count + " - " + (varb+=10) + " | " );
    	}
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Histogram java problem

    Is the code supposed to ask for each number or only ask once and take what the user enters.
    If the code is taking what the user enters in a loop, when should should the loop end?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Histogram java problem

    Also posted at: Histogram java problem
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    supernowlin is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Re: Histogram java problem

    Norm its supposed to keep asking for numbers until you enter crtl z

Similar Threads

  1. Problem with a Simple Histogram Problem
    By kathmandu in forum New To Java
    Replies: 12
    Last Post: 06-25-2013, 02:19 AM
  2. Java Histogram program
    By rodjav in forum New To Java
    Replies: 15
    Last Post: 08-30-2012, 03:26 PM
  3. need help with name histogram
    By cachico12 in forum New To Java
    Replies: 1
    Last Post: 05-09-2011, 09:27 AM
  4. histogram program in java
    By deepaktomar in forum New To Java
    Replies: 3
    Last Post: 03-05-2011, 05:27 PM
  5. Replies: 8
    Last Post: 11-24-2009, 10:03 PM

Posting Permissions

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