Results 1 to 13 of 13
  1. #1
    Argyros is offline Member
    Join Date
    Aug 2014
    Posts
    10
    Rep Power
    0

    Default Help with using printf

    I can't seem to get printf down to format my code right.
    So far this is what I have
    Java Code:
    /*
     * Heat Index description:
     *
     * @author
     * @version
     *
     */
     
    import java.util.Scanner;
    import java.io.File;
    import java.io.IOException;
     
    public class HeatIndex
    {
        public static void main(String[] args) throws IOException
        {
            //initialize and declare vars
     
            String location = "Key West, Florida";
            File fileNameTemp = new File("KeyWestTemp.txt");
            File fileNameHumid = new File("KeyWestHumid.txt");
     
            Scanner inFileTemp = new Scanner(fileNameTemp);
            Scanner inFileHumid = new Scanner(fileNameHumid);
     
            String [] month = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "Novermber", "December"};
            int length = month.length;
            double [] temperature = new double[length];
            int [] humidity = new int[length];
            double [] hI = new double[length];
     
     
                    //INPUT - read in data for temp and humidity from files
     
                    int n = 0; //index value for arrays
            while( inFileTemp.hasNextDouble() )
            {
                temperature[n] = inFileTemp.nextDouble( );
                //System.out.println (temperature[n]); //debug statement - uncomment to see values assignned to temperature
                n++;
            }
            inFileTemp.close();
     
     
            n = 0; //reset index to 0
            while (inFileHumid.hasNextDouble())
            {
                    humidity[n] = inFileHumid.nextInt( );
                    //System.out.println (humidity[n]);  //debug statement - uncomment to see values assignned to humidity
                    n++;
            }
            inFileHumid.close();
     
                    //PROCESSING - calculate Heat Index if needed- see lecture notes for details, formula is incomplete
     
                    double t = 0.0;
                    int h = 0;
     
            for(n = 0; n < hI.length; n++)
            {
                if( temperature[n] >= 80.0  ) //determine if HI should be calculated, complete the condition based on Lecture notes
                {
                    t = temperature[n];
                    h = humidity[n];
     
                    hI[n] = -42.379 + (2.04901523 * t) + (10.14333127 * h) - (0.22475541 * t * h)
                                    - (6.83783 * Math.pow(10, -3) * Math.pow(t, 2)) - (5.481717*Math.pow(10, -2) * Math.pow(h, 2))
                                    + (1.22874 * Math.pow(10, -3) * Math.pow(t, 2) * h) + (8.5282 * Math.pow(10, -4) * t * Math.pow(h, 2))
                                    - (1.99 * Math.pow(10, -6) * Math.pow(t, 2) * Math.pow(h, 2));
                }
                else  // HI is the temperature value
                {
                    hI[n] = temperature[n];
                }
            }
     
     
            //calculate averages for temperature, humidity and HI
     
            double tempSum = 0.0;
            int humidSum = 0;
            double hISum = 0.0;
     
            for(n = 0; n < temperature.length; n++)
            {
                    tempSum += temperature[n];
            }
            for(n = 0; n < humidity.length; n++)
            {
                    humidSum += humidity[n];
            }
            for(n = 0; n < hI.length; n++)
            {
                    hISum += hI[n];
            }
            double tempAvg = tempSum/temperature.length;
            int humAvg = humidSum/humidity.length;
            double hIAvg = hISum/hI.length;
           
                   
           
            //OUTPUT - print table. Use enhanced for loops to print the months, temp, humidity and HI
     
            System.out.printf("%35s %s\n","Heat Index:", location);
            System.out.println();
     
     
            System.out.printf("%40s","Months");
            System.out.println();
            for(String m : month)
            {
                System.out.printf("  %3.3s",m);
            }
     
     
            System.out.println(" Avg");
            System.out.println("******************************************************************************************");
     
     
            System.out.print("Temp (F)");
             for(n = 0; n < month.length; n++)
             {
                     System.out.printf("%9s",temperature[n]);
             }
           
                    //print average
             System.out.printf("%6.4s", tempAvg);
            System.out.println();
     
            System.out.print("Humidity (%)");
            for(n = 0; n < month.length; n++)
            {
                    System.out.printf("%3s",humidity[n]);
            }
           
            System.out.println();
     
            System.out.print("HI (F)");
           for(n = 0; n < hI.length; n++)
           {
               System.out.printf("%11.4s",hI[n]);
           }
            System.out.printf("%8.4s",hIAvg);
            System.out.println();
        } //end main()
    }//end HeatIndex
    The format comes out all wacky like this:
    Last edited by Argyros; 11-10-2014 at 12:18 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    12

    Default Re: Help with using printf

    Post your code here using code tags. Most people won't follow links.

  3. #3
    Argyros is offline Member
    Join Date
    Aug 2014
    Posts
    10
    Rep Power
    0

    Default Re: Help with using printf

    Quote Originally Posted by Junky View Post
    Post your code here using code tags. Most people won't follow links.
    Alright.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help with using printf

    The code has a lot of calls to printf(). Which one(s) are the problem?
    Can you post a few lines of the print out that shows the problem
    and add a few more lines that show what it should be.

    Wrap the print out in code tags to preserve its formatting.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Argyros is offline Member
    Join Date
    Aug 2014
    Posts
    10
    Rep Power
    0

    Default Re: Help with using printf

    System.out.printf(" %3.3s",m);
    System.out.printf("%9s",temperature[n]);
    System.out.printf("%3s",humidity[n]);
    System.out.printf("%11.4s",hI[n]);

    Due to the allowing only so many characters in the string it throws the format off if I try and align all of the months, and numbers up right.
    They will end up like how the picture above is.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help with using printf

    What is wrong with the output from those printf()s you just posted?

    Can you post a few lines of the print out that shows the problem
    and add a few more lines that show what it should be.

    Wrap the print out in code tags to preserve its formatting.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Argyros is offline Member
    Join Date
    Aug 2014
    Posts
    10
    Rep Power
    0

    Default Re: Help with using printf

    Quote Originally Posted by Norm View Post
    What is wrong with the output from those printf()s you just posted?

    Can you post a few lines of the print out that shows the problem
    and add a few more lines that show what it should be.

    Wrap the print out in code tags to preserve its formatting.
    This is what its supposed to look like


    The issue is if I change the format to increase the numbers or text will get spaced out between each number.

    It ends up like this:
    Last edited by Argyros; 11-10-2014 at 11:16 PM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help with using printf

    Take a piece of paper and map out the width of each column.
    Then look at the print statements and make sure each print format String used in the printf() statement corresponds with the width of the column that has been mapped on paper.

    What columns are going past the allocated width for that column?

    Note: The images are hard to read. Copied and pasted data might be better.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Argyros is offline Member
    Join Date
    Aug 2014
    Posts
    10
    Rep Power
    0

    Default Re: Help with using printf

    Quote Originally Posted by Norm View Post
    Take a piece of paper and map out the width of each column.
    Then look at the print statements and make sure each print format String used in the printf() statement corresponds with the width of the column that has been mapped on paper.

    What columns are going past the allocated width for that column?

    Note: The images are hard to read. Copied and pasted data might be better.
    I know that. I use a formatting grid, but when using .3 with the formatting it throws most of the alignment off.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help with using printf

    Use a piece of paper to map the width of each column that you want to print.
    Then change the format Strings so each printf()s output matches the width of the column the data is supposed to have.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Argyros is offline Member
    Join Date
    Aug 2014
    Posts
    10
    Rep Power
    0

    Default Re: Help with using printf

    Quote Originally Posted by Norm View Post
    Use a piece of paper to map the width of each column that you want to print.
    Then change the format Strings so each printf()s output matches the width of the column the data is supposed to have.
    I have already done that, but the issue becomes when using the format with decimals as it throws most of the padding off between each number.
    when using
    Java Code:
     System.out.printf("%35s %s\n","Heat Index:", location);
            System.out.println();
    
    
            System.out.printf("%40s","Months");
            System.out.println();
            for(String m : month)
            {
                System.out.printf("%16.3s", m);
            }
    
    
            System.out.printf("%16s","Avg");
            System.out.println();
            System.out.println("******************************************************************************************");
    
    
            System.out.print("Temp (F)");
             for(n = 0; n < month.length; n++)
             {
            	 System.out.printf(" %18.1f",(double)temperature[n]);
             }
            
    		//print average
            //System.out.printf("%f", (double)tempAvg);
            System.out.println();
    
            System.out.print("Humidity (%)");
            for(n = 0; n < month.length; n++)
            {
            	System.out.printf("%15.0f",(double)humidity[n]);
            }
            //System.out.printf("%f",(double)humAvg);
            System.out.println();
    
            System.out.print("HI (F)");
           for(n = 0; n < hI.length; n++)
           {
        	   System.out.printf("%20.1f",(double)hI[n]);
           }
           	//System.out.printf("%f",(double)hIAvg);
            System.out.println();
    Makes it
    Heat Index: Key West, Florida

    Months
    Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Avg
    ************************************************** ****************************************
    Temp (F) 70.3 70.8 73.8 77.0 80.7 83.4 84.5 84.4 83.4 80.2 76.3 72.0
    Humidity (%) 69 67 66 64 66 69 67 67 70 69 69 70
    HI (F) 70.3 70.8 73.8 77.0 83.5 88.9 90.5 90.3 89.2 83.1 76.3 72.0
    Last edited by Argyros; 11-16-2014 at 07:48 PM.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help with using printf

    Are you getting what you want now? If not please show the desired widths of each column on a line.

    Hint: Wrap the output in code tags to preserve its formatting.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Help with using printf

    Just a silly follow-up question: where is that "wrong" output taken from? The command prompt / shell? Or the console of an IDE?

    I ask because if the IDE is configured to have a true type font rather than a fixed-width font in the console, then the output is going to be miss-aligned no matter what you do. You might be spending ages trying to fix the code when there is in fact nothing wrong with it but its rather just a problem in the displaying of the data.

    Probably not, but its worth to check.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Printf
    By Kev in forum New To Java
    Replies: 5
    Last Post: 04-30-2014, 12:40 PM
  2. Using printf to pad spaces
    By hamster in forum New To Java
    Replies: 2
    Last Post: 05-06-2012, 05:01 PM
  3. printf VS printWriter.printf
    By Mapisto in forum New To Java
    Replies: 14
    Last Post: 01-20-2012, 12:29 PM
  4. printf
    By razzle in forum New To Java
    Replies: 4
    Last Post: 11-12-2010, 02:13 PM
  5. printf
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 04:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •