Android Build an Interactive Story App Finishing the User Interface Formatting Strings

Caleb Seeling
Caleb Seeling
1,189 Points

I tried to make an app

I tried to make an app where you guess the name of the Letter in Hebrew.But I can only press the button one time, after that it won't do anything anymore. But also I find it hard to put in the same randomNumber into both methods so that if the user presses the answer button the German answer comes.

the code is:

AlefBet.java
package com.teamtreehouse.hebrewlearning.model;

import java.util.Random;

public class AlefBet {

    public static String[] AlefBets = {

            "א",
            "ב",
            "ג",
            "ד",
            "ה",
            "ו",
            "ז",
            "ח",
            "ט",
            "י",
            "כ",
            "ל",
            "מ",
            "נ",
            "ס",
            "ע",
            "פ",
            "צ",
            "ק",
            "ר",
            "ש",
            "ת"

    };

    public static Random randomGenerator = new Random();
    public static int randomNumber = randomGenerator.nextInt( AlefBets.length);

    public static String getAlefBets() {

        return AlefBets[randomNumber];
    }

    public static int getrandomNumber() {
        return randomNumber;
    }
}
alephBet.java
package com.teamtreehouse.hebrewlearning.model;

public class AlephBet {
    public int randomNumber;

    public static String[] AlefBets = {
            "Alef",
            "Bet",
            "Gimel",
            "Dalet",
            "Hey",
            "Vav",
            "zain",
            "Chet",
            "Tet",
            "Yod",
            "Khaf",
            "Lamed",
            "Mem",
            "Nun",
            "Samech",
            "ayin",
            "pey",
            "tzadik",
            "kuf",
            "resh",
            "shin",
            "taf"
    };

    public static String getAlephBets() {
        int randomNumber = AlefBet.getrandomNumber();
        return AlefBets[randomNumber];

    }
}
alefBetActivity.java
package com.teamtreehouse.hebrewlearning.ui;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.teamtreehouse.hebrewlearning.R;
import com.teamtreehouse.hebrewlearning.model.AlefBet;
import com.teamtreehouse.hebrewlearning.model.AlephBet;

public class alefBetActivity extends AppCompatActivity {

    private TextView randomAlefText;
    private Button randomAlefBetAnswerButton;
    private Button randomAlefBetButton;
    private TextView randomAlefBetAnswer;
    public AlefBet getLetter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alef_bet);

        final TextView randomAlefText = (TextView) findViewById(R.id.randomAlefText);
        Button randomAlefBetAnswerButton = (Button) findViewById(R.id.randomAlefBetAnswerButton);
        Button randomAlefBetButton = (Button) findViewById(R.id.randomAlefBetButton);
        final TextView randomAlefBetAnswer = (TextView) findViewById(R.id.randomALefBetAnswer);

        randomAlefBetAnswer.setVisibility(View.INVISIBLE);
        randomAlefBetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String letter = AlefBet.getAlefBets();
                randomAlefText.setText(letter);
                randomAlefBetAnswer.setVisibility(View.VISIBLE);
            }
        });

        randomAlefBetAnswerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String germanLetter = AlephBet.getAlephBets();
                randomAlefBetAnswer.setText(germanLetter);
                randomAlefBetAnswer.setVisibility(View.VISIBLE);
            }
        });
    }
} 

1 Answer

Ben Deitch
STAFF
Ben Deitch
Treehouse Teacher

When you call 'getRandomNumber' you're returning the same number every time. Instead you want to be generating a new random number for each method call:

    public static Random randomGenerator = new Random();
    public static int getrandomNumber() {
        return randomGenerator.nextInt( AlefBets.length);
    }

Hope that helps!

Caleb Seeling
Caleb Seeling
1,189 Points

Dear Ben, thanks for your help. I am trying to get a random letter out of the AlefBet but want to get returned the same letter(in German)when you press the answer button.