Is there any way to use a single switch statement more than once? In the program I'm writing currently, rainfall figures are read from a file, and I have two methods that are determining which month has the highest/lowest rainfall. I keep a count using these methods and return the count to the main, in which I have a switch written that assigns a month name based on the count it sees. I would like to use the single switch for both the highest and lowest, to get the month name. Is this possible? If my explanation isn't clear enough I will post code.

Thank you.

I'm not sure what you mean, post example of what you have and possibly what you are trying to achieve.

Alright, I was afraid my explanation wasn't quiet clear.

Here is my code

public static void main(String[] args) throws IOException {
        //Array to hold rainfall values
        double[] figures = new double[12];
        
        figures=getData(figures);
        
        for (int i=0;i<figures.length;i++)
            System.out.println(figures[i]);
            
        System.out.println(" ");
        
        double totalRainfall=getTotal(figures);
        double averageRainfall=getAverage(totalRainfall);
        
        System.out.println("Total rainfall is "+totalRainfall);
        System.out.println("The average rainfall is "+averageRainfall);
        
        int monthNum=getLowest(figures);
        String month=" ";
        switch (monthNum)
        {
            case 1: month="January";
            break;
            case 2: month="Febuary";
            break;
            case 3: month="March";
            break;
            case 4: month="April";
            break;      
            case 5: month="May";
            break;
            case 6: month="June";
            break;
            case 7: month="July";
            break;
            case 8: month="August";
            break;
            case 9: month="September";
            break;
            case 10: month="October";
            break;
            case 11: month="November";
            break;
            case 12: month="December";
            break;
        }
           System.out.println(month);

  
    }

    public static double[] getData(double[] figures) throws IOException {
        Scanner keyboard=new Scanner(System.in);
        System.out.println("What is the filename?");
        
        String filename=keyboard.nextLine();
        
        File myfile=new File(filename);
        Scanner inputfile=new Scanner(myfile);
        
        int i=0;
        while (inputfile.hasNext()){
            for (i=0;i<figures.length;i++)
            {
                figures[i]=inputfile.nextDouble();
            }
        }

        return figures;
        }
        
          public static double getTotal(double figures[])
        {
        double total=0;
        
        for (int i=0;i<figures.length;i++)
        {
            total=figures[i]+total;
        }
        return total;
    }
    
        public static double getAverage(double total)
        {
            double average;
        return average=total/12;
    }
    
        public static int getLowest(double figures[])
    {
        double lowest=figures[0];
        int i;
        int count=1;
        for (i=1;i<figures.length;i++)
            {
                if (figures[i]<lowest)
                {
                    lowest=figures[i];
                    count++;
                }
            }
            return count;

 public static int getHighest(double figures[])
    {
        double highest=figures[0];
        int i;
        int count=1;
        for (i=1;i<figures.length;i++)
            {
                if (figures[i]>highest)
                {
                    highest=figures[i];
                    count++;
                }
            }
            return count;
}

I have the switch in main, I would like to use that single switch for my getLowest and getHighest methods, they return a value which represents a month, eg:1 for January,2 for February,ect.

I want to display:

April has had the most rain.

July has had the least rain.

But by using the single switch to determine the month for each. Hope that's clearer.

Sounds like you want to put the switch in a method:

private String intToMonth(int i)
{
  String s;
  //case statement assigns s according to i
  return s;
}

Is that what you had in mind? You can use that anywhere in this class.

You might want to consier using an array of Strings

String month[] = {"", "January", ..., "December"}; //null string to offset January to 1st position

That's a lot easier to work with, and it's also more efficient.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.