# Java: Coding Issues

## Multiple variables in one declaration

    		int 	totalRainfall = 0,
    			dailyRainfall = 0,
    			maxDailyRainfall = 0,
    			currentDay = 0,
    			rainlessDays = 0,
    			maxRainlessDays = 0;

Declaring many variables in one declaration is generally not the best style. Some code reformaters will try to fill out the line with all the declarations. Documenting each variable is more difficult.

## Integer size

    	public static final short NUMBER_OF_DAYS = 7;

Using short was nice, altho for single numbers the savings in memory is inconsequential. Shorts are expanded to do any computation. Why didn't you use "byte" type?

## All fields of the for loop

    		int 	totalRainfall = 0,
    			dailyRainfall = 0,
    			maxDailyRainfall = 0,
    			currentDay = 0,
    			rainlessDays = 0,
    			maxRainlessDays = 0;

    		for( ; currentDay < NUMBER_OF_DAYS ; currentDay++ )

Leaving out the for loop initialization saves an infinitesimal amount of CPU time, but causes big problems for the reader, who has to read the program backwards looking for the assignment to that variable. Easy in this case, but it creates a big interruption in reading speed.

## For when counting

            day = 1;

            while (day <= 7)
            {
                totalRainfall += rainfall;
                System.out.println("Rainfall for day " +day + " is " +rainfall + " millimeters.");
                day++;
                rainfall = SavitchIn.readLineInt();
                System.out.println("Total rainfall so far is " +totalRainfall);
                if (rainfall > maxRainfall)
                    maxRainfall = rainfall;
            }

## Else clause not needed

        if (maxRain <= rain)
            maxRain = rain;
        else
            maxRain = maxRain;
        }

## Reusing variables

    int averageRainfall = 0;
    static final int DAYS = 7;

    for (int day=0; day < DAYS; day++) {
        String rainStr = JOptionPane.showInputDialog(null, "Enter rainfall");
        int rain = Integer.parseInt(rainStr);

        averageRainfall = averageRainfall + rain;
    }

    averageRainfall = averageRainfall / DAYS;
    . . .

## Weird braces

    			{
    			if (rain == 0)
    				clearDay++;
    			else
    				clearDay = 0;//This resets clearDay when there is rain
    				{
    				if (clearDay > inARow)// This keeps track of rainless days in a row
    					inARow = clearDay;
    				}
    			}

## Psychological indentation

    for (count = 1; count <= 7; count++ ){
        temp = JOptionPane.showInputDialog(null,"Please enter ...") ;
        rain = Integer.parseInt(temp);

            if (rain > maxRain){
                maxRain = rain;
            }

            if (rain<0){
                JOptionPane.showMessageDialog(null, "You can't ...");
                System.exit(0);
            }
            allRain = allRain + rain;
            rainTotal = allRain;

                if ((lastNumber==0) && (rain==0)){
                    zeroDays++;}
                lastNumber = rain;
                    }

Should be

    for (count = 1; count <= 7; count++ ){
        temp = JOptionPane.showInputDialog(null,"Please enter ...") ;
        rain = Integer.parseInt(temp);

        if (rain > maxRain){
            maxRain = rain;
        }

        if (rain<0){
            JOptionPane.showMessageDialog(null, "You can't ...");
            System.exit(0);
        }
        allRain = allRain + rain;
        rainTotal = allRain;

        if ((lastNumber==0) && (rain==0)){
            zeroDays++;}
        lastNumber = rain;
    }