Solution 1 :
You are adding an extra space in front when next button is clicked
. Either remove the space by using trim()
method.
String answer = answerText.getText().toString().trim();
or use setText("")
instead of setText(" ")
.
Problem :
I have a simple app which generates a random String and display it as a TextView. It replaces the letters with their transliterations. If the user types the correct transliteration, it prints “That’s correct” otherwise it prints “Not exactly, it’s: ” + the correct transliteration. However, it works correctly just for the first time. After that, even when the user types the correct transliteration it says: “Not exactly, it’s:”.
By printing the answer, I checked if it detects the user input correctly; It does but it still doesn’t work. I’m not sure which part of my code makes trouble.
I’d appreciate any help.
Here is a simple form of my MainActivity:
package com.example.test;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Random;
//Letter activity
public class MainActivity extends AppCompatActivity {
String wordStr;
TextView resultTextView;
EditText answerText;
String result;
TextView randomText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultTextView = (TextView) findViewById(R.id.result);
answerText = (EditText) findViewById(R.id.answer);
randomText = (TextView) findViewById(R.id.randomText);
findViewById(R.id.newBtn).setOnClickListener(buttonClickListener);
findViewById(R.id.check).setOnClickListener(buttonClickListener);
generateRandomStr();
}
//generates random word
public void generateRandomStr(){
String Chars = "Бд";
StringBuilder word = new StringBuilder();
Random rnd = new Random();
while (word.length() < 3) {
int index = (int) (rnd.nextFloat() * Chars.length());
word.append(Chars.charAt(index));
}
wordStr = word.toString();
randomText.setText(wordStr);
}
private View.OnClickListener buttonClickListener = new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View view) {
switch (view.getId()) {
//Check button
case R.id.check:
String answer = answerText.getText().toString();
String transliteration = wordStr.replace("Б", "b").replace("д", "d");
if (answer.equals(transliteration)) {
result = "That's correct";
} else {
result = "Not exactly, it's actually:" + " " + transliteration;
}
resultTextView.setText(result + " ");
break;
//New button
case R.id.newBtn:
resultTextView.setText(" ");
answerText.setText(" ");
generateRandomStr();
break;
}
}
};
}
And here is activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns_android="http://schemas.android.com/apk/res/android"
xmlns_app="http://schemas.android.com/apk/res-auto"
xmlns_tools="http://schemas.android.com/tools"
android_layout_width="match_parent"
android_layout_height="match_parent"
tools_context=".MainActivity">
<Button
android_id="@+id/newBtn"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="New"
android_textSize="18sp"
app_layout_constraintBottom_toBottomOf="parent"
app_layout_constraintEnd_toEndOf="parent"
app_layout_constraintHorizontal_bias="0.682"
app_layout_constraintStart_toStartOf="parent"
app_layout_constraintTop_toTopOf="parent"
app_layout_constraintVertical_bias="0.528" />
<Button
android_id="@+id/check"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Check"
android_textSize="18sp"
app_layout_constraintBottom_toBottomOf="parent"
app_layout_constraintEnd_toEndOf="parent"
app_layout_constraintHorizontal_bias="0.314"
app_layout_constraintStart_toStartOf="parent"
app_layout_constraintTop_toTopOf="parent"
app_layout_constraintVertical_bias="0.528" />
<TextView
android_id="@+id/randomText"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="TextView"
android_textSize="36sp"
app_layout_constraintBottom_toBottomOf="parent"
app_layout_constraintEnd_toEndOf="parent"
app_layout_constraintHorizontal_bias="0.498"
app_layout_constraintStart_toStartOf="parent"
app_layout_constraintTop_toTopOf="parent"
app_layout_constraintVertical_bias="0.136" />
<EditText
android_id="@+id/answer"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_ems="10"
android_inputType="textPersonName"
app_layout_constraintBottom_toBottomOf="parent"
app_layout_constraintEnd_toEndOf="parent"
app_layout_constraintHorizontal_bias="0.497"
app_layout_constraintStart_toStartOf="parent"
app_layout_constraintTop_toTopOf="parent"
app_layout_constraintVertical_bias="0.371" />
<TextView
android_id="@+id/result"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Result"
android_textSize="24sp"
app_layout_constraintBottom_toBottomOf="parent"
app_layout_constraintEnd_toEndOf="parent"
app_layout_constraintStart_toStartOf="parent"
app_layout_constraintTop_toTopOf="parent"
app_layout_constraintVertical_bias="0.726" />
</androidx.constraintlayout.widget.ConstraintLayout>
Thanks in advance
Comments
Comment posted by John Joe
did you try print out
Comment posted by Sam
Yes, I printed the
Comment posted by GhostCat
Then the two strings aren’t equal. Just “printing” looks identical doesnt make them identical. Some characters arent printed for example. You could, for example print out each string char by char in case of a “no match”. Rest assured: “if” works as it should, and “equals” does as it should. If your code prints that second message, then your strings are NOT identical. For whatever reason.
Comment posted by MC Emperor
Are you sure