I am trying to write a program where the user enters in the average and actual rainfall amounts for 12 months. Then the system will display the month, actual, average and the difference in a table layout -
Month Actual Average Difference
Jan 5 3 2
Feb 2 5 3
and then a new line for every month.
I have it set up to get the amounts and do the difference calculation but cannot figure out the display. There are two classes of code - the rainfall calc and the rainfall test which contains main. I am including both hoping that somone can help me get it to display in the above layout.
import java.util.Scanner;
public class RainfallCalc
{
private int [] [] rainfall;
private int startMonth;
public RainfallCalc(String monthNum)
{
if(monthNum == null)
{
System.err.println("Empty Month Number");
System.exit(1);
}
startMonth = Integer.parseInt(monthNum);
if (startMonth < 1 || startMonth >12)
{
System.err.println("Invalid month number");
System.exit(1);
}
rainfall = new int[12] [4];
//MAKE CALL POPULATE ARRAY WITH startMonth
this.popMonth(startMonth);
}
private void computeDiff()
{
for (int i = 0; i < rainfall.length; i++)
rainfall[3] = rainfall[2] - rainfall[1];
}
public void getInput()
{
Scanner keyboard = new Scanner(System.in);
for (int i = rainfall.length - 1; i >=0; i--)
{
System.out.println("Please enter the average rainfall " + "for " + numToMonth(rainfall[0]) + ": ");
int num = keyboard.nextInt();
if (num < 0)
{
System.out.println("Negative Rainfall, Sorry!");
System.exit(1);
}
rainfall[1] = num;
//Collect the Actual Rainfall Amount
System.out.println("Please enter the actual rainfall "+ "for " + numToMonth(rainfall[0]) + ": ");
num = keyboard.nextInt();
if (num < 0)
{
System.out.println("Negative Rainfall, Sorry!");
System.exit(1);
}
rainfall[2] = num;
}
this.computeDiff();
}
private String numToMonth(int num)
{
switch (num)
{
case 1:
return "January";
case 2:
return "February";
case 3:
return "March";
case 4:
return "April";
case 5:
return "May";
case 6:
return "June";
case 7:
return "July";
case 8:
return "August";
case 9:
return "September";
case 10:
return "October";
case 11:
return "November";
case 12:
return "December";
default:
return "";
}
}
private void popMonth(int start)
{
//int month = start;
for(int i = rainfall.length - 1; i >= 0; i--)
{
rainfall[0] = start;
start--;
if(start <1)
start = 12;
}
}
public String toString()
{
String result;
result = "Month \t \t Average \t\t Actual \t\t Difference \t\t\n";
return result;
}
}
Here is where main resides:
import java.util.Scanner;
public class RainTest
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
while (true)
{
System.out.println("Please enter the current month: \n"
+ "1 - 12 = Jan to Dec\n" + "Q to stop calculating");
String currMonth = keyboard.next();
if (currMonth.charAt(0) == 'Q' || currMonth.charAt(0) == 'q')
break;
RainfallCalc calc = new RainfallCalc(currMonth);
calc.getInput();
System.out.println(calc);
}
}
}
Month Actual Average Difference
Jan 5 3 2
Feb 2 5 3
and then a new line for every month.
I have it set up to get the amounts and do the difference calculation but cannot figure out the display. There are two classes of code - the rainfall calc and the rainfall test which contains main. I am including both hoping that somone can help me get it to display in the above layout.
import java.util.Scanner;
public class RainfallCalc
{
private int [] [] rainfall;
private int startMonth;
public RainfallCalc(String monthNum)
{
if(monthNum == null)
{
System.err.println("Empty Month Number");
System.exit(1);
}
startMonth = Integer.parseInt(monthNum);
if (startMonth < 1 || startMonth >12)
{
System.err.println("Invalid month number");
System.exit(1);
}
rainfall = new int[12] [4];
//MAKE CALL POPULATE ARRAY WITH startMonth
this.popMonth(startMonth);
}
private void computeDiff()
{
for (int i = 0; i < rainfall.length; i++)
rainfall[3] = rainfall[2] - rainfall[1];
}
public void getInput()
{
Scanner keyboard = new Scanner(System.in);
for (int i = rainfall.length - 1; i >=0; i--)
{
System.out.println("Please enter the average rainfall " + "for " + numToMonth(rainfall[0]) + ": ");
int num = keyboard.nextInt();
if (num < 0)
{
System.out.println("Negative Rainfall, Sorry!");
System.exit(1);
}
rainfall[1] = num;
//Collect the Actual Rainfall Amount
System.out.println("Please enter the actual rainfall "+ "for " + numToMonth(rainfall[0]) + ": ");
num = keyboard.nextInt();
if (num < 0)
{
System.out.println("Negative Rainfall, Sorry!");
System.exit(1);
}
rainfall[2] = num;
}
this.computeDiff();
}
private String numToMonth(int num)
{
switch (num)
{
case 1:
return "January";
case 2:
return "February";
case 3:
return "March";
case 4:
return "April";
case 5:
return "May";
case 6:
return "June";
case 7:
return "July";
case 8:
return "August";
case 9:
return "September";
case 10:
return "October";
case 11:
return "November";
case 12:
return "December";
default:
return "";
}
}
private void popMonth(int start)
{
//int month = start;
for(int i = rainfall.length - 1; i >= 0; i--)
{
rainfall[0] = start;
start--;
if(start <1)
start = 12;
}
}
public String toString()
{
String result;
result = "Month \t \t Average \t\t Actual \t\t Difference \t\t\n";
return result;
}
}
Here is where main resides:
import java.util.Scanner;
public class RainTest
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
while (true)
{
System.out.println("Please enter the current month: \n"
+ "1 - 12 = Jan to Dec\n" + "Q to stop calculating");
String currMonth = keyboard.next();
if (currMonth.charAt(0) == 'Q' || currMonth.charAt(0) == 'q')
break;
RainfallCalc calc = new RainfallCalc(currMonth);
calc.getInput();
System.out.println(calc);
}
}
}