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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Problem

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem

    Use the DateFormat class
    or the printf() method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem

    Did you look at the two options I suggested?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem

    I tried the printf one but it just made it worse

  6. #6
    Junior Member
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    The posted code does not compile. Please correct the errors or copy and paste the full text of the error messages here if you need help with them.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM