5 Replies - 1923 Views - Last Post: 12 July 2009 - 02:20 PM Rate Topic: -----

#1 Weazel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 11-July 09

Java Histogram help...

Posted 11 July 2009 - 09:59 PM

Hi, I am having problems with a histogram program.
I labeled the problems in the coding.

Problem #1 is easy and I'm probably just missing it. I have to key entries between 0 - 500 and if the entry doesn't fall with in that range it should be kicked back for a valid entry. This should also keep with the correct valid entries for the average.

Problem #2 I'm totally at a loss. It should display as follows:
Histogram of 10 inputs:

0 - 99: **
100 - 199: *
200 - 299: ++
300 - 399: ***
400 - 500: **

Average: 271.4


the asterisk[] coding i have is messing with me. I can not figure what to put in [] to get it to display properly.

And Problem #3 is how to get to display the * as + for the range the average falls under.

Thanks, any help is appreciated.


import java.util.Scanner;

public class project2histogram2{

  public static void main(String[] args){
  
	int[] rangeInput;
	 
	 rangeInput = getRangeData(); //Method 1 - Input
	 processRangeData(rangeInput); //Method 2 - Output
	 
  }
  
  //Method 1
  public static int[] getRangeData(){
	//User input number of inputs to be entered.
	Scanner input1 = new Scanner(System.in);
	 System.out.println("Number of inputs: ");
	 int numInputs = input1.nextInt();
	 
	 Scanner input2 = new Scanner(System.in);
	 
	 int[] rangeInput = new int[numInputs];
	 
	 System.out.println("Enter " + rangeInput.length + " numbers to be sorted: ");
	 for(int i = 0; i < rangeInput.length; i++){
	   if(rangeInput[i] < 0 || rangeInput[i] > 500)
		  System.out.println("Please specifiy a number between 0 and 500.");
		//Problem #1
		//above is suppose to delete and replace invalid entries.
		else{
		  if (rangeInput[i] >= 0 || rangeInput[i] <= 500)
		  System.out.println("You entered: " + rangeInput[i]);
		}
		
		rangeInput[i] = input2.nextInt();
	 }
	 return rangeInput;
  }
  
  
  //Method 2
  public static void processRangeData(int[] rangeInput){
	double averageRange = 0;
	 int num = 0;
	 int count = 0;
	 
	 
	 	 
	 //Calculates average		 	 
	 for(int i = 0; i < rangeInput.length; ++i){
	   averageRange += rangeInput[i];
	 }
	 averageRange /= rangeInput.length;
	 
	 //Outputs Array Length
	 System.out.println("Histogram of " + rangeInput.length + " inputs: ");
	 
	 //0-500 Display
	 String[] asterisk = {"  0-  99 | ", "100- 199 | ", "200- 299 | ", "300- 399 | ", "400- 500 |"}; 
	  for (int i = 0; i < rangeInput.length; ++i){
		num=rangeInput[i];
		  
		 if (num >=0 && num <= 99) asterisk [0] +="*";
		else if (num >= 100 && num <= 199) asterisk[1] +="*";
		  else if (num >= 200 && num <= 299) asterisk[2] +="*";
		  else if (num >= 300 && num <= 399) asterisk[3] +="*";
		  else if (num >= 400 && num <= 500) asterisk[4] +="*";
		
	  }
	  for (int i =0;i < rangeInput.length;++i)
	  System.out.println(asterisk[]);
		// Problem #2
		// asterisk[?] can't get to display properly.
	
	 //Display Average
	System.out.printf("\nAverage is: %.2f", averageRange, ".");
		
	 
  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Java Histogram help...

#2 scrat   User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 18
  • Joined: 30-June 09

Re: Java Histogram help...

Posted 12 July 2009 - 05:19 AM

I'm not sure I understand enough about problems 1 and 3 to answer, but I think this is what you are looking for for problem 2:

	  for (int i = 0; i < rangeInput.length; ++i) {
		  for (int j = 0; j < asterisk.length; j++) {
			  System.out.println(asterisk[j]);
		  }
	  }



Good luck,
Scrat
Was This Post Helpful? 0
  • +
  • -

#3 Weazel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 11-July 09

Re: Java Histogram help...

Posted 12 July 2009 - 01:03 PM

Thanks Scrat, your suggestion worked. Had to change it a little, just take off the first line of code, and Ding. :D
Was This Post Helpful? 0
  • +
  • -

#4 Weazel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 11-July 09

Re: Java Histogram help...

Posted 12 July 2009 - 01:53 PM

Also figured out problem #1. Still have no clue as to how to have the average range to show + instead of *.
:blink:
Was This Post Helpful? 0
  • +
  • -

#5 jpav   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 08-July 09

Re: Java Histogram help...

Posted 12 July 2009 - 02:01 PM

View PostWeazel, on 11 Jul, 2009 - 08:59 PM, said:

Problem #1 is easy and I'm probably just missing it. I have to key entries between 0 - 500 and if the entry doesn't fall with in that range it should be kicked back for a valid entry. This should also keep with the correct valid entries for the average.


Your first problem is occurring because you're checking the values in your output before it's populated. Your loop needs to be interrogating input2, not the rangeInput element since that doesn't get set to the value in input2 until after the if statements. I haven't tried this in an IDE, but I'm guessing what your seeing is "You entered: 0" regardless of what you input. You don't need to worry about deleting or replacing values in your array since they haven't been set yet, but you would need to put your "Enter XXX numbers..." statement in the loop to allow for re-entry of invalid values.

View PostWeazel, on 11 Jul, 2009 - 08:59 PM, said:

And Problem #3 is how to get to display the * as + for the range the average falls under.


It wouldn't be terribly efficient, but a quick and dirty way would be to code another if-else block after the one you already have that replaces each
asterisk[x] +="*"

with
asterisk[x] = asterisk[x].replace('*', '+')

checking averageRange instead of num.

View PostWeazel, on 12 Jul, 2009 - 12:53 PM, said:

Also figured out problem #1. Still have no clue as to how to have the average range to show + instead of *.
:blink:

Oh, sorry, didn't see this post for some reason, so ignore the first part of my last reply. :)
Was This Post Helpful? 0
  • +
  • -

#6 Weazel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 11-July 09

Re: Java Histogram help...

Posted 12 July 2009 - 02:20 PM

thanks Jpav, that worked out great, totally should have thought of it myself, but I was obviously making it harder than it was LOL. ain't pretty like you said bu it does the job. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1