Reputation: 63
I am writing a simple program. Seems easy enough. The program is to ask the user what type of dwelling they live in, and how many hours they spend at home. Then we take the values the user entered and suggest a type of pet for them based on they're inputs.
Now the question. Why is it that when I call the methods they are called twice. The first two methods are called twice and not the third. I suspect it is because the third method declared the a and b variable as the methods themselves but I cannot figure out why this is so, and not sure how to fix this bug.
import javax.swing.JOptionPane;
public class PetAdvice {
public static int dwelling(){
int a = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter 1 (Apartment), 2 (House), 3 (Dorm)","Dwelling", JOptionPane.QUESTION_MESSAGE));
if(!(a == 1 || a == 2 || a == 3)){
JOptionPane.showMessageDialog(null, "The value for dwelling must be 1 (Apartment), 2 (House), 3 (Dorm)","Dwelling type error", JOptionPane.ERROR_MESSAGE);
System.exit(-1);
}
return a;
}
public static int hours(){
int b = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter the number of hours a week you are home.", JOptionPane.QUESTION_MESSAGE));
if (b <= 0 || b >= 168){
JOptionPane.showMessageDialog(null, "The number of hour per week you are home must be between 0 and 168 inclusivly.","Hours at home error.", JOptionPane.ERROR_MESSAGE);
System.exit(-1);
}
return b;
}
public static void pet(int a, int b){
a = dwelling();//Pretty sure these variables are declared wrong
b = hours();
if(a == 1 && b >= 10){
JOptionPane.showMessageDialog(null, "You should get a cat!" ,"Reccomened Pet.", JOptionPane.INFORMATION_MESSAGE);
}
else
if(a == 1 && b <= 10){
JOptionPane.showMessageDialog(null, "You should get a Hamster!" ,"Reccomened Pet.", JOptionPane.INFORMATION_MESSAGE);
}
else
if(a == 2 && b > 18){
JOptionPane.showMessageDialog(null, "You should get a Pot-bellied Pig!" ,"Reccomened Pet.", JOptionPane.INFORMATION_MESSAGE);
}
else
if(a == 2 && b > 10 || b < 17){
JOptionPane.showMessageDialog(null, "You should get a dog!" ,"Reccomened Pet.", JOptionPane.INFORMATION_MESSAGE);
}
else
if(a == 3 && b >= 6){
JOptionPane.showMessageDialog(null, "You should get a fish!" ,"Reccomened Pet.", JOptionPane.INFORMATION_MESSAGE);
}
else
if(a == 3 && b < 6){
JOptionPane.showMessageDialog(null, "You should get an Ant farm!" ,"Reccomened Pet.", JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String[] args) {
dwelling();
hours();
//I don't know what parameters to call. Most likley due to the method variables. Can't figure out what to fix.
pet(dwelling(), hours());
}
}
Upvotes: 0
Views: 567
Reputation: 6486
You yourself are calling the methods twice. Every instance of dwelling()
is a method call to that method. And finally, since you pass in the variables a, b
into your pet(int a, int b)
method, there is no reason to fetch them again by calling dwelling()
and hours()
To remove the redundant call in pet(a, b)
:
public static void pet(int a, int b){
//remove these and just use the values passed in from main method
//a = dwelling();
//b = hours();
...
}
public static void main(String[] args) {
pet(dwelling(), hours());
}
OR
Keep the calls to dwelling()
and hours()
inside your pet()
method, and change it to not need the two arguments (so you don't call them in you main method).
public static void pet(){
//call the methods here, instead of passing in their return values
int a = dwelling();
int b = hours();
...
}
public static void main(String[] args) {
pet();
}
Upvotes: 0
Reputation: 285405
The methods are being called twice because your code calls them twice, once at the beginning of main, where you call them but then throw out the results returned:
dwelling();
hours();
And a 2nd time in the 3rd method's parameters:
pet(dwelling(), hours());
Save the values returned from your method calls and pass them into the 3rd method. Don't recall them in the 3rd method parameter as you're doing. For example, change
public static void main(String[] args) {
dwelling();
hours();
pet(dwelling(), hours());
}
to
public static void main(String[] args) {
int dwell = dwelling();
int hrs = hours();
pet(dwell, hrs);
}
And yeah, change
public static void pet(int a, int b){
a = dwelling();//Pretty sure these variables are declared wrong
b = hours();
to
public static void pet(int a, int b){
// these guys below are completely unnecessary
// a = dwelling();
// b = hours();
Upvotes: 1
Reputation: 177
pet(dwelling(), hours());
There is your problem. Try setting them to values first then sending those values to the pet() method. Example:
public static void main(String[] args) {
ind dw = dwelling();
int h = hours();
//I don't know what parameters to call. Most likley due to the method variables. Can't figure out what to fix.
pet(dw, h);
}
Upvotes: 1