0

Snippet of my code:

...
    System.out.println("\t" + "MONTH |" + "\t" + "HIGH | " + "\t" + "LOW |" + "\t" + "AVERAGE |" + "\t" + "RANGE");
    System.out.println("\t" + "______________________________________________");

    main.averageMonthOne(hightemp, lowtemp);

    in1.close();
    out.close();                
}//end of main 


private static double averageMonthOne (int hightemp, int lowtemp)
{
    double avgM = (hightemp + lowtemp)/2;
    System.out.println(avgM);
    return avgM;
} 

I want to be able to use the average I received from averageMonthOne and place it respectively under a the word "AVERAGE" in the println. Is that possible?

Expected Output:

MONTH | HIGH | LOW | AVERAGE | RANGE
_____________________________________
                      30.0
GameDroids
  • 5,584
  • 6
  • 40
  • 59
Wisdom Ins
  • 11
  • 4
  • Look at this question http://stackoverflow.com/questions/1358512/java-println-formatting-so-i-can-display-a-table – prashant Apr 12 '13 at 11:45
  • Ya bro, why not, just put your callable method in SOP at the position you want to display – commit Apr 12 '13 at 11:46
  • say I am getting other information too, like RANGE for instance... would I have to just do that all under the same `averageMonthOne` method? – Wisdom Ins Apr 12 '13 at 11:47

4 Answers4

0

Use System.out.printf() and format accordingly !

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
0

Very possible Just change your code to be like the following:

...
System.out.println("\t" + "MONTH |" + "\t" + "HIGH | " + "\t" + "LOW |" + "\t" + "AVERAGE |" + "\t" + "RANGE");
System.out.println("\t" + "______________________________________________");

double myresult = main.averageMonthOne(hightemp, lowtemp);
System.out.println("\t\t\t\t" + myresult);

in1.close();
out.close();                

}//end of main

private static double averageMonthOne (int hightemp, int lowtemp) { double avgM = (hightemp + lowtemp)/2; System.out.println(avgM); return avgM; }

javadev
  • 1,639
  • 2
  • 17
  • 35
0

If I where you I would create an Array of Strings where you put the numbers you get. For example:

try{
    BufferedReader bufferedReader = new BufferedReader(new FileReader("fileName.txt"));
    String[] numbers = new String[5];

    for(int i=0; i<5; i++){     // here you only read 5 values from a file, line by line. If the file is formatted differently you have to read them differently.
       numbers[i] = bufferedReader.readLine();
    }
}catch(IOException io){ ... }

Then you format the array of strings into one single String line like this:

String printedNumbers = "";
for(int i=0; i<numbers; i++){
    printNumbers+="\t";            // every table cell is seperated by a TAB
    if(numbers[i] == null){        // if the cell is empty
        printNumbers +=\t;         //    fill it with a TAB
    }else{ 
        printNumbers +=numbers[i]; // otherwise put the number in
    }
}

This may be a little much for only 5 values, but you shouldn't have to fill the gaps between your "table cells" with whitspaces. In the end to print your table do:

System.out.println("\t" + "MONTH |" + "\t" + "HIGH | " + "\t" + "LOW |" + "\t" + "AVERAGE |" + "\t" + "RANGE");
System.out.println("\t" + "______________________________________________");
System.out.println(printNumbers);
GameDroids
  • 5,584
  • 6
  • 40
  • 59
  • Even better, then you can read them from the textFile and put them directly into the `numbers` array. In addition you could check the length of each string in`numbers` and if it is smaller than 4, fill it with whitespace. – GameDroids Apr 12 '13 at 12:03
  • I'm confused how to both read the file and put them into the array ;x – Wisdom Ins Apr 12 '13 at 12:57
  • I edited the answer to show you what I ment with reading the file and putting the values directly into an array. – GameDroids Apr 12 '13 at 13:06
0

This works, if you have a fixed number of Strings, that can be filled or empty. Have a bunch of variables ready like this:

String avg = "", high = "", low = "", ...;

Next, fill those you need with values:

avg = averageMonthOne(hightemp, lowtemp);
high = ...;
low = ...;

Lastly, call the String.format method to gather it all up:

String s = String.format("%s %s %s", avg, high, low);
System.out.println(s);

You could shorten the above to

System.out.format("%s %s %s", avg, high, low);

According to http://docs.oracle.com/javase/6/docs/api/java/util/Formatter.html, there are lots of options for formatting and spacing the output, just change the first argument of the format method accordingly.

Simon Hellinger
  • 1,309
  • 12
  • 19