Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## 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  Reply With Quote

3. ## Re: Problem

Use the DateFormat class
or the printf() method.  Reply With Quote

4. ## 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  Reply With Quote

5. ## Re: Problem

Did you look at the two options I suggested?  Reply With Quote

6. ## Re: Problem

I tried the printf one but it just made it worse  Reply With Quote

7. ## 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.  Reply With Quote

8. ## 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; System.out.println("Sunshine for 2009");enterValues(year1);
double[]year2=new double; System.out.println("Sunshine for 2010");enterValues(year2);
double[]year3=new double; 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 ; 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?  Reply With Quote

9. ## Re: Problem  Reply With Quote