# Problem

• December 13th, 2012, 10:17 AM
marty_
Problem
So yea i got my assingment working so i need help on this part!

The average sunshine for month is 1 from 2009 to 2011 is 39.0
The average sunshine for month is 2 from 2009 to 2011 is 38.333333333333336
The average sunshine for month is 3 from 2009 to 2011 is 39.0
The average sunshine for month is 4 from 2009 to 2011 is 21.666666666666668
The average sunshine for month is 5 from 2009 to 2011 is 38.0
The average sunshine for month is 6 from 2009 to 2011 is 38.666666666666664
The average sunshine for month is 7 from 2009 to 2011 is 39.0
The average sunshine for month is 8 from 2009 to 2011 is 42.0
The average sunshine for month is 9 from 2009 to 2011 is 21.333333333333332
The average sunshine for month is 10 from 2009 to 2011 is 20.333333333333332
The average sunshine for month is 11 from 2009 to 2011 is 5.0
The average sunshine for month is 12 from 2009 to 2011 is 5.666666666666667

But how do i get all the numbers to be like 39.0, 39.0, 38.0, 39.0, 42.0, 5.0? help please
• December 13th, 2012, 10:28 AM
Norm
Re: Problem
Use the DateFormat class
or the printf() method.
• December 13th, 2012, 11:02 AM
marty_
Re: Problem
{
monthlyaverage = 0;

totalaverage = (year1 [count] + year2 [count] + year3 [count]);
monthlyaverage = (totalaverage / 3);

System.out.println ("The average sunshine for month is " + (count+1) + " from 2009 to 2011 is " + (monthlyaverage));

}
}

}

Thats the code i have for it
• December 13th, 2012, 11:03 AM
Norm
Re: Problem
Did you look at the two options I suggested?
• December 13th, 2012, 11:05 AM
marty_
Re: Problem
I tried the printf one but it just made it worse
• December 13th, 2012, 11:09 AM
JMeacham
Re: Problem
You could try to use a Decimal Formatter. Just add the import, and then write the line

DecimalFormat fmt = new DecimalFormat("##.0");

Then use your new formatter on the number you're printing. It will preserve only the first digit after the decimal point

If you want the number after the decimal point to always be a 0, then You could format it to exclude the decimal and then just concatenate a ".0" onto the string.
• December 13th, 2012, 11:14 AM
marty_
Re: Problem
import java .util. Scanner;

import java.util.Scanner;
//project name
class Assignment2 {

public static void main (String [] arguments)
{

System.out.println("**************************");
System.out.println("****Weather forecast****");
System.out.println("***Intro to Programing***");
System.out.println(" ***Assignment 2***");
System.out.println(" ***Sunshine***");
System.out.println("***By Martin Davidson ***");
System.out.println("**************************\n") ;

double[]year1=new double[13]; System.out.println("Sunshine for 2009");enterValues(year1);
double[]year2=new double[13]; System.out.println("Sunshine for 2010");enterValues(year2);
double[]year3=new double[13]; System.out.println("Sunshine for 2011");enterValues(year3);

System.out.println("Sunshine for 2009"); calculateandDisplayResults(year1); System.out.println();
System.out.println("Sunshine for 2010"); calculateandDisplayResults(year2); System.out.println();
System.out.println("Sunshine for 2011"); calculateandDisplayResults(year3); System.out.println();

String [] months = new String [13]; System.out.println (" Monthly Average Sunshine from 2009-11 ");monthlyresults(year1, year2, year3);

}

public static void enterValues(double[]value)
{
for (int count=0; count<12; count++)
{

Scanner keyboard=new Scanner(System.in);
System.out.println("sunshine for month "+ (count+1));
value[count]=keyboard.nextDouble();
}
}

public static void calculateandDisplayResults(double [] value)
{
double average = calculateAverage (value);

System.out.printf ("The average sunshine is %.1f\n " , average);

int highest = calculateHighest (value);
System.out.println ("The month with highest sunshine is month " + (highest +1)
+ " with value " + (value [highest]));

int lowest = calculateLowest (value);
System.out.println ("The month With has lowest sunshine is month " + (lowest +1)
+ " with value " + (value [lowest]));

}

public static double calculateAverage (double [] value)
{
double total = 0;
//double average = 0;

int count;
for (count = 0; count < 12; count++)
{
total = total + value [count];
}
return (total / 12);
}

public static int calculateHighest (double [] value)
{
int positionhighest = 0;

double highest = value [positionhighest];

int count;
for (count = 1; count <12; count++)
{
if (value [count] > highest)
{
highest = value [count];
positionhighest = count;
}
}
return (positionhighest);
}

public static int calculateLowest (double [] value)
{
int positionlowest = 0;

double lowest = value [positionlowest];

int count;
for (count = 1; count <12; count++)
{
if (value [count] < lowest)
{
lowest = value [count];
positionlowest = count;
}
}
return (positionlowest);
}

public static void monthlyresults (double [] year1, double [] year2, double [] year3)
{
double monthlyaverage = 0;
double totalaverage;

int count;
for (count = 0; count < 12; count ++)

{
monthlyaverage = 0;

totalaverage = (year1 [count] + year2 [count] + year3 [count]);
monthlyaverage = (totalaverage / 3);

System.out.println9("The average sunshine for month is " + (count+1) + " from 2009 to 2011 is " + (monthlyaverage));

}
}

}

That's my whole code there any ideas?
• December 13th, 2012, 11:32 AM
Norm
Re: Problem