Have first part of program written. It will use enum array for month and average rainfall amount.
It starts at January and runs through December.
Second part is to ask user at what month they want to start (example, April).
The printout would then start at April of year -1 (prior year) and run through March of current year.

Lots of time searching internet with no luck. I am looking for method to ask for start month, then read enum data to find that month and then start the program from that new point.
Hints, suggestions welcome.
Code snippets greatly appreciated.

Thanks.

import java.util.Scanner;
import java.util.Arrays;

public class Rainfall
{

   public enum Average
   {
     January (2.72), February (2.34), March (2.93), April (3.49), May (3.66), June (3.43),
     July (3.46), August (3.51), September (3.81), October (3.07), November (3.62), December (3.10);

     private double average;


     private Average(double average)
     {
         this.average = average;        
     }

     public double getAverage()
     {
        return this.average;
     }

    }

    public static void main(String[] args)
    {
      Average[] CurrentMonth = Average.values();
      Scanner keyboard = new Scanner(System.in);
      Scanner scannerObject = new Scanner(System.in);
      double [] actual = new double[13];
      double [] difference = new double[13];
      int i;

      //*******************************************************************************
      //Now start input for what month at which to start.
      //*******************************************************

      System.out.println("From which month do you want to start?");
      System.out.println("Please use January, February, March, April, May.......");

      String month = scannerObject.next();
      System.out.println("You entered : "+month);

      for ( i=0; i<CurrentMonth.length; i++)
      {
          System.out.println("Enter rainfall average for "+ CurrentMonth[i]);
          actual[i] = keyboard.nextDouble();
          //
          //book problem requires output to show "...how much above or below average".
          //this will lead into negative numbers if below average. this is why "Math.abs" is commented out.
          //difference[i] = Math.abs(actual[i]-CurrentMonth[i].getAverage( ));
          //
          difference[i] = (actual[i]-CurrentMonth[i].getAverage( ));
      }



      System.out.println("Test for rainfall.");
      System.out.println("Monthly average:   Month  Average     Current    Difference");
      for ( i=0; i<CurrentMonth.length; i++)
      {
         System.out.println("Month average: " + CurrentMonth[i] + "   " + CurrentMonth[i].getAverage( ) + "    " + actual[i] +"   "+ difference[i]);
      }


   }
}

Look at the Enum.valueOf method - it gives you the enum value from a String. Eg you pass it the String "May" and it it returns the enum value Average.May

A little messy, but;
1) Added loop to repeat program until user quits;
2) Modified program to start at user defined month. Had to ask for number of the month (ex. June = 6). Used this number as ordinal number in enum field.
3) Still having issues on formatting output. Program runs, but printing the output is messy. Looking for help on this as I think my formatting code is not compatable with the code of the arrays.
Thanks

    /**
     * This program, "Rainfall", is to calculate the difference from average, the current monthly rainfall.
     * The user is asked a start month. This numeric value for the month is used to search the enumeration field for the start month.
     * Once the start month is established, the program asks for the start month current rainfall and the next 11 months measurements.
     * 
     * 
     */

    import java.util.Scanner;
    import java.util.Arrays;
    import java.text.DecimalFormat;
    import java.util.Formatter;

    public class Rainfall
    {
       static Scanner sc = new Scanner(System.in);

       public enum Average
       {
         January (2.72), February (2.34), March (2.93), April (3.49), May (3.66), June (3.43),
         July (3.46), August (3.51), September (3.81), October (3.07), November (3.62), December (3.10),;

         private double average;

         private Average(double average)
         {
             this.average = average;        
         }

         public double getAverage()
         {
            return this.average;
         }

       }

       public static void main (String [] args)
       {
         System.out.println("Welcome to the Rainfall Program");
          do
          {
            askForMonth();
          }
          while (askForAnotherYear());
           System.out.println("Good bye!");
       }

       public static void askForMonth ()
       {
          Average[] CurrentMonth = Average.values();
          Scanner keyboard = new Scanner(System.in);
          Scanner scannerObject = new Scanner(System.in);
          double [] actual = new double[13];
          double [] difference = new double[13];
          int i;

          //*******************************************************************************
          //Now start input for what month at which to start.
          //*******************************************************

          System.out.println("From which month do you want to start?");
          System.out.println("Please use January = 1, February=2, March=3, April=4, May=5.......");

          int selection = keyboard.nextInt();
          selection = selection - 1;
          Average selectedAverage = Average.values()[selection];
          System.out.println("You entered : "+selectedAverage);

          for ( i=selection ; i<CurrentMonth.length; i++)
          {
              System.out.println("Enter rainfall average for "+ CurrentMonth[i]);
              actual[i] = keyboard.nextDouble();
              difference[i] = (actual[i]-CurrentMonth[i].getAverage( ));
          }

          for ( i=0; i<selection; i++)
          {
              System.out.println("Enter rainfall average for "+ CurrentMonth[i]);
              actual[i] = keyboard.nextDouble();
              difference[i] = (actual[i]-CurrentMonth[i].getAverage( ));
          }

          System.out.println();
          System.out.println();
          System.out.println();
          System.out.println();

          System.out.println("Output for the Rainfall Program");
          System.out.println();
          //System.out.println("Month"+"\t"+"\t"+"\t"+"Month Average"+"\t"+"\t"+"Actual for Current Month"+"\t"+"\t"+"Difference");
          System.out.printf("%s %30s %15s %15s", "Month", "Month Average", "Month Current", "Difference");
          System.out.println();
          for ( i=selection; i<CurrentMonth.length; i++)
          {
             System.out.println(CurrentMonth[i] +"\t"+"\t" +"\t"+ CurrentMonth[i].getAverage( ) +"\t" + actual[i] +"\t"+"\t"+ difference[i]);
             //System.out.printf("%-19s %10.2d %10.2d %10.2d %n", CurrentMonth[i],CurrentMonth[i].getAverage( ), actual[i],difference[i] );

          }

           for ( i=0; i<selection; i++)
          {
             System.out.println(CurrentMonth[i] + "\t"+"\t"+"\t" + CurrentMonth[i].getAverage( ) + "\t" + actual[i] +"\t"+"\t"+ difference[i]);
             //System.out.printf("%-19s %10.2d %10.2d %10.2d %n", CurrentMonth[i],CurrentMonth[i].getAverage( ), actual[i],difference[i] );
          }
              }

       public static boolean askForAnotherYear()
       {
          while (true)
          {
             String answer;
             System.out.println();
             System.out.println("Do you want to run through another year? (Y or N)");
             answer = sc.next();
               if (answer.equalsIgnoreCase("Y"))
                  return true;
               else if (answer.equalsIgnoreCase("N"))
                  return false;
          }
       }
    }

Rather than using println with tabs, you will get better results using printf (like line 91) so you can specify the width and alignment of each field. I see you tried that, but I don't know what problems you had - maybe it was because CurrentMonth[i] is an enum value but you have a string format - in which case you probably want the string CurrentMonth[i].name(). There is definitely NO problem using printf with arrays etc, so just keep trying, or come back here for help.

As mentioned before, you can use Enum.valueOf to allow the user to enter month names rather than numbers.

Got it on both accounts. For output;
System.out.printf("%-19s %10.2f %10.2f %10.2f %n", CurrentMonth[i],CurrentMonth[i].getAverage( ), actual[i],difference[i] );

As for entering month names rather than numbers, I kept numbers for the rainfall, but had another assignment where i called using names. Part of the program is below.

Many thanks for your help and ideas.

import java.util.Scanner; 


public class Planet
{ 
    public static void main(String args[])
    { 

        Scanner scannerObject = new Scanner(System.in);
        Scanner s = new Scanner(System.in); 
        double weight = 0.0; 

        System.out.println("What is your weight on Earth (in pounds)?"); 
        weight = s.nextDouble(); 

        System.out.println("On which solar body would you like your weight calculated?");

        System.out.println(" 1. Mercury\n 2.  Venus\n 3.  Earth\n 4.  Moon\n 5.  Mars\n 6.  Jupiter\n 7.  Saturn\n 8.  Uranus\n 9.  Neptune\n 10. Pluto\n"); 

        System.out.println("Please type (exactly) the name of the solar body."); 
        String planet = scannerObject.next();

        switch(planet)
        { 
            //case 1: 
            case "Mercury":
            weight = weight * 0.1550;
            planet = "Mercury"; 
            break; 
            //case 2: 
            case "Venus":
            weight = weight * 0.8975; 
            planet = "Venus";
            break; 
            //case 3: 
            case "Earth":
            weight = weight * 1.0000; 
            planet = "Earth";
            break; 
            //case 4: 
            case "Moon":
            weight = weight * 0.1660;
            planet = "the Moon"; 
            break;
            //case 5: 
            case "Mars":
            weight = weight * 0.3507; 
            planet = "Mars";
            break; 

For the benefit of anyone looking for enum-related help here...
ParPau's last code may meet the requirements of his exercise, but it doesn't use enums.
To get an enum value from a String entered by the user you can use Enum's valueOf method, eg

enum Planet {Mars, Venus;}

String input = ... get user input
Planet p = Enum.valueOf(Planet.class, input);

And for formating the output of the enum (rather than just returning a String containing the exact value like MARS in this code).
Mind this might under some conditions cause side effects, if there's something that relies on Foo.F being "F" exactly.

public enum Foo {
    F("f1"),
    G("f2"),
    H("f3");

    private final String f;
    private static Map<String, Foo> foos;

    private Foo(final String f) {
        this.f = f;
        Foo.foo(f, this);
    }

    private static void foo(String f, Foo foo) {
        if (foos == null) {
            foos = new HashMap<String, Foo>();
        }
        foos.put(f, foo);
    }

    public String toString() {
        return f;
    }

    public static Foo getFoo(final String f) {
        return foos.get(f);
    }
}

Also allows you to get a Foo from the user input using Foo.getFoo(input) using the new values rather than the internal name of the entries.

Edited 2 Years Ago by jwenting

This question has already been answered. Start a new discussion instead.