equals with strings is producing wrong results [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
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.
Improve this question
i have a simple function inside a class that checks if two strings are equal ( this is for a hangman game). The code is simple, with two class variables:
private String hiddenWord;
private String dashWord;
public void compareGuessWord(String clientGuess) {
if (clientGuess.length() > 1) {
System.out.println("Kollar om dashord: " + dashWord);
System.out.println("Kollar om hidden: " + this.hiddenWord);
System.out.println("Kollar om guess:" + clientGuess);
if (clientGuess.equalsIgnoreCase(this.hiddenWord));
{
this.dashWord = clientGuess;
}
System.out.println("SECOND Kollar om guess:" + clientGuess);
System.out.println("SECOND Kollar om dashord: " + dashWord);
System.out.println("SECOND Kollar om hidden: " + hiddenWord);
}
}
Here is the console output, which u can see that it is running the inner if even though they dont match.
problem

if (clientGuess.equalsIgnoreCase(this.hiddenWord)); Your if does nothing but checking. Remove the ; after it.

In your if condition have ';' so that if is not working
if (clientGuess.equalsIgnoreCase(this.hiddenWord))**;**
{
this.dashWord = clientGuess;
}
The compiler consider it as a normal statement, so remove semicolon ";" and check

your If Statement is getting terminated as you had put semicolon at the end
if (clientGuess.equalsIgnoreCase(this.hiddenWord));
remove the semicolon and it will work fine

Related

Having trouble copying an ArrayList [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 2 years ago.
Improve this question
This might be a bit of a stupid question, but I can't figure this out! I'm working on a minigame that involves one player being randomly selected as the terminator, and all the rest as weaklings. I have an ArrayList of players, and I can get it to choose the terminator, and then I need it to copy players into weaklings, but skip terminator. The code I'm using doesn't copy anything at all into weaklings. Here's the code I'm using, could someone a bit more experienced help me out please:
for (int i = 0; i < players.size(); i++ ) {
if (!players.get(i).contentEquals(terminator)) {
players.add(players.get(i));
}
}
System.out.print("The other players are: " + weaklings + ".");
}
You add to the players again: players.add, instead of weaklings.add.

Why do-while dosen't respond to .equals [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
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.
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.
Improve this question
I'm setting up this do- while loop . Where do i need to correct this code
so when 000000 is given , the loop ends.
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
String am ;
{
do
{ System.out.println("give am number");
am = kb.next();
if (am.matches("[0-9]+") && am.length() <= 6)
{System.out.println("am = "+am);
{break;}}
else
{System.out.println("wrong try again");
am = kb.next();
}
} while(!"000000".equals(am));
Right now, you are checking for equality, not matching. "000000" is not equal to "[000000]", so the loop keeps going.
It's not completely clear what you want, but I think just ditching the square braces is probably the answer.

Expression expected android [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 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 am trying to show my summary result in a textview is there any syntax to use other than return summary because I get an error of unidentified expression, and I dont know of any other syntax.
//construct the summary size out of select sentences
String summary = "";
summary = summary + "• " + firstSentence + System.getProperty("line.separator") + System.getProperty("line.separator");
for(String sentence : sentences)//foreach string sentence in sentences list
{
if(setSummarySentences.contains(sentence))
{
//produce each sentence with a bullet point and good amounts of spacing
summary = summary + "• " + sentence + System.getProperty("line.separator") + System.getProperty("line.separator");
}
}
return summary;
}
}
Your mistake is in the following line:
TextView textView3 = return summary;
You're trying to assign a return statement to a variable, which is not a valid syntax.
Please take a look at the Java tutorials, specially in the assignment and return sections.

Beginning java; compile time error [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 6 years ago.
Improve this question
I am taking an intro to java class, and I have to make a chicken class with a method walk(). I have everything done, but the method walk().
public void walk();
{
System.out.print( Chicken + "is walking." );
}
This is what I'm trying, and I am getting a compile time error
Chicken.java:25: error: missing method body, or declare abstract
public void walk();
Chicken.java:27: error: cannot find symbol
System.out.print( Chicken + "is walking." );
symbol: variable Chicken
location: class Chicken
Any help would be appreciated, and thanks!
That is because of a semi colon after walk(). It denotes the end of statement, all you need to do is to remove it and the code will compile. E.g.
public void walk(){
System.out.print( Chicken + "is walking." );
}

Why am I getting an invalid character constant for the '[ in Java? [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
There is an issue with for the '[ right in front of the data-testing-id and I am not sure why. Any help is appreciated.
for (int i = 1; i < 1001; i = i + 1) {
if(driver.findElement(By.cssSelector('[data-testing-id="data-id1"]'))!= null){
System.out.println("Element is Present");
}else{
System.out.println("Element is Absent");
}
}
Java String(s) can't be done in single quotes. This,
By.cssSelector('[data-testing-id="data-id1"]')
Should be (escaping the double quotes),
By.cssSelector("[data-testing-id=\"data-id1\"]")
or with single quotes inside the double quotes, like
By.cssSelector("[data-testing-id='data-id1']")
You should use
"[data-testing-id=\"data-id1\"]"
for(int i = 1; i < 1001; i++)
i++ is another way to add an increment of 1 to i for each loop... And a wee bit easier than typing (i = i + 1)
I am new to Java and not sure if that was worth your time reading, but I think its pretty cool and might make your code prettier...

Categories