unable to find problems with this piece of code - java

I have this piece of code, I think I have all the braces covered. Still getting braces not closed error. When I put more braces, then gives me something else. Can you anybody tell me what I am doing wrong here?
public static boolean isValid(int day, int month, int year)
{
if (year < 1900)
{
return false;
}
else {
if (month>0 && month<13)
{
if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
{
return day > 0 && day <=31;
}
else if (month==4 || month==6 || month==9 || month==11)
{
return day>0 && day<=30;
}
else if (month==2)
{
if (isLeap(year))
{
//(d= 29);
return day>0 && day <=29;
}
else {
return day>0 && day<= 28;
}
}
}
}
}
}

Because of your if-else conditions it may not be able to return a boolean value.That is the problem.Check your code's logic.

I don't think you are missing any braces but you are not returning values for every possible execution path.
if you put a return false at the bottom of the function it will compile.
Little tip, to save the reader all of those nested ifs and braces:
if(day>0 && day<= 28){
return true;
else{
return false;
}
you can do this:
return day > 0 && day <= 28;

if you're using eclipse use ctrl shift f to balance braces easier to read and see if you miss one or need one

Related

How can I simplify this Java code?

I am fairly new in Java and need to do some ugly if / else code.
if (st1 == 0 || st2 == 0 || st3 == 0) {
if (st1 == 0) {
return a;
} else if (st2 == 0) {
return b;
} else {
return c;
}
}
But to me it seems like there should be some simpler way to do this kind of code.
(I know i could leave the outer if away, but it is for the purpose of showing the problem)
I hope somebody has an idea on how to beautify this code :)
Remove the outer condition, and remove unnecessary 'else':
if (st1 == 0) {
return a;
}
if (st2 == 0) {
return b;
}
if (st3 == 0) {
return c;
}

Unreachable Statement in my Public Boolean Method

So I'm trying to create an isValid method that verifies if a certain date is real or not (i.e. 3/31/2016 is valid, 2/29/2001 is valid since it's a leap year, 2/30/2016 is not valid)
Here is my method public boolean isValid()
`
{
//January
if (month == 1 && day <= 31) {
return true;
}
else {
return false;
}
//February
if (month == 2 && day <= 28) {
return true;
}
else {
if ((((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0)) {
if (day == 29) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
//March
if (month == 3 && day <= 31) {
return true;
}
else {
return false;
}
//April
if (month == 4 && day <= 30) {
return true;
}
else {
return false;
}
//May
if (month == 5 && day <= 31) {
return true;
}
else {
return false;
}
//June
if (month == 6 && day <= 30) {
return true;
}
else {
return false;
}
//July
if (month == 7 && day <= 31) {
return true;
}
else {
return false;
}
//August
if (month == 8 && day <= 31) {
return true;
}
else {
return false;
}
//September
if (month == 9 && day <= 30) {
return true;
}
else {
return false;
}
//October
if (month == 10 && day <= 31) {
return true;
}
else {
return false;
}
//November
if (month == 11 && day <= 30) {
return true;
}
else {
return false;
}
//December
if (month == 12 && day <= 31) {
return true;
}
else {
return false;
}
}
`
Now, when I compile, it says there are unreachable statements essentially wherever it says "if". Could somebody please help? I've already tried the 'if (true) {return}' method and I can't find anything else helpful.
In your first if statement, it returns regardless of outcome, meaning all the following if statements will never be reached as if your first condition is not met it will return, and if it is, it also returns.
A fix for this is to remove all your else return false paths and string all if statements in an else if chain and then return false at the end of that.
e.g.
if(){
...
}else if(){
...
}
return false;
Your code should if elseif elseif elseif... else...
when you say if and else.. your code will end either in if or else.. henceforth other if statements are unreachable...
that is the error you are getting...change other conditions to elseif
Your code will never go to the second if statement as the else part of first(January's) will terminate your program
Your code has to be like:
if(month == 1)
{
if(day <= 31)
return true;
else
return false;
}
if(month==2)
{
----
----
}
return **statement should be last statement of any method**
else you can use variable instead of direct return statement like
public boolean isValid()
{
boolean status=false;
if(condition)
{
status=true;
}
return status;
}

Can't figure out how to fix my code

Hi for the past few hours i have been trying to fix my code. The problem is that when I go to check if "" is an integer is returns true when it should be false. I know why this is happening, it is because it doesn't enter the for loop and returns true , but I can't seem to figure out how to make it return false for "". I can provide more info if needed.
public boolean isInteger(String str)
{
for (int x = 0, n = str.length(); x < n; x++)
{
char c = str.charAt(x);
if (c < '0' || c > '9')
{
if (c != 0 || c != '-')
{
return false;
}
}
}
return true;
}
Thank you for spending your time on trying to help me :)
You could check valid input (ie, a string with length = 0) and return false before you ever try the loop. You're correct, though, it's not entering the loop and just returning true.
--edit--
Something like
if (string == null) || (string.length() == 0){
return false
}
In your algorithm, an empty string will always return true. You just need to add a check:
if(str==null || str.length()==0) return false;
Alternatively, you can use this function:
public static boolean isInteger(String str)
{
try
{
Integer.parseInt(str);
return true;
}
catch(NumberFormatException e)
{
return false;
}
}

Java nested ifs

Below is a small function which when given two numbers (a, b), returns true if one of the numbers is a teen-number. Returns false if both are teen. Returns false if both are not teen. I failed these test cases but I can't figure out why. Help?
(13, 99), (14, 20), and (16, 9)
public boolean loneTeen(int a, int b)
{
if(a<=19 && a>=13)
{
if(b<=19 && b>=13)
{
return false;
}
}
else if(a<=19 && a>=13)
{
return true;
}
else if(b<=19 && b>=13)
{
return true;
}
return false;
}
All three test cases will enter first if branch, they will not match inner condition and, since they already matched first branch, will not match any of else if's. So, they will all return false which is wrong.
Using a small auxiliary method can make your life much easier (and the code more readable!):
private boolean isTeen(int a) {
return a > 12 && a < 20;
}
public boolean loneTeen(int a, int b) {
if(isTeen(a) && isTeen(b) ||
!isTeen(a) && !isTeen(b)) {
return false;
}
return true;
}
First else won't be executed as you are putting the same condition on both if and else.try putting
if( a>=13 && a<=19 && b>=13 && b<=19){return false;}
else if(a>=13 && a<= 19){return true;}
else if(b<=19 && b>=13){return true;}
else return false;

this method must return a result of type boolean, java

public boolean Winner() {
for (int z = 0; z < 3; z++) {
if (board[z] != null && board[z] == board[z+3] && board[z] == board[z+6]
) {
return true;
}
}
for(int i=0; i<7;i+=3){
if (board[i] != null && board[i] == board[i+1] && board[i] == board[i+2]) {
return true;}
}
}
It returns me this error: this method must return a result of type boolean. What am I doing wrong?
Right now, the function isn't guaranteed to return a boolean, because it's possible that neither of the if statements will ever be entered.
You could fix it like this (but only do this if it's actually what your logic needs!):
public boolean Winner() {
for (int z = 0; z < 3; z++) {
if (board[z] != null && board[z] == board[z+3] && board[z] == board[z+6]
) {
return true;
}
}
for(int i=0; i<7;i+=3){
if (board[i] != null && board[i] == board[i+1] && board[i] == board[i+2]) {
return true;}
}
return false;
}
The Java compiler doesn't make assumptions that a for loop will have an iteration or that an if statement block will run.
There are execution paths where there is no return statement. What happens if the execution path doesn't execute any of the existing return statements and drops to the bottom? There isn't a return there.
Add a return at the bottom.
All possible ways that the method can exit need to return something. If your code makes it through both for loops without having an if condition evaluate to true, then you need a return at the end that specifies what gets returned.
The compiler is not aware that at least one of the loops will be executed. It takes into account all the possibilities of the execution and one of them is that neither of the loops will be executed and in that case there is no return statement. So insert a return statement out of the loops as well.
The answer to this question is easy. It happened to me too. The problem in your code is that you don't say to the computer what to do in case that the "if" statement is wrong, so you just have to add an "else {return false}" to every "if". Another tip is: please make your code cleaner and readable.
public boolean Winner() {
for (int z = 0; z < 3; z++) {
if (board[z] != null && board[z] == board[z+3] && board[z] == board[z+6]) {
return true;
} else {
return false;
}
}
for (int i=0; i<7; i+=3) {
if (board[i] != null && board[i] == board[i+1] && board[i] == board[i+2]) {
return true;
} else {
return false;
}
}
}

Categories

Resources