Combing println with for loop
Hello,
I am having trouble combing my println statements with a for loop that prints out stars.
What my program does is read in a file with temperature data on it from different cites. It will increase the counters in an array if they are in a certain range. I have to count how many days it hits that certain range of temperatures and create a star histogram for each range.
for example - this data file for denver shows how many days are given for the specified ranges
Yearly temperature data for Denver
<= 9 ( 3):
10 - 19 ( 4):
20 - 29 ( 9):
30 - 39 ( 27):
40 - 49 ( 47):
50 - 59 ( 66):
60 - 69 ( 62):
70 - 79 ( 44):
80 - 89 ( 66):
90 - 99 ( 37):
>=100 ( 0):
so 3 days were less than 9 degress, 66 days were in the 50's and so forth. I have to construct a star histogram next to these so it can be displayed as a graph. I am able to create the starts according to the array but can't line them up...
***
****
*********
***************************
***********************************************
************************************************** ****************
************************************************** ************
********************************************
************************************************** ****************
*************************************
so the output that i have looks like this so far
/////////////////////////////////////////////////////////////////////
Yearly temperature data for Denver
<= 9 ( 3):
10 - 19 ( 4):
20 - 29 ( 9):
30 - 39 ( 27):
40 - 49 ( 47):
50 - 59 ( 66):
60 - 69 ( 62):
70 - 79 ( 44):
80 - 89 ( 66):
90 - 99 ( 37):
>=100 ( 0):
***
****
*********
***************************
***********************************************
************************************************** ****************
************************************************** ************
********************************************
************************************************** ****************
*************************************
//////////////////////////////////////////////////////////////////////////////////////////////////////
i am trying to make it look like this:
Yearly temperature data for Denver
<= 9 ( 3):***
10 - 19 ( 4):****
20 - 29 ( 9):*********
30 - 39 ( 27):***************************
40 - 49 ( 47):********************************************** *
50 - 59 ( 66):********************************************** ********************
60 - 69 ( 62):********************************************** ****************
70 - 79 ( 44):********************************************
80 - 89 ( 66):********************************************** ********************
90 - 99 ( 37):*************************************
>=100 ( 0):
here is my code
Code :
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class Temperature
{
public static void main(String [] args)
{
//ask the user what city they want to see the temps for
//System.out.println("Enter in the City: ");
//Scanner city = new Scanner(System.in);
String filename = args[0];
Scanner inputStream = null;
int numfile = args.length;
System.out.println(numfile);
//tells you how many files are in the arguments tab and prints out their names
for(int i = 0; i<numfile;i++)
{
System.out.println(args[i]);
}
try
{
inputStream = new Scanner(new File(filename));
}
//
catch(FileNotFoundException e)
{
if(numfile == 0)
{
System.out.println("Error: User did not specify a file name.");
}
else
{
System.out.println("Error: " + filename + " does not exist.");
System.exit(0);
}
//e.printStackTrace();
}
int [] temps = new int[11];
//scans the file for a number
while(inputStream.hasNextInt())
{
//print the numbers of the document on the screen. if no other numbers the loop ends
int num = inputStream.nextInt();
System.out.println(num);
//calculates where the number falls in the range, increase its index every time is finds an occurence of it
if(num <= 9 )
{
temps[0]++;
}
if(num>=10 && num<=19)
{
temps[1]++;
}
if(num>=20 && num<=29)
{
temps[2]++;
}
if(num>=30 && num<=39)
{
temps[3]++;
}
if(num>=40 && num<=49)
{
temps[4]++;
}
if(num>=50 && num<=59)
{
temps[5]++;
}
if(num>=60 && num<=69)
{
temps[6]++;
}
if(num>=70 && num<=79)
{
temps[7]++;
}
if(num>=80 && num<=89)
{
temps[8]++;
}
if(num>=90 && num<=99)
{
temps[9]++;
}
if(num>=100)
{
temps[10]++;
}
}
System.out.println("end!!!!!!!!!!!!");
System.out.println("Yearly temperature data for " + args[0]); //change this to a counter
System.out.println(" <= 9 ( " + temps[0] + "):");
System.out.println("10 - 19 ( " + temps[1] + "):");
System.out.println("20 - 29 ( " + temps[2] + "):");
System.out.println("30 - 39 ( " + temps[3] + "):");
System.out.println("40 - 49 ( " + temps[4] + "):");
System.out.println("50 - 59 ( " + temps[5] + "):");
System.out.println("60 - 69 ( " + temps[6] + "):");
System.out.println("70 - 79 ( " + temps[7] + "):");
System.out.println("80 - 89 ( " + temps[8] + "):");
System.out.println("90 - 99 ( " + temps[9] + "):");
System.out.println(" >=100 ( " + temps[10] + "):");
for(int i = 0;i<temps[i];i++){
for (int j = 0; j<temps[i];j++)
{
System.out.print("*");
}
System.out.println("");
}
}
}
Any advice or help would be greatly appreciated! Thank you!
Re: Combing println with for loop
Hello norske_lab!
If i were you i would make a method that prints the stars (i would change a little bit the for loops...) and call that method after every System.out.println(). There may be a better and simpler way though.
Hope it helps.