unreachable statement calling for recursive method [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Writing this code, I'm getting 'unreachable statement' error when I try to compile, any time I try to reach a[x] in the recursive method.
public class recursion
{
public static boolean match (int [] a, int [] pattern)
{
if(pattern.length==0)
return true;
boolean x;
x=match(a,pattern,0,0);
if(x==true)
return true;
return false;
}
public static boolean match (int [] a, int [] pattern,int aCounter,int ptCounter)
{
int count=0;
int x=aCounter;
if(x==a.length);
{
if(count==pattern.length)
return true;
else return false;
}
if(a[x]>100)
{
count=0;
return match(a,pattern,aCounter+1,0);
}
else if(((pattern[ptCounter]==1)||(pattern[ptCounter]==0))&&((a[x]>-10)&&(a[x]<10)))
{
count++;
return match(a,pattern,aCounter+1,ptCounter+1);
}
else if(((pattern[ptCounter]==2)||(pattern[ptCounter]==0))&&(((a[x]<-10)&&(a[x]>-100))||((a[x]>9)&&(a[x]<100))))
{
count++;
return match(a,pattern,aCounter+1,ptCounter+1);
}
}
}
Would appreciate input regarding this issue and also about the calling of recursive method. Thank you!

Your problem is an unnecessary ; :
if(x==a.length); // here
{
if(count==pattern.length)
return true;
else return false;
}
This ; closes the if statement, so the following bock is always executed (and returns true or false), and the code after that block becomes unreachable.

Related

JAVA Linked List [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
public LinkedList<MazeCoord> getPath() {
return getPath();
}
public class MazeCoord {
final private int row; // final (non-static) means it can't be updated once
final private int col; // it's initialized
// create a MazeCoord with the given row and column
public MazeCoord(int row, int col) {
this.row = row;
this.col = col;
}
// get the row of this MazeCoord
public int getRow() { return row; }
// get the col of this MazeCoord;
public int getCol() { return col; }
}
Why when I try to run this in my Eclipse, the console prompts me stack over flow?
Can any one tell me the reason?
getPath() is calling itself, which causes an infinite chain of calls, that ends when the stack overflows. A correct recursive method must have a stopping condition.

how to write method for sequential search through array? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am writing a java method which runs through an array and if a value is present, then it returns the index of the value. It is not compiling, but I don't know what part of my code isn't comprehensive.
import java.util.*;
import static java.lang.System.out;
public class Lab26 {
public static void main(String[] args) {
}
public static int simpleSearch(int[] nums, int value) {
int nul = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == value) {
return i;
}
}
}
}
What if if (nums[i] == value) is never satisfied? You will not return anything but the method signature expects you to return an int.
in simpleSearch method, return an integer at the end.

Always Getting a "FALSE" [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm making a quiz program. First step, I'm taking the quiz questions and their correct answers from the teacher/ user. I have a child class named TrueFalseQuestion which takes the boolean model answer and the string question as a parameter. I've made an array of TrueFalseQuestion type and I'm stuck at this part where I run the code, insert a question and no matter whether the model answer I've inserted is true or false, it's always stored as false when I print it out. HELP?
Here's this part of my code:
System.out.println("How many true or false questions would you like to include in your quiz?");
int l=s.nextInt();
TrueFalseQuestion[] qu2= new TrueFalseQuestion[l];
int x;
for(x=0;x<l;x++){
System.out.println("Please insert question "+(x+1)+":\n");
String Q2=s.next();
System.out.println("Please insert the correct answer");
boolean A2=s.nextBoolean();
qu2[x]=new TrueFalseQuestion(Q2,A2);
System.out.println(qu2[x].GetCorrectAnswer());
}
EDIT: Here's the TrueFalseQuestion code
public class TrueFalseQuestion extends Question {
private boolean CorrectB;
public TrueFalseQuestion(String qu, boolean b){
super(qu);
}
#Override
public void GetQuestion() {
System.out.println(getMyText()+"\n Is this statement true or false?");
}
#Override public String GetAnswer() {
System.out.println("Insert Answer: ");
boolean MyAnswer=s.nextBoolean();
return Boolean.toString(MyAnswer);
}
#Override public String CheckAnswer() {
return Boolean.toString(GetAnswer().equalsIgnoreCase(Boolean.toString(GetCorrectAnswer(‌​))));
}
/** * return the MyAnswer / /* * return the CorrectAnswer / public boolean GetCorrectAnswer() { return CorrectB; } /* * return the MyAnswer */
}
your problem is your constructor:
public TrueFalseQuestion(String qu, boolean b){
super(qu);
}
You don't set the field CorrectB so it defaults to its default value which in java is false
the fix is
public TrueFalseQuestion(String qu, boolean b){
super(qu);
CorrectB = b;
}
Problem is nextInt() leaves "\n" in the stream and when next() executes in
String Q2=s.next();
It reads "\n" and doesn't gives you an opportunity to input Question.
Try this:
System.out.println("How many true or false questions would you like to include in your quiz?");
int l=Integer.parseInt(nextLine());
TrueFalseQuestion[] qu2= new TrueFalseQuestion[l];
int x;
for(x=0;x<l;x++){
System.out.println("Please insert question "+(x+1)+":\n");
String Q2=s.nextLine();
System.out.println("Please insert the correct answer");
boolean A2=s.nextBoolean();
qu2[x]=new TrueFalseQuestion(Q2,A2);
System.out.println(qu2[x].GetCorrectAnswer());
}

Translating a ternary operation to an if/else statement? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have the following boolean method in Java, but I can't understand its return statement because it uses a ternary operation. Can anyone rewrite it to an if/else statement so that I can better understand what the ternary operation is doing?
public boolean collidesWith(Rectangle object){
return (isDestroyed)? false:hitbox.intersects(object);
}
The ternary operator is a short-hand for writing an if-else statement. Its general for is
<boolean condition to evaluate> ?
<return value if condition is true - i.e., the "if" branch > :
<return value is condition is false - i.e., the "else" branch>
So, if you unwrap the method you showed, you'd get:
public boolean collidesWith(Rectangle object){
if (isDestroyed) {
return false;
} else {
return hitbox.intersects(object);
}
}
Firstly, here is how I would write the method you posted (adding whitespace):
public boolean collidesWith(Rectangle object) {
return isDestroyed ? false : hitbox.intersects(object);
}
Here is the if-else you're looking for:
public boolean collidesWith(Rectangle object) {
if (isDestroyed) {
return false;
}
else {
return hitbox.intersects(object);
}
}
..or a bit simplified:
public boolean collidesWith(Rectangle object) {
if (isDestroyed)
return false;
return hitbox.intersects(object);
}
You may also make the ternary operator look a bit like an if-else:
public boolean collidesWith(Rectangle object) {
return isDestroyed ?
false :
hitbox.intersects(object);
}
public boolean collidesWith(Rectangle object){
if(isDestroyed)
return false;
else
return hitbox.intersects(object);
}

Could you let me now why not call equals() in this HashSet code? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
This is my example code to test HashSet.
I expect that the result is [3K,1K] but the this code results in [1K,3K,3K]
Could you let me know why the code not call equals?
import java.util.HashSet;
class SutdaCard{
private int num;
private boolean isKwang;
SutdaCard(){
this(1,true);
}
SutdaCard(int num, boolean isKwang){
this.num = num;
this.isKwang = isKwang;
}
public String toString(){
return num+(isKwang ? "K":"");
}
public boolean equals(Object obj){
String compareValue = obj.toString();
String thisValue = toString();
System.out.println("equals");
return thisValue.equals(compareValue);
}
public int hashcode(){
return toString().hashCode();
}
}
class exercise11_11 {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashSet<SutdaCard> set = new HashSet<SutdaCard>();
set.add(new SutdaCard(3,true));
set.add(new SutdaCard(3,true));
set.add(new SutdaCard(1,true));
System.out.println(set);
}
}
You haven't overridden hashCode() properly. Try:
#Override
public int hashCode() {
return this.toString().hashCode();
}
Since the Set uses the hashCode() from Object in your code, the two SutdaCards hash codes won't match, and equals() is never called.
If you add the #Override annotation, the compiler will check that you are actually overriding something, and generate a warning if you have a spelling error.

Categories

Resources