Why does the return-statement not quit the method? [closed]Does a finally block always get executed in Java?Avoiding != null statementsWhat is a serialVersionUID and why should I use it?Why does Java have transient fields?Why is subtracting these two times (in 1927) giving a strange result?Why don't Java's +=, -=, *=, /= compound assignment operators require casting?Why is char[] preferred over String for passwords?Why is it faster to process a sorted array than an unsorted array?Why does this code using random strings print “hello world”?Why is printing “B” dramatically slower than printing “#”?
When writing an error prompt, should we end the sentence with a exclamation mark or a dot?
My coworkers think I had a long honeymoon. Actually I was diagnosed with cancer. How do I talk about it?
Why only the fundamental frequency component is said to give useful power?
Java guess the number
Can you really not move between grapples/shoves?
Last survivors from different time periods living together
Russian equivalents of "no love lost"
How to pass a regex when finding a directory path in bash?
How hard would it be to convert a glider into an powered electric aircraft?
Do any instruments not produce overtones?
Strange symbol for two functions
Remove sudoers using script
Why don't B747s start takeoffs with full throttle?
Do simulator games use a realistic trajectory to get into orbit?
What is the purpose of building foundations?
PL/SQL function to receive a number and return its binary format
What are the peak hours for public transportation in Paris?
Can an Eldritch Knight use Action Surge and thus Arcane Charge even when surprised?
How did students remember what to practise between lessons without any sheet music?
Does the "6 seconds per round" rule apply to speaking/roleplaying during combat situations?
What is the advantage of carrying a tripod and ND-filters when you could use image stacking instead?
How do I write "Show, Don't Tell" as a person with Asperger Syndrome?
Why is the relationship between frequency and pitch exponential?
Should an arbiter claim draw at a K+R vs K+R endgame?
Why does the return-statement not quit the method? [closed]
Does a finally block always get executed in Java?Avoiding != null statementsWhat is a serialVersionUID and why should I use it?Why does Java have transient fields?Why is subtracting these two times (in 1927) giving a strange result?Why don't Java's +=, -=, *=, /= compound assignment operators require casting?Why is char[] preferred over String for passwords?Why is it faster to process a sorted array than an unsorted array?Why does this code using random strings print “hello world”?Why is printing “B” dramatically slower than printing “#”?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm working in Junit/Java for an assignment. This method should go to the correct if-statement, advance the day in a way the common calendar does and then exit the whole method.
I've googled this problem extensively. The only pages I've found, point me to the exact thing i'm trying to do. It is likely that i'm missing something, I just don't know what. When I run the tests through the debugger, I see that Java does go to the correct statement, it just "ignores" the return.
protected final void advanceDay()
int[] highMonth = new int[]1, 3, 5, 7, 8, 10, 12;
boolean isMonth31 = false;
for (int x : highMonth)
if (this.monthFirstDay == x)
isMonth31 = true;
//checks if month has 31 days
if (this.dayFristDay >= 30)
if (this.dayFristDay == 31)
this.dayFristDay = 1;
this.monthFirstDay++;
return;
//if it's the 31st, then proceed to the next month, the day is set to one.
if (this.dayFristDay == 31 && this.monthFirstDay == 12)
this.dayFristDay = 1;
this.monthFirstDay = 1;
return;
//if it's december the 31st, set the date to january 1st
if (isMonth31 && this.dayFristDay == 30)
this.dayFristDay++;
System.out.println("");
return;
//if the month has 31 days, but it is the 30st, just advance the day.
if (!isMonth31 && this.dayFristDay == 30)
this.monthFirstDay++;
this.dayFristDay = 1;
System.out.println("");
return;
//if the month has 30 days and it is the 30st, advance the month and set the day to one.
if (this.dayFristDay == 28 && this.monthFirstDay == 2)
this.monthFirstDay++;
this.dayFristDay = 1;
System.out.println("");
return;
//if it's the 28st of february, advance to march the first.
System.out.println("");
this.dayFristDay++;
The prints are meant as breakpoints for the debugger. If any of the the if-statements is true, I should never get to the last print. But I keep getting to the last print, while it's not supposed to.
EDIT: to reproduce the error:
// to use in different class in the same package
Cockpit testCP = new Cockpit(28, 2);
testCP.advanceDay();
public class Cockpit
private int dayFristDay;
private int monthFirstDay;
public Cockpit(int dayFristDay, int monthFirstDay)
this.dayFristDay = dayFristDay;
this.monthFirstDay = monthFirstDay;
//advanceDay method as a above
protected String getCurrentDay()
return this.dayFristDay + "-" + this.monthFirstDay;
java return
closed as off-topic by Hovercraft Full Of Eels, EJoshuaS, Slaw, Sir E_net4 the Downvoter, Mike M. Mar 24 at 18:28
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Hovercraft Full Of Eels, EJoshuaS, Slaw, Sir E_net4 the Downvoter
|
show 4 more comments
I'm working in Junit/Java for an assignment. This method should go to the correct if-statement, advance the day in a way the common calendar does and then exit the whole method.
I've googled this problem extensively. The only pages I've found, point me to the exact thing i'm trying to do. It is likely that i'm missing something, I just don't know what. When I run the tests through the debugger, I see that Java does go to the correct statement, it just "ignores" the return.
protected final void advanceDay()
int[] highMonth = new int[]1, 3, 5, 7, 8, 10, 12;
boolean isMonth31 = false;
for (int x : highMonth)
if (this.monthFirstDay == x)
isMonth31 = true;
//checks if month has 31 days
if (this.dayFristDay >= 30)
if (this.dayFristDay == 31)
this.dayFristDay = 1;
this.monthFirstDay++;
return;
//if it's the 31st, then proceed to the next month, the day is set to one.
if (this.dayFristDay == 31 && this.monthFirstDay == 12)
this.dayFristDay = 1;
this.monthFirstDay = 1;
return;
//if it's december the 31st, set the date to january 1st
if (isMonth31 && this.dayFristDay == 30)
this.dayFristDay++;
System.out.println("");
return;
//if the month has 31 days, but it is the 30st, just advance the day.
if (!isMonth31 && this.dayFristDay == 30)
this.monthFirstDay++;
this.dayFristDay = 1;
System.out.println("");
return;
//if the month has 30 days and it is the 30st, advance the month and set the day to one.
if (this.dayFristDay == 28 && this.monthFirstDay == 2)
this.monthFirstDay++;
this.dayFristDay = 1;
System.out.println("");
return;
//if it's the 28st of february, advance to march the first.
System.out.println("");
this.dayFristDay++;
The prints are meant as breakpoints for the debugger. If any of the the if-statements is true, I should never get to the last print. But I keep getting to the last print, while it's not supposed to.
EDIT: to reproduce the error:
// to use in different class in the same package
Cockpit testCP = new Cockpit(28, 2);
testCP.advanceDay();
public class Cockpit
private int dayFristDay;
private int monthFirstDay;
public Cockpit(int dayFristDay, int monthFirstDay)
this.dayFristDay = dayFristDay;
this.monthFirstDay = monthFirstDay;
//advanceDay method as a above
protected String getCurrentDay()
return this.dayFristDay + "-" + this.monthFirstDay;
java return
closed as off-topic by Hovercraft Full Of Eels, EJoshuaS, Slaw, Sir E_net4 the Downvoter, Mike M. Mar 24 at 18:28
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Hovercraft Full Of Eels, EJoshuaS, Slaw, Sir E_net4 the Downvoter
5
Thereturn
will end the method. You are having wrong assumptions. If the method does not end, you are not hitting the statements. Place some prints to confirm if you enter or not, or use your debugger.
– Zabuza
Mar 24 at 15:35
Are you reachingif
statements or their bodies?
– Andronicus
Mar 24 at 15:36
@Zabuza Both the print and the debugger confirm that it goes into the februay-28-if and to the last one as if the return does not exist.
– Jay Aberlour
Mar 24 at 15:39
1
@JayAberlour post a complete minimal example that we can copy, paste and run by ourselves to reproduce the problem. But I doubt you'll be able to do that, because ifreturn
didn't work, I think someone would have noticed by now.
– JB Nizet
Mar 24 at 15:43
1
This is a really complicated approach to the problem: just define an array containing the month lengths, and then check if (day+1) exceeds the current month's length.
– Andy Turner
Mar 24 at 15:45
|
show 4 more comments
I'm working in Junit/Java for an assignment. This method should go to the correct if-statement, advance the day in a way the common calendar does and then exit the whole method.
I've googled this problem extensively. The only pages I've found, point me to the exact thing i'm trying to do. It is likely that i'm missing something, I just don't know what. When I run the tests through the debugger, I see that Java does go to the correct statement, it just "ignores" the return.
protected final void advanceDay()
int[] highMonth = new int[]1, 3, 5, 7, 8, 10, 12;
boolean isMonth31 = false;
for (int x : highMonth)
if (this.monthFirstDay == x)
isMonth31 = true;
//checks if month has 31 days
if (this.dayFristDay >= 30)
if (this.dayFristDay == 31)
this.dayFristDay = 1;
this.monthFirstDay++;
return;
//if it's the 31st, then proceed to the next month, the day is set to one.
if (this.dayFristDay == 31 && this.monthFirstDay == 12)
this.dayFristDay = 1;
this.monthFirstDay = 1;
return;
//if it's december the 31st, set the date to january 1st
if (isMonth31 && this.dayFristDay == 30)
this.dayFristDay++;
System.out.println("");
return;
//if the month has 31 days, but it is the 30st, just advance the day.
if (!isMonth31 && this.dayFristDay == 30)
this.monthFirstDay++;
this.dayFristDay = 1;
System.out.println("");
return;
//if the month has 30 days and it is the 30st, advance the month and set the day to one.
if (this.dayFristDay == 28 && this.monthFirstDay == 2)
this.monthFirstDay++;
this.dayFristDay = 1;
System.out.println("");
return;
//if it's the 28st of february, advance to march the first.
System.out.println("");
this.dayFristDay++;
The prints are meant as breakpoints for the debugger. If any of the the if-statements is true, I should never get to the last print. But I keep getting to the last print, while it's not supposed to.
EDIT: to reproduce the error:
// to use in different class in the same package
Cockpit testCP = new Cockpit(28, 2);
testCP.advanceDay();
public class Cockpit
private int dayFristDay;
private int monthFirstDay;
public Cockpit(int dayFristDay, int monthFirstDay)
this.dayFristDay = dayFristDay;
this.monthFirstDay = monthFirstDay;
//advanceDay method as a above
protected String getCurrentDay()
return this.dayFristDay + "-" + this.monthFirstDay;
java return
I'm working in Junit/Java for an assignment. This method should go to the correct if-statement, advance the day in a way the common calendar does and then exit the whole method.
I've googled this problem extensively. The only pages I've found, point me to the exact thing i'm trying to do. It is likely that i'm missing something, I just don't know what. When I run the tests through the debugger, I see that Java does go to the correct statement, it just "ignores" the return.
protected final void advanceDay()
int[] highMonth = new int[]1, 3, 5, 7, 8, 10, 12;
boolean isMonth31 = false;
for (int x : highMonth)
if (this.monthFirstDay == x)
isMonth31 = true;
//checks if month has 31 days
if (this.dayFristDay >= 30)
if (this.dayFristDay == 31)
this.dayFristDay = 1;
this.monthFirstDay++;
return;
//if it's the 31st, then proceed to the next month, the day is set to one.
if (this.dayFristDay == 31 && this.monthFirstDay == 12)
this.dayFristDay = 1;
this.monthFirstDay = 1;
return;
//if it's december the 31st, set the date to january 1st
if (isMonth31 && this.dayFristDay == 30)
this.dayFristDay++;
System.out.println("");
return;
//if the month has 31 days, but it is the 30st, just advance the day.
if (!isMonth31 && this.dayFristDay == 30)
this.monthFirstDay++;
this.dayFristDay = 1;
System.out.println("");
return;
//if the month has 30 days and it is the 30st, advance the month and set the day to one.
if (this.dayFristDay == 28 && this.monthFirstDay == 2)
this.monthFirstDay++;
this.dayFristDay = 1;
System.out.println("");
return;
//if it's the 28st of february, advance to march the first.
System.out.println("");
this.dayFristDay++;
The prints are meant as breakpoints for the debugger. If any of the the if-statements is true, I should never get to the last print. But I keep getting to the last print, while it's not supposed to.
EDIT: to reproduce the error:
// to use in different class in the same package
Cockpit testCP = new Cockpit(28, 2);
testCP.advanceDay();
public class Cockpit
private int dayFristDay;
private int monthFirstDay;
public Cockpit(int dayFristDay, int monthFirstDay)
this.dayFristDay = dayFristDay;
this.monthFirstDay = monthFirstDay;
//advanceDay method as a above
protected String getCurrentDay()
return this.dayFristDay + "-" + this.monthFirstDay;
java return
java return
edited Mar 24 at 16:01
Jay Aberlour
asked Mar 24 at 15:33
Jay AberlourJay Aberlour
176
176
closed as off-topic by Hovercraft Full Of Eels, EJoshuaS, Slaw, Sir E_net4 the Downvoter, Mike M. Mar 24 at 18:28
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Hovercraft Full Of Eels, EJoshuaS, Slaw, Sir E_net4 the Downvoter
closed as off-topic by Hovercraft Full Of Eels, EJoshuaS, Slaw, Sir E_net4 the Downvoter, Mike M. Mar 24 at 18:28
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Hovercraft Full Of Eels, EJoshuaS, Slaw, Sir E_net4 the Downvoter
5
Thereturn
will end the method. You are having wrong assumptions. If the method does not end, you are not hitting the statements. Place some prints to confirm if you enter or not, or use your debugger.
– Zabuza
Mar 24 at 15:35
Are you reachingif
statements or their bodies?
– Andronicus
Mar 24 at 15:36
@Zabuza Both the print and the debugger confirm that it goes into the februay-28-if and to the last one as if the return does not exist.
– Jay Aberlour
Mar 24 at 15:39
1
@JayAberlour post a complete minimal example that we can copy, paste and run by ourselves to reproduce the problem. But I doubt you'll be able to do that, because ifreturn
didn't work, I think someone would have noticed by now.
– JB Nizet
Mar 24 at 15:43
1
This is a really complicated approach to the problem: just define an array containing the month lengths, and then check if (day+1) exceeds the current month's length.
– Andy Turner
Mar 24 at 15:45
|
show 4 more comments
5
Thereturn
will end the method. You are having wrong assumptions. If the method does not end, you are not hitting the statements. Place some prints to confirm if you enter or not, or use your debugger.
– Zabuza
Mar 24 at 15:35
Are you reachingif
statements or their bodies?
– Andronicus
Mar 24 at 15:36
@Zabuza Both the print and the debugger confirm that it goes into the februay-28-if and to the last one as if the return does not exist.
– Jay Aberlour
Mar 24 at 15:39
1
@JayAberlour post a complete minimal example that we can copy, paste and run by ourselves to reproduce the problem. But I doubt you'll be able to do that, because ifreturn
didn't work, I think someone would have noticed by now.
– JB Nizet
Mar 24 at 15:43
1
This is a really complicated approach to the problem: just define an array containing the month lengths, and then check if (day+1) exceeds the current month's length.
– Andy Turner
Mar 24 at 15:45
5
5
The
return
will end the method. You are having wrong assumptions. If the method does not end, you are not hitting the statements. Place some prints to confirm if you enter or not, or use your debugger.– Zabuza
Mar 24 at 15:35
The
return
will end the method. You are having wrong assumptions. If the method does not end, you are not hitting the statements. Place some prints to confirm if you enter or not, or use your debugger.– Zabuza
Mar 24 at 15:35
Are you reaching
if
statements or their bodies?– Andronicus
Mar 24 at 15:36
Are you reaching
if
statements or their bodies?– Andronicus
Mar 24 at 15:36
@Zabuza Both the print and the debugger confirm that it goes into the februay-28-if and to the last one as if the return does not exist.
– Jay Aberlour
Mar 24 at 15:39
@Zabuza Both the print and the debugger confirm that it goes into the februay-28-if and to the last one as if the return does not exist.
– Jay Aberlour
Mar 24 at 15:39
1
1
@JayAberlour post a complete minimal example that we can copy, paste and run by ourselves to reproduce the problem. But I doubt you'll be able to do that, because if
return
didn't work, I think someone would have noticed by now.– JB Nizet
Mar 24 at 15:43
@JayAberlour post a complete minimal example that we can copy, paste and run by ourselves to reproduce the problem. But I doubt you'll be able to do that, because if
return
didn't work, I think someone would have noticed by now.– JB Nizet
Mar 24 at 15:43
1
1
This is a really complicated approach to the problem: just define an array containing the month lengths, and then check if (day+1) exceeds the current month's length.
– Andy Turner
Mar 24 at 15:45
This is a really complicated approach to the problem: just define an array containing the month lengths, and then check if (day+1) exceeds the current month's length.
– Andy Turner
Mar 24 at 15:45
|
show 4 more comments
1 Answer
1
active
oldest
votes
Cockpit testCP = new Cockpit(28, 2);
this.testCP.advanceDay();
Line 2 isn't invoking advanceDay
on the instance you created on line 1. You're invoking it on the instance referred to by some member variable.
Remove the this
.
Ideone demo, showing that return works
True, but irrelevant to my issue. Bug also happens without this.
– Jay Aberlour
Mar 24 at 16:15
2
@JayAberlour then you're going to have to provide a better example to reproduce the issue, because I copied and pasted your code, and return worked (link in answer).
– Andy Turner
Mar 24 at 16:18
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Cockpit testCP = new Cockpit(28, 2);
this.testCP.advanceDay();
Line 2 isn't invoking advanceDay
on the instance you created on line 1. You're invoking it on the instance referred to by some member variable.
Remove the this
.
Ideone demo, showing that return works
True, but irrelevant to my issue. Bug also happens without this.
– Jay Aberlour
Mar 24 at 16:15
2
@JayAberlour then you're going to have to provide a better example to reproduce the issue, because I copied and pasted your code, and return worked (link in answer).
– Andy Turner
Mar 24 at 16:18
add a comment |
Cockpit testCP = new Cockpit(28, 2);
this.testCP.advanceDay();
Line 2 isn't invoking advanceDay
on the instance you created on line 1. You're invoking it on the instance referred to by some member variable.
Remove the this
.
Ideone demo, showing that return works
True, but irrelevant to my issue. Bug also happens without this.
– Jay Aberlour
Mar 24 at 16:15
2
@JayAberlour then you're going to have to provide a better example to reproduce the issue, because I copied and pasted your code, and return worked (link in answer).
– Andy Turner
Mar 24 at 16:18
add a comment |
Cockpit testCP = new Cockpit(28, 2);
this.testCP.advanceDay();
Line 2 isn't invoking advanceDay
on the instance you created on line 1. You're invoking it on the instance referred to by some member variable.
Remove the this
.
Ideone demo, showing that return works
Cockpit testCP = new Cockpit(28, 2);
this.testCP.advanceDay();
Line 2 isn't invoking advanceDay
on the instance you created on line 1. You're invoking it on the instance referred to by some member variable.
Remove the this
.
Ideone demo, showing that return works
edited Mar 24 at 16:17
answered Mar 24 at 16:00
Andy TurnerAndy Turner
88.5k991158
88.5k991158
True, but irrelevant to my issue. Bug also happens without this.
– Jay Aberlour
Mar 24 at 16:15
2
@JayAberlour then you're going to have to provide a better example to reproduce the issue, because I copied and pasted your code, and return worked (link in answer).
– Andy Turner
Mar 24 at 16:18
add a comment |
True, but irrelevant to my issue. Bug also happens without this.
– Jay Aberlour
Mar 24 at 16:15
2
@JayAberlour then you're going to have to provide a better example to reproduce the issue, because I copied and pasted your code, and return worked (link in answer).
– Andy Turner
Mar 24 at 16:18
True, but irrelevant to my issue. Bug also happens without this.
– Jay Aberlour
Mar 24 at 16:15
True, but irrelevant to my issue. Bug also happens without this.
– Jay Aberlour
Mar 24 at 16:15
2
2
@JayAberlour then you're going to have to provide a better example to reproduce the issue, because I copied and pasted your code, and return worked (link in answer).
– Andy Turner
Mar 24 at 16:18
@JayAberlour then you're going to have to provide a better example to reproduce the issue, because I copied and pasted your code, and return worked (link in answer).
– Andy Turner
Mar 24 at 16:18
add a comment |
5
The
return
will end the method. You are having wrong assumptions. If the method does not end, you are not hitting the statements. Place some prints to confirm if you enter or not, or use your debugger.– Zabuza
Mar 24 at 15:35
Are you reaching
if
statements or their bodies?– Andronicus
Mar 24 at 15:36
@Zabuza Both the print and the debugger confirm that it goes into the februay-28-if and to the last one as if the return does not exist.
– Jay Aberlour
Mar 24 at 15:39
1
@JayAberlour post a complete minimal example that we can copy, paste and run by ourselves to reproduce the problem. But I doubt you'll be able to do that, because if
return
didn't work, I think someone would have noticed by now.– JB Nizet
Mar 24 at 15:43
1
This is a really complicated approach to the problem: just define an array containing the month lengths, and then check if (day+1) exceeds the current month's length.
– Andy Turner
Mar 24 at 15:45