1 Attachment(s)
Help with formatting arrays
I am almost done with this program but I cannot find a way to print the the arrays in an organized table. I tried using a for statement for each array and the program compiles but no input screen appears to accept used input. Any help would be appreciated. I am trying to make it look like this image:
Attachment 5692
Here is what I have so far:
Code:
import java.util.Scanner;
class AnnualClimate1
{
public static void main (String [ ] args)
{
// define city and state
Scanner in = new Scanner(System.in);
String city = " Tallahassee ";
String state = " Florida ";
//input data into string
String month [] ={"Jan", "Feb", "Mar.", "Apr.", "May.", "Jun.", "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec." };
double temperature [] ={51.8, 54.8, 61.1, 66.4, 74.4, 80.4, 82.4, 82.1, 78.9, 69.1, 60.4, 53.7,}; //initialize with Fahrenheit values
double precipitation [] ={5.4, 4.6, 6.5, 3.6, 5.0, 6.9, 8.0, 7.0, 5.0, 3.3, 3.9, 4.1}; //initialize with inches values
double averageTemp = 0.0, totalPrecip = 0.0;
double sumTemp = 0;
String tempLabel = "F"; //label
String precipLabel = "cm"; //label
//input
System.out.print("Choose the temperature scale (F = Fahrenheit, C = Celsius): ");
String tempChoice = in.next();
//temp conversions
if(tempChoice.equalsIgnoreCase("F"))
{
for( int n = 0; n < temperature.length; n++)
{
tempLabel = "(F)";
sumTemp += temperature[n];
averageTemp = sumTemp/12;
//System.out.println(temperature[n]);
}
}
if(tempChoice.equalsIgnoreCase("C"))
{
for( int n = 0; n < temperature.length; n++)
{
tempLabel="(C)";
sumTemp += temperature[n];
averageTemp = ((sumTemp/12)-32)*5/9;
//System.out.println(temperature[n]);
}
}
System.out.print("Choose the precipitation scale (i = inches, c = centimeteres): ");
String precipChoice = in.next();
//precipitation conversion
if(precipChoice.equalsIgnoreCase("c")){
for( int n = 0; n < precipitation.length; n++)
{
precipLabel="(cm)";
totalPrecip += precipitation[n] * 2.54;
//System.out.println(precipitation[n]);
}
}
if(precipChoice.equalsIgnoreCase("i")){
for( int n = 0; n < precipitation.length; n++)
{
precipLabel ="(in)";
totalPrecip += precipitation[n];
//System.out.println(precipitation[n]);
}
}
//output
System.out.printf("%30s\n", "Climate Data");
System.out.printf("%19s\n", "Location: " + city +", " + state);
System.out.printf("%18s", "Temperature",tempLabel,"Precipitation",precipLabel);
System.out.println("***************************************************");
for (int n = 0; n<=11; n++)
{
System.out.printf(month[n], temperature[n], precipitation[n]);
}
System.out.println("***************************************************");
System.out.println(averageTemp);
System.out.println(totalPrecip);
}
}//end main
//end Annual Climate
Re: Help with formatting arrays
Quote:
no input screen appears to accept used input
The Scanner class's methods read its input from the keyboard on a command prompt screen. There isn't a special screen.
If you want to present a dialog box for the user to enter data in, look at the JOptionPane class.
What does the program's output look like? When pasting the output be sure to wrap it in code tags to preserve its formatting.
Please edit your post and wrap your code with code tags:
[code]
YOUR CODE HERE
[/code]
to get highlighting and preserve formatting.
Re: Help with formatting arrays
I made some progress my adding in a third for statement which now prints the info from each array. However, I am still having trouble setting up the formatting of the table
Re: Help with formatting arrays
If you want any help, please post the output and point out the problems with it.
2 Attachment(s)
Re: Help with formatting arrays
Attachment 5693Attachment 5694
The program prints the months array one after another but not the precipitation and temperature arrays.
Re: Help with formatting arrays
I would like to copy some of the program's output so I could paste it here, but that is not possible to do with an image.
Check how you have coded the args for the printf() statements. There are rules for how to code them that you need to read.
Go read the API doc for the method in the PrintStream class