I'm currently creating a mobile application containing flag guessing games using Android Studio 3.2. For one of the games I have to display a random flag and the corresponding name (covered with dashes). The user can input a letter into the edit text box underneath and click the submit button. If the user gets the answer right then dashes with that letter are removed to show the actual letter.
The problems for me start with replacing each dash individually. When I enter a letter and submit it, all the dashes turn into that same letter.
package com.example.anisa.assignment1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Random;
public class GuessHints extends AppCompatActivity
{
private ImageView flag;
private int randIndex;
public char[] answers = {};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guess_hints);
displayHintsFlag();
splitCountryNameLetters();
}
public void displayHintsFlag()
{
flag = findViewById(R.id.displayHintsFlag);
Random r = new Random();
int min = 0;
int max = 255;
randIndex = r.nextInt(max-min) + min;
Country countries = new Country();
int randomHintsFlagImage = countries.countryImages[randIndex];
flag.setImageResource(randomHintsFlagImage);
}
public void splitCountryNameLetters()
{
Country countries = new Country();
String randomHintsFlagName = countries.countryNames[randIndex];
TextView hintsQuestion = (TextView) findViewById(R.id.countryDashesDisplay);
String hintsQuestionString;
int flagNameLength = randomHintsFlagName.length();
char letter;
for (int i = 0; i < flagNameLength; i++)
{
Log.d("Flag: ",randomHintsFlagName + "");
hintsQuestionString = hintsQuestion.getText().toString();
letter = '-';
hintsQuestion.setText(hintsQuestionString + " " + letter);
}
Log.d("Answers: ", answers + "");
}
public void checkUserEntries(View view)
{
Country countries = new Country();
String randomHintsFlagName = countries.countryNames[randIndex];
int flagNameLength = randomHintsFlagName.length();
TextView hintsQuestion = (TextView) findViewById(R.id.countryDashesDisplay);
String hintsQuestionString = hintsQuestion.getText().toString();
EditText userEntry = (EditText) findViewById(R.id.enterLetters);
String userEntryText = userEntry.getText().toString();
//int numCorr = 0;
char letterChar = userEntryText.charAt(0);
for(int k = 0; k < flagNameLength; k++)
{
if((letterChar == randomHintsFlagName.charAt(k)))
{
//numCorr++;
hintsQuestionString = hintsQuestionString.replace(hintsQuestionString.charAt(k), letterChar);
}
}
hintsQuestion.setText(hintsQuestionString);
}
public void nextGuessHints(View view)
{
Button submitButtonHints = (Button) findViewById(R.id.submitLetterButton);
submitButtonHints.setText("Next");
Intent intent = getIntent();
finish();
startActivity(intent);
}
@Override
public void onBackPressed()
{
super.onBackPressed();
startActivity(new Intent(GuessHints.this, MainActivity.class));
finish();
}
}
I know the issue is with the use of k index, but not sure how to solve this problem as it is in a for loop.