The Artima Developer Community
Sponsored Link

Java Answers Forum
Beginner's block

13 replies on 1 page. Most recent reply: Mar 29, 2002 9:04 AM by Patti

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 13 replies on 1 page
Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Beginner's block Posted: Mar 26, 2002 9:28 PM
Reply to this message Reply
Advertisement
I know this should be easy, but I can't quite get it.
I guess I have a beginner's block going. This is supposed to create a histogram with asterisks in the appropriate row for each number entered. Please help.


import java.io.*;
 
class Histogram {
	
	//----------------------------------------------------------
	// Program to display histogram of range of entered numbers.
	//----------------------------------------------------------
	
	public static int number;
	private static BufferedReader stdin;
	
	
	public static void main (String[] args) throws IOException{
 
		int[] count = new int [10];
	    int x = (number - 1)/ 10;
	
	
	   stdin = new BufferedReader (new InputStreamReader(System.in));
       System.out.println( "Enter numbers between 1 and 100, or 0 to quit");
       number = Integer.parseInt (stdin.readLine());
       
     while (number != 0){ //sentinal to stop entering numbers
                  
          number = Integer.parseInt (stdin.readLine());
          
          }
             
      // Loop to generate labels of rows
       for (int row = 0; row < count.length; row++){
         System.out.println ((row * 10 + 1) + "- " + ((row + 1) * 10) + " | ");          
         if (x == count[index])
            System.out.print ('*');
            count[index]++;
       
         
         }    
}//main method
}//class Histogram


Elliott Wood

Posts: 15
Nickname: tooess
Registered: Mar, 2002

Re: Beginner's block Posted: Mar 27, 2002 4:24 PM
Reply to this message Reply
Hi Patti, I've had a go at solving your problem and i've come up with a solution, or at least a part solution.
The code does what I think you want it to except it has a minor problem in that the first number entered isn't counted when the histogram is drawn. The problem is to do with the while loop and at this moment I can't figure a way round it and its getting late so I've given up. The code is

import java.io.*;
class Histogram
{
//----------------------------------------------------------
// Program to display histogram of range of entered numbers.
//----------------------------------------------------------

public static int number;
private static BufferedReader stdin;

public static void main (String[] args) throws IOException
{
int[] count = new int [10];
int x = (number - 1)/ 10;
int index = 0;

stdin = new BufferedReader (new InputStreamReader(System.in));

System.out.println( "Enter numbers between 1 and 100, or type 0 to quit");

number = Integer.parseInt (stdin.readLine());
while ((number != 0) || (index==count.length))
{ //sentinal to stop entering numbers
number = Integer.parseInt (stdin.readLine());
count[index] = number;
index++;
}

// Loop to generate labels of rows
for (int row = 0; row < count.length; row++)
{
String result = ""+ ((row * 10 + 1) + "- " + ((row + 1) * 10) + " | ");

for(int i = 0; i <count.length; i++)
{
if((count >= (row * 10 + 1)) && (count <= (row + 1) * 10))
{
result += '*';
}
}
System.out.println(result);
}
}//main method
}//class Histogram


If I manage to solve the rest I'll let you know the answer. I hope i've been some help. Elliott

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: Beginner's block Posted: Mar 27, 2002 4:57 PM
Reply to this message Reply
Thanks for answering me. I was going nuts!!! I'll try it and see.

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: Beginner's block Posted: Mar 27, 2002 8:01 PM
Reply to this message Reply
It's supposed to use the array to count all the numbers in each range and print out that many asterisks. I can't get it to work.

Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: Beginner's block Posted: Mar 27, 2002 9:05 PM
Reply to this message Reply
Here you go...

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;

public class Histogram {
//----------------------------------------------------------
// Program to display histogram of range of entered numbers.
//----------------------------------------------------------

public static void main(String[] args) throws IOException {
HashMap hm = new HashMap();
int number;
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

System.out.println( "Enter numbers > 0, or type 0 to quit");

while ( (number = Integer.parseInt(stdin.readLine())) > 0) {
String val = (String) hm.get( String.valueOf(number) );

if ( val != null ) {
hm.put(String.valueOf(number), String.valueOf(Integer.parseInt(val)+1));
} else {
hm.put(String.valueOf(number), String.valueOf(1));
}
}

for (Iterator iter = hm.keySet().iterator(); iter.hasNext();){
String key = (String)iter.next();
System.out.print(key+ " : ");
for (int i = 0; i < Integer.parseInt( (String)hm.get(key)); i++){
System.out.print("*");
}

System.out.println();
//System.out.println("Key = "+key+" Value = "+(String)hm.get(key));
}

}//main method
}//class Histogram

Lynn Hollerman

Posts: 67
Nickname: gmholler
Registered: Mar, 2002

Just an aside... Posted: Mar 28, 2002 8:41 AM
Reply to this message Reply
I, too, tried to get Patti's original problem to work, with no success - and I have to confess I did not know what a "HashMap" was. Going to this URL (http://www.esus.com/javaindex/j2se/jdk1.2/javautil/collections/hashmap/hashmap.html) explained it really well - with an example that I could understand! I haven't tried the newest code as yet, but a HashMap seems to be the thing I was thinking of for this situation. I know there's probably someone else with the same thoughts as me - I hope this URL is of some help!

Lynn.

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: Just an aside... Posted: Mar 28, 2002 10:17 AM
Reply to this message Reply
You guys-I HAVE TO USE AN ARRAY-that's the assignment. I have to use the index of the array to count the numbers in each range. I appreciate all your input though. It has been a learning experience.

Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: Just an aside... Posted: Mar 28, 2002 5:03 PM
Reply to this message Reply
Nowhere did you mention that array was supposed to be used. This is from your very first post..

>>> guess I have a beginner's block going. This is supposed to create a histogram with asterisks in the appropriate row for each number entered. Please help.

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: Just an aside... Posted: Mar 28, 2002 5:25 PM
Reply to this message Reply
Yes, I did mention it. You didn't see it. But you're right I should have mentioned it first. It's just that I asked before and got no answers and so I forgot to mention it up front. Sorry.

Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: Just an aside... Posted: Mar 28, 2002 5:43 PM
Reply to this message Reply
Ok. You must have got the idea by now how this problem is to be tackled. Now, this is a small exercise for you...

Create an array which can hold integer values.
Initialize with zeros. (this will represent that no entries in the beginning)
If you get a number within the range 1 to 100, then get the value stored at the index (numberReadFromKeyboard - 1), add 1 to it and put it back in the array.

Printing the stars would be a child's play.

MS

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

OK So everyone hates me now Posted: Mar 28, 2002 6:06 PM
Reply to this message Reply
You guys sure are being mean to little ole me. Some day I might know how to do that.

Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: OK So everyone hates me now Posted: Mar 28, 2002 6:20 PM
Reply to this message Reply
dont get me wrong... no one hates you here. This is for your own good. If you wont do the exercise then how will you cope up with these things in your exams.

Jay Kandy

Posts: 77
Nickname: jay
Registered: Mar, 2002

Re: OK So everyone hates me now Posted: Mar 29, 2002 7:07 AM
Reply to this message Reply
I think I can almost see frustration on your face. Actually you were close to getting it working.

Also, I did not clearly understand what you wanted. I only got an idea from your code. So, here goes:

import java.io.*;
 
class Histogram
{
	//----------------------------------------------------------
	// Program to display histogram of range of entered numbers.
	//----------------------------------------------------------
 
	public static int number = -1;
	private static BufferedReader stdin;
 
	public static void main (String[] args) throws IOException
	{
		int[] count = new int [10];
		/**
		 * 	I am not sure why you need this line:
		 *
		 *	int x = (number - 1)/ 10;
		 *
		 *	So hack it
		 */
 
		stdin = new BufferedReader( new InputStreamReader( System.in ) );
 
		System.out.println( "Enter numbers between 1 and 100, or 0 to quit" );
 
		/**
		 *	New Code. index was not declared
		 */
		int index = 0;
		/**
		 *	Addition ends
		 */
		while (number != 0)//sentinal to stop entering numbers
		{
			number = Integer.parseInt( stdin.readLine() );
			/**
			 * New Code
			 */
			if( number > 0 )
			{
				count[ index ] = number;
				index ++;
			}
			/**
			 *	Addition ends
			 */
		}
 
		// Loop to generate labels of rows
		for (int row = 0; row < count.length; row++)
		{
			System.out.print((row * 10 + 1) + "- " + ((row + 1) * 10) + " | ");
			/**
			 * 	I am not sure why you need these lines:
			 *
			 *	if (x == count[row])
			 *	System.out.print ('*');
			 *	count[row]++;
			 *
			 *  Instead I added these lines:
			 */
			int temp = count[ row ];
			for( int i=0; i < temp ; i++ )
			{
				System.out.print( "*" );
			}
			System.out.println();
		}
	}
}


Sincerely,
Jay

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

I got it !!!!!!! Posted: Mar 29, 2002 9:04 AM
Reply to this message Reply
Thanks so much !!!

Flat View: This topic has 13 replies on 1 page
Topic: Help!!!! Previous Topic   Next Topic Topic: VM/Console blocks

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use