Results 1 to 17 of 17
  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default formatting data output

    How would I create a chart (such as a table) with the following output data (using printf)?:

    Month: December,
    High Temperature: 76
    Low Temperature: -10
    Average temperature for the month is: 33.0
    Range: 86.0.



    also, if i have a loop that gets all this data, what would be the best way to remove the commas after each month?

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    I don't know how you want to make it look-like but this is how you need to use printf:

    Java Code:
    String month = "December";
    int high = 76;
    int low = -10;
    double avg = 33.0;
    double range = 86.0;
    
    System.out.printf("%s: High: %d, Low: %d, Avg: %.1f Range: %.1f",month,high,low,avg,range);
    [quote="output"]
    December: High: 76, Low: -10, Avg: 33.0, Range: 86.0
    [/quote]

  3. #3
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    Sorry. i guess i should have been more clear. it needs to be in table format.

    My probelm is that my method gets all of the data, and prints it all at once. It is hard to format it, especially with the spacing.

    Java Code:
    int counter = 0;
    		
    		//
    		while (in.hasNext()) {
    					
    			counter++;
    			
    			data = getData(in, counter);
    			allData = allData + data;
    			
    		}
    
                    // We print out the data
    		System.out.println(allData + "\n");
    
                  public static String getData(Scanner in, int counter){
    		
    			String month = "";
    			int lowTemp;
    			int highTemp;
    			String data;
    			double average;
    			double range;
    						
    			// This will get the next piece of text (or month name)
    			month = in.next();
    			highTemp = in.nextInt();
    			lowTemp = in.nextInt();
    				
    			average = (highTemp + lowTemp)/2;
    			range = (highTemp-lowTemp);
    						
    			if (counter == 1) {
    			
    				System.out.println("Month     High    Low     Average     Range");
    				
    				counter++;
    				
    			}
    	
    			System.out.printf("\n%s %d, %d, %.1f, %.1f",month,highTemp,lowTemp,average,range);
    			
    			data="";
    			
    			return data;
    			
    		}
    Attached Thumbnails Attached Thumbnails formatting data output-java.jpg  
    Last edited by droidus; 04-09-2011 at 03:32 PM.

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    then create a GUI?

    download Netbeans IDE from oracle website

  5. #5
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    this is a homework assignment, and there has to be a way to format this with some type of spacing.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Check out the printf tutorials and examples in this forum. Those format specifiers can be made to display their Strings with a pre-defined width if you give them number constants as described in the java.util.Formatter API: Formatter API

  7. #7
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    so i have something like this:

    System.out.printf("\n%s %5d, %5d, %.1f, %.1f",month,highTemp,lowTemp,average,range);

    but the output is:

    January, 72, -12, 30.0, 84.0
    February, 75, -5, 35.0, 80.0
    March, 80, 8, 44.0, 72.0

    i wouldn't have to change the spacing after each month, would i?

  8. #8
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    You don't need to use "printf()" at all for spacing. Have you heard of the tab character: "\t"?

    Java Code:
    System.out.println("Month\tHigh\tLow\tAverage\tRange");
    System.out.println(month+"\t"+high+"\t"+low+"\t"+avg+"\t"+range);

  9. #9
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    no, i haven't. thanks sooo much though, for this!!!!

    one problem though...

    see the attached image. some of the bigger months have their numbers pushed farther out.
    Attached Thumbnails Attached Thumbnails formatting data output-java.png  

  10. #10
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Hmmmm.....

  11. #11
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    Quote Originally Posted by ra4king View Post
    Hmmmm.....
    i tried putting in another tab after the month, but still did not do anything for me

  12. #12
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    The only left to do is to put 2 tabs for all months except for February, September, November, and December, who all get 1 tab.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ra4king View Post
    You don't need to use "printf()" at all for spacing. Have you heard of the tab character: "\t"?

    Java Code:
    System.out.println("Month\tHigh\tLow\tAverage\tRange");
    System.out.println(month+"\t"+high+"\t"+low+"\t"+avg+"\t"+range);
    but take care if you do this if your text between tabs is longer than the standard tab length. For example, check out this code sample:

    Java Code:
    public class TabVsPrintf {
       public static void main(String[] args) {
          String[][] data = {
                   {"January", "February", "March", "April", "May"},
                   {"1", "2", "3", "4", "5"},
                   {"6", "7", "8", "9", "10"},
                   {"11", "12", "13", "14", "15"}
          };
          
          System.out.println("using tabs:");
          for (int i = 0; i < data.length; i++) {
             for (int j = 0; j < data[i].length; j++) {
                System.out.print(data[i][j] + "\t");
             }
             System.out.println();
          }
          
          int width = 12;
          System.out.println("\nusing printf:");
          for (int i = 0; i < data.length; i++) {
             for (int j = 0; j < data[i].length; j++) {
                System.out.printf("%-" + width + "s", data[i][j]);
             }
             System.out.println();
          }
          
       }
    }

    The output is:
    Java Code:
    using tabs:
    January	February	March	April	May	
    1	2	3	4	5	
    6	7	8	9	10	
    11	12	13	14	15	
    
    using printf:
    January     February    March       April       May         
    1           2           3           4           5           
    6           7           8           9           10          
    11          12          13          14          15

  14. #14
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Whoa, I didn't know about the %-#s format. That works too :D

  15. #15
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    if i have the following code, how can i return it so that I can easily format it for month, high and low temp.?

    Java Code:
    // This will get the next piece of text (or month name)
    				month = in.next();
    				num1 = in.nextInt();
    				num2 = in.nextInt();
    						
    				// This removes the comma in the month
    				for (i=0; i < (month.length()-1); i++) {
    					curChar = month.charAt(i);
    					chars = chars + curChar;
    				}
    			
    				month = chars;
    		
    				// Converts the temperatures to strings
    				intNum1 = Integer.toString(num1);
    				intNum2 = Integer.toString(num2);
    				
    				highLows = intNum1 + " " + intNum2;
    				
    			
    			
    			data = month + " " + highLows;
    			
    			return data;
    			
    			}
    Last edited by droidus; 04-12-2011 at 05:40 PM.

  16. #16
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    right now, i just get it like:

    January 72 -12
    February 75 -5
    March 80 8
    April 98 15
    May 98 29
    June 100 28
    July 103 48
    August 106 40
    September 102 35
    October 90 26
    November 85 14
    December 76 -10

    it doesn't help me. i was thinking about getting each letter, for the month, and then the numbers for the high and low, but there has to be an easier way...

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    You have been told how to do it, use the printf method. If you bother to read the code provided above it explains how you can provide a width for each cell. If you use 20 and try to print "dog" it will be followed by 17 spaces. Print "caterpillar" and it will be followed by 9 spaces. The printf does all the formatting for you and you would know this had you bothered to read about the method and or a tutorial. In short do some research!

Similar Threads

  1. Help with formatting output
    By John Lord in forum New To Java
    Replies: 1
    Last Post: 10-31-2010, 01:10 PM
  2. formatting numbers in output
    By andy3 in forum New To Java
    Replies: 3
    Last Post: 05-27-2010, 07:43 PM
  3. Formatting output somewhat like a table
    By latereg in forum New To Java
    Replies: 3
    Last Post: 04-06-2010, 07:44 AM
  4. Need help formatting File Output
    By aaroncarpet in forum New To Java
    Replies: 2
    Last Post: 11-26-2009, 06:26 PM
  5. Need help formatting output and some code
    By A5i19 in forum New To Java
    Replies: 6
    Last Post: 11-09-2009, 05:58 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
  •