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.
Related
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.
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.
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());
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
I write the code
public class keys {
private static final String[] keyss = {
"HGDLV-KLLQC-DM3ON-MAUOX-FNQK5",
"HQKLD-NYRDL-NJXCV-BVCKF-NRKLC",
"LDFCV-BDJCQ-VMFQV-FNRKC-NFCFL"
//and many more
};
public static String getKey(String key) {
boolean b = Arrays.asList(keyss).contains(key);
if (b == true) {
key = "approved";
} else {
key = "unapproved";
}
return key;
}
and gets the approved for all Strings even if the above dosen't match with the user specified string
Compare Strings using equals. Also the implementation of getKeys() is too complicated and redundant.. Consider changing it to:
public class Keys {
private static final String[] keys = {
"HGDLV-KLLQC-DM3ON-MAUOX-FNQK5",
"HQKLD-NYRDL-NJXCV-BVCKF-NRKLC",
"LDFCV-BDJCQ-VMFQV-FNRKC-NFCFL"
//and many more
};
public static String getKey(String key) {
for(String current : keys){
if(current.equals(key)){
return "approved";
}
}
return "unapproved";
}
}
No need to use Arrays in the way you use it, nor a boolean switch..
You might want to check out this question to read about comparing Strings.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
Why don't I get 20 after delivering e.g. "Obsidian" to Test(String pStr) if I call getMatInt() ?
Also tried .toString() after all String-declariations, also declarated e.g. "Obsidian" as new String a. Nothing works.
getBonus is aways returning a 0 instead of a 20/30/... .
I already tried "Obsidian" and "obsidian", both doesnt work for me ...
public class test
{
private String str;
private int matInt;
private int bonus;
private int magic;
public test(int pMagic, String pStr)
{
int magic = pMagic;
str = pStr;
}
private void materialEquals()
{
if(str.equals("Obsidian"))
{
matInt = 20;
}
.....
}
private void calcBonus()
{
materialEquals();
bonus = magic * matInt;
}
public int getBonus()
{
calcBonus();
return bonus;
}
}
try:
public int getMatInt()
{
materialEquals();
return matInt;
}
There is no reason for this in your constructor: str = new String(pStr);, just use str = pStr.
In fact, you might be better off setting matInt in your constructor:
public test(String pStr)
{
str = pStr;
materialEquals();
}
And depending on how many materials you have, you might want to look into using enumeration.
Ok after your edits:
public int getBonus()
{
calcBonus(); //bonus won't be calculated otherwise
return bonus;
}
After further edits:
Your constructor is wrong, you're not initializing int magic. Try this constructor instead.
public test(int pMagic, String pStr)
{
this.magic = pMagic; //int magic = pMagic was a new variable only in the constructor scope
this.str = pStr;
calcBonus();
}
Also you might as well calculate the bonus on construction.
You will need to call materialEquals() so that 20 can be assigned to matInt upon equals comparison, as integers are always initialized to default value 0 upon declaration.