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

Thread: Combing println with for loop

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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


    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!
    Last edited by norske_lab; March 25th, 2012 at 12:40 PM. Reason: show stars


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default 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.

Similar Threads

  1. [SOLVED] println() for JTextArea
    By KILL3RTACO in forum AWT / Java Swing
    Replies: 4
    Last Post: November 28th, 2011, 06:15 PM
  2. Combing/Decoding MPEG and Other Video Format?
    By MrFish in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 5th, 2011, 08:21 AM
  3. How to modify System.out.println()
    By emailkia in forum Java Theory & Questions
    Replies: 6
    Last Post: April 25th, 2011, 12:40 AM
  4. what is System,out,println in System.out.println()?
    By koteshk in forum Java Theory & Questions
    Replies: 2
    Last Post: April 18th, 2011, 12:28 AM
  5. println class
    By javanub:( in forum Java Theory & Questions
    Replies: 9
    Last Post: May 18th, 2010, 01:18 AM