1
I have the following code snippet:
import java.util.Random;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import java.io.*;
import java.lang.String;
public class findthewordgame {
static Random random = new Random();
public static String arrayWord () {
String[] wordgame = { //array
"TEA",
"COFFEE",
"BOAT",
"SEA",
"SUN"
};
int idx = random.nextInt(wordgame.length);
String wordChosen = (wordgame[idx]);
//System.out.println(wordChosen); //prints the random word
//System.out.println(scramble(wordChosen));
return (scramble(wordChosen));
}
public static String scramble(String inputString )
{
// Convert string into a simple char array:
char[] a = inputString.toCharArray();
// Scramble the letters using the standard Fisher-Yates shuffle,
for( int i=0 ; i<a.length ; i++ )
{
int j = random.nextInt(a.length);
//shuffle the characters
char temp = a[i];
a[i] = a[j];
a[j] = temp;
}
return new String(a);
}
public static String input() {
Scanner reader = new Scanner(System.in);
System.out.println("Enter the correct answer: ");
String n = reader.next();
return n;
}
public static void verifyWord(String wordChosen, Scanner reader){
boolean answeredCorrectly = false;
int tries = 1;
//String wordChosen = random.nextInt();
String answer = wordChosen;
while (tries>0 && answeredCorrectly == false) {
answer = reader.next();
if (wordChosen == answer) {
System.out.println("You got it right");
answeredCorrectly = true;
}
else if (answer != wordChosen) {
System.out.println("Wrong");
}
}
}
I do not understand when I try to call you on Main is giving error(see photo).
From what I understand
verifyWord(String wordChosen, Scanner reader)
has two input parameters and you are calling it without any; that’s not the point?– rLinhares
You have created a method with two arguments, but you are not passing any parameters to it
– Jefferson Quesado
I’ve tried that too and gives equal error. what solution suggest?
– Diana Madeira
In time: your comparison will not return the expected result. The equal operator
==
checks if the reference of the objects is the same, not the content. Therefore, no matter what is typed, this operation will return false– Jefferson Quesado
Diana, the point of the function call has already been answered by Wictorchaves and Sorack. Doubts on other points (such as why even typing the right word the program does not detect) shall be published in another
– Jefferson Quesado
@Jeffersonquesado no
Java
it makes no difference to use==
orequals
.– Sorack
Is this question by any chance a duplicate of this? https://answall.com/questions/240649/problems-createclass
– user28595
@Sorack does yes.
new Integer(1) == new Integer(1)
. This expression returns false– Jefferson Quesado
@Jeffersonquesado was referring to
String
in the case...– Sorack
@Sorack, isn’t that dependent on some string cache optimization? If the behavior is not as I hope, I will open a new question here at Sopt about this
– Jefferson Quesado
@Jeffersonquesado take a look in this answer of the SO... will be the same, but is not very reliable.
– Sorack