• Post Reply Bookmark Topic Watch Topic
  • New Topic

Questions and Answers problem  RSS feed

 
Alfredo Romero
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone

I am trying to learn java with Head First Java, but I have no one to discuss my code with, because i am only 14. I hope you coul read my code and tell me what is wrong, I would be very grateful . It compiles but the problem is that every time I respond Alfredo it says i am wrong(equivocado) , after i solve this problem I am adding more Questions and answers, and a score based on int x, other problem is that i have no questions, the question is "What is my name?" but I do not know how to put Questions according to the answer.

Test Class
import java.util.*;
public class Test {
public String name;
private int x = 0;
private GameHelper helper = new GameHelper();
public ArrayList<Test> listadepreguntas = new ArrayList<Test>();

public void setName(String n) {
name = n;
}

public void setUp() {

Test answerone = new Test();
answerone.setName("Alfredo");
listadepreguntas.add(answerone);

System.out.println("Escribe la respuesta sin espacio ejemplo: AmericaDelSur");

} // end SetUp();

public void preguntas() {

while(!listadepreguntas.isEmpty()) {
String userGuess = helper.getUserInput("La respuesta es:");
checkUserGuess(userGuess);

} // end while
} // end preguntas()

public String checkYourself(String userInput) {
String result = "equivocado";
int index = listadepreguntas.indexOf(userInput);

if (index == 0 && userInput == "Alfredo") {
result = "correcto";
}

if (index >= 0) {
listadepreguntas.remove(index);
if (listadepreguntas.isEmpty()) {
System.out.println("sacaste " + x + "pregunta de 1 pregunta(s)");

} else {
result = "correcto";
} // end else
} // end if

return result;
} // end checkYourself

public void checkUserGuess(String userGuess) {
String result = "equivocado";

for(Test TestToTest : listadepreguntas) {
result = TestToTest.checkYourself(userGuess);
if (result.equals("correcto")) {
x = x + 1;
} // end if
} // end for
System.out.println(result);
} // end checkUserGuess
} // end class

TestTestDrive Class
import java.util.*;

public class TestTestDrive { // start class


public static void main (String[] args) {
Test t = new Test();
t.setUp();
t.preguntas();

} // end main
} // end class


Game Helper Class
import java.io.*;
import java.util.*;

public class GameHelper {
public String getUserInput(String prompt) {
String inputLine = null;
System.out.print(prompt + " ");
try {
BufferedReader is = new BufferedReader(new InputStreamReader (System.in));
inputLine = is.readLine();
if(inputLine.length() == 0) return null;
} catch (IOException e) {
System.out.println("IOExeption: " + e);
}
return inputLine.toLowerCase();
} // end get user input
} // end class

Sorry for my bad english, as you can see I am not a Native-Speaker
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Don't know, but using == to check equality of objects will usually give the wrong answer.
If you enter "Alfredo" it will be a different "Alfredo" from the one you have already written.

Try using the equals method . . . "Alfredo".equals(userInput) . . .

But there, you are not using the name you entered earlier. I don't have a copy of HFJ, so I don't know how faithfully you are copying the exercise.

But, you entered "Alfredo" into a List somewhere, then you wrote "Alfredo" again; you could get all sorts of problems if you are not called Alfredo, or even worse, if you write "Alfreddo"!
 
Albareto McKenzie
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hola Alfredo,

Please use code tags next time

Test Class


TestTestDrive Class



Game Helper Class


I haven't understood you very well, you said that allways you answer Alfredo the program says that you are "equivocado" (so it doesn't go inside the 'if' sentence in line 35 of class Test), am I right?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have misunderstood line 35, and also need to check about == on objects.

Actually if you enter "Alfredo" into the List and then find userInput at index 0, then that implies that "Alfredo" was entered, and the test for equality is unnecessary.
 
Albareto McKenzie
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right in the second appreciation, I didn't realize that.
 
Alfredo Romero
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed to:

but i am still having the same problem, by the way its not an HFJ exercise but thanks for trying to help
 
Albareto McKenzie
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is de value of index? and the value of userInput? do some System.out.println or debug the aplication to see what are the results and see why is not going inside where you want, and let us know

Suerte!
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try taking out the test about equals("Alfredo") and see whether that improves things.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!