Reputation: 351
I've looked on here to find something similar and nothing has seemed to help. I'm trying to return my value inputed into my boolean method to my tester program. Any advice would be greatly excepted. Since my code is so long, I'll only include what I think is relevant.
public class SeasonCalculatorTester
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the month: ");
int month = in.nextInt();
System.out.print("Enter the day: ");
int day = in.nextInt();
SeasonCalculator calculator = new SeasonCalculator(month, day);
//TODO USE THE calculator (SeasonCalculator) TO CHECK FOR VALID MONTH AND VALID DAY.
// IF MONTH OR DAY IS NOT VALID PRINT ERROR MESSAGE.
// IF VALID PRINT OUT THE MONTH (IN STRING FORM, NOT INT FORM), THE DAY,
// AND THE SEASON THAT THE DAY IS IN
if(calculator.isValidMonth())
{
System.out.print(calculator.getMonth());
}
if(calculator.isValidDay())
{
System.out.print(" "+calculator.isValidDay()+" is in the "+ calculator.getSeason());
}
else
{
System.out.println("False");
}
}
}
The method I need the int value back from is "isValidDay()", here is that code:
public boolean isValidDay()
{
if ((month == 1 && day >= 1) && (month == 1 && day <= 31))
{
return true;
}
if ((month == 2 && day >= 1) && (month == 2 && day <= 29))
{
return true;
}
if ((month == 3 && day >= 1) && (month == 3 && day <= 31))
{
return true;
}
if ((month == 4 && day >= 1) && (month == 4 && day <= 30))
{
return true;
}
if ((month == 5 && day >= 1) && (month == 5 && day <= 31))
{
return true;
}
if ((month == 6 && day >= 1) && (month == 6 && day <= 30))
{
return true;
}
if ((month == 7 && day >= 1) && (month == 7 && day <= 31))
{
return true;
}
if ((month == 8 && day >= 1) && (month == 8 && day <= 31))
{
return true;
}
if ((month == 9 && day >= 1) && (month == 9 && day <= 30))
{
return true;
}
if ((month == 10 && day >= 1) && (month == 10 && day <= 31))
{
return true;
}
if ((month == 11 && day >= 1) && (month == 11 && day <= 31))
{
return true;
}
if ((month == 12 && day >= 1) && (month == 12 && day <= 31))
{
return true;
}
else
{
return false;
}
}
Upvotes: 0
Views: 1099
Reputation: 7083
You can also try to return an Integer object. If it is null, then it is not ok.
public static Integer returnInt() {
Integer ret = calculate_something();
if (ret > AN_OK_CONDITION) {
return ret;
}
else {
return null;
}
}
Then, call it:
Integer ret = returnInt();
if (ret != null) {
System.out.println("Number: " + ret);
}
Upvotes: 0
Reputation: 359966
Add a different method on to SeasonCalculator
which just returns day
. Then:
if(calculator.isValidDay())
{
System.out.print(" "+calculator.getDay()+" is in the "+ calculator.getSeason());
}
Upvotes: 0
Reputation: 3254
You can't return a int
or something from a method returning boolean. You can add a global variable which you can set from the method when the date is valid.
Upvotes: 0
Reputation: 328737
What you could do is:
class SeasonCalculator {
private static final int[] days = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
private final int month;
private final int day;
public SeasonCalculator(int month, int day) {
if (month < 1 || month > 12) {
throw new IllegalArgumentException("Not a valid month: " + month);
}
if (day < 1 || day > days[month - 1]) {
throw new IllegalArgumentException("Not a valid day: " + day + " for month " + month);
}
this.month = month;
this.day = day;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
}
Upvotes: 2