Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – If statement just works correctly once

Posted on November 14, 2022

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

READ  [ANSWERED] android - How to call a layout inside the linear layout after clicking the buttons?
Powered by Inline Related Posts

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

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress