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 5 of 5

Thread: Histogram java problem

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

    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
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Histogram java problem

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

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Histogram java problem

    What happens if you try while ( scan.hasNextInt() )?

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Histogram java problem

    Im actually really bad at programming. I haven't tried it yet. Im not sure how to incorporate it but ill give it a go.

    --- Update ---

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

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Histogram java problem

    "Really bad at programming" is what most (all?) people are before they start studying and trying things. However, most who become proficient probably never thought of themselves as really bad at it. You shouldn't either.

Similar Threads

  1. Histogram equalization Help Please!
    By dazzle1218 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 12th, 2013, 04:08 PM
  2. Array Histogram
    By Thor in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 4th, 2012, 08:06 AM
  3. [SOLVED] Printing A Histogram...
    By Nuggets in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 18th, 2012, 02:11 PM
  4. [SOLVED] BufferReader, histogram
    By Nhedro in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: February 22nd, 2012, 09:39 AM
  5. Histogram Java Program
    By djl1990 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: October 24th, 2011, 06:47 AM