Trying to find the index if the array is in range [duplicate] - 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;
}
}
}

Related

Why do I get a StringOutOfBoundsException even though I am within bounds of the String array?

I'm currently attempting this problem and seem to keep getting this StringOutOfBoundsException, can anyone explain to me why?
When I exit the while loop, it seems that I mistyped endPtr--;
class Solution {
public boolean isPalindrome(String s) {
if (s == null) {
throw new IllegalArgumentException("Illegal input!");
}
if (s.length() <= 1) {
return true;
}
int startPtr = 0;
int endPtr = s.length() - 1;
while (startPtr < endPtr) {
while (startPtr < endPtr && !Character.isLetterOrDigit(s.charAt(startPtr))) {
startPtr++;
}
//ERROR while (startPtr < endPtr && !Character.isLetterOrDigit(s.charAt(endPtr))) {
//endPtr--;
//}
if (startPtr < endPtr && Character.toLowerCase(s.charAt(startPtr)) != Character.toLowerCase(s.charAt(endPtr))) {
return false;
}
startPtr++;
endPtr++;
}
return true;
}
}
In the end of the main loop, you are incrementing endPtr.
startPtr++;
endPtr++;
By doing so, you are positionning the end of your String after its real ending.
And here comes the error.
And also, you are creating an infinite loop because the condition
while (startPtr < endPtr)
will always be true.

Loop: This method must return a result of type boolean

This is the error I get:
This method must return a result of type boolean
And this is the code:
public boolean seleccionar(Aeronave otra) {
for (int i = 0; i < this.as.length; i++) {
if (otra != null && !otra.equals(this.as[i]) && otra.amenazadaPor(this.as[i])) {
return true;
} else {
return false;
}
}
}
Add a return false before the last brace. Your function doesn't return anything if this.as.length == 0, and Java is giving a compile error because of that.
The issue is that it is possible that the for-loop will loop through all elements and eventually reach the end and no result is returned. In this case we return false to ensure this.
public boolean seleccionar (Aeronave otra) {
for (int i=0; i < this.as.length; i++) {
if (otra !=null && !otra.equals(this.as[i]) && otra.amenazadaPor(this.as[i])) {
return true;
}
}
return false;
}
Your code will exit on first loop element. But when array this.as is empty, so loop will not execute, then your function is missing a return value -therefore compiler does not allow this.
To solve this issue, simply move return false after the loop ends.
public boolean seleccionar (Aeronave otra) {
for (int i=0; i < this.as.length; i++) {
if (otra !=null && !otra.equals(this.as[i]) && otra.amenazadaPor(this.as[i])) {
return true;
}
}
return false; // if no elements are matching loop condition, return false
}}

Palindrome Stack

I wrote this method to check to see if a words is a palindrome, but when it is a palindrome it keeps returning false
public boolean isPalindrome(String s){
int i;
int n = s.length();
Stack <Character> stack = new Stack <Character>();
for (i = 0; i < n/2; i++)
stack.push(s.charAt(i));
if (n%2 == 1)
i++;
while(!stack.empty( )) {
char c = stack.pop( );
if (c != s.charAt(i));
return false;
}
return true;
}
I'm not sure why you're not using { } brackets. Try to learn proper Java conventions early.
if (c != s.charAt(i)); // <- this semicolon is your problem
return false;
Is equivalent to:
if (c != s.charAt(i)) {
// Do nothing
}
// Do this no matter what
return false;
Furthermore, the logic on your for-loop may be flawed for similar reasons. Remove your semicolon, and better yet, practice always using brackets:
if (c != s.charAt(i)) {
return false;
}
#jhamon also points out that you never actually increment i in your while loop:
while(!stack.empty( )) {
char c = stack.pop( );
if (c != s.charAt(i)) {
return false;
}
i++;
}

While loop: Need assistance

I can't find the problem that causes my while loop not to work.
When I run the program and press a radio button, I get this error code:
Syntax error, insert "while ( Expression ) ;" to complete DoStatement
Here is my loop:
int i = 1;
boolean x;
//for (i = 0; i < 6; i++) {
do{
warning.setText(" FEL!");
i++;
while(x == false);{
if(e.getSource() == buttonOK){
if(buttonDollar.isSelected() == false){
x = false;
}
if(buttonEuro.isSelected() == false){
x = false;
}
if(buttonPund.isSelected() == false){
x = false;
}
if(buttonKrona.isSelected() == false){
x = false;
}
break;
}
}
}
You are missing the syntax for "while" element
From the sun site (I am guessing that this is java)
do {
statement(s)
} while (expression);
I think you need a closing curly brace before the while
do{
warning.setText(" FEL!");
i++;
}while(x == false);
The whole structure is all wrong, you're looking to do something very simple:
if(e.getSource() == buttonOK)
{
if( !buttonDollar.isSelected() && !buttonEuro.isSelected()
&& !buttonPund.isSelected() && !buttonKrona.isSelected() )
{
warning.setText(" FEL!");
}
}
From a UI perspective, it's better to ensure that one radio button is always selected (as this is what your users will likely expect).

Java - modified compareTo method says it needs to return an int, but it should be returning one

I'm learning basic Java right now and have a problem with my code that I can't figure out. It's basically what the title says. My Java compiler is telling me that there's an error with my custom compareTo method, saying that it needs to return an int. The problem is, as far as I can tell, it IS returning an int. Yet it's still giving me an error. Could someone please point out in my code what's wrong? And also I have already implemented Comparable in my class. Here's my method:
public int compareTo(Homework other) {
if (getDaysLate() < other.getDaysLate()) {
return -1;
} else if ((dateSubmitted == other.dateSubmitted)
&& (files.compareTo(other.files) == -1)) {
return -1;
} else if ((dateSubmitted == other.dateSubmitted)
&& (files == other.files)) {
if (name.compareTo(other.name) == -1) {
return -1;
} else if (name.compareTo(other.name) == 1) {
return 1;
} else if (name.compareTo(other.name) == 0) {
return 0;
}
} else {
return 0;
}
}
There is a path in the third else that does't return anything.
else if ((dateSubmitted == other.dateSubmitted) && (files == other.files)) {
if (name.compareTo(other.name) == -1) {
return -1;
}
else if (name.compareTo(other.name) == 1) {
return 1;
}
else if (name.compareTo(other.name) == 0) {
return 0;
} else return ...
}
BTW, I'm not sure if I'm following the logic of your implementation, since it seems that you are returning 0 if dateSubmitted != other.dateSubmitted. compareTo should also be anti-symetric (i.e. sgn(x.compareTo(y)) == -sgn(y.compareTo(x))), but your implementation is not.
How can you be sure (with all these if and else) that you are always returning an int? It does not seem so obvious to me and aparently the compiler agrees with me too.
One way to solve this (probably not the best one) is to add a return -1; //or whatever value at the end of your function.
In your second else-if statement you have a code path that may not return anything. You say:
else if ((dateSubmitted == other.dateSubmitted) && (files == other.files)) {
if (name.compareTo(other.name) == -1) {
return -1;
}
else if (name.compareTo(other.name) == 1) {
return 1;
}
else if (name.compareTo(other.name) == 0) {
return 0;
}
, but what if none of those else if's are true? Try changing the last else-if in your second else-if statement to else.
You are missing an else after this branch:
else if (name.compareTo(other.name) == 0) {
return 0;
}
If the test fails (compareTo doesn't return 0) the method would have to exit without a return value, which is illegal in Java.
Also, compareTo may return any integer value, not only 0, 1, and -1.
The method should return appropriate value on all code flow paths; in other words, on all conditions when the method returns. In the following if block it does not return on one path that I've marked.
else if ((dateSubmitted == other.dateSubmitted) && (files == other.files)) {
if (name.compareTo(other.name) == -1) {
return -1;
}
else if (name.compareTo(other.name) == 1) {
return 1;
}
else if (name.compareTo(other.name) == 0) {
return 0;
}
// It should return something here, if none of the above "if" statements match.
// Or one of the above "else if" should be changed to "else"
}

Categories