skip to Main Content

basically its a wordle type game where we take input and if the first letter of the input is the same as the first letter of our word "sc" it should make the textView background color to green or yellow if not in the right position

  String str=editText.getText().toString();
        String first=str.substring(0,1);
        String sec=str.substring(1,2);
        String third=str.substring(2,3);
        String fourth=str.substring(3,4);

        if(count==0)
        {
            textView1.setText(first);
            textView2.setText(sec);
            textView3.setText(third);
            textView4.setText(fourth);
            if(first==sc.substring(0,1))
            {
                textView1.setBackgroundColor(Color.GREEN);
            }
            else if(first==sc.substring(1,2)|| first==sc.substring(2,3) || first==sc.substring(3,4))
            {
                textView1.setBackgroundColor(Color.YELLOW);
            }

i have tried this but it doesn’t seem to be working it does no change

i am new to android studio code so please excuse if this has a very easy solution and i have also tried searching for similar questions online but none of them seemed to be working

2

Answers


  1. In your code, you try for execute your if statement inside if(count==0). I assume you try execute your code if wordle is not fill yet since you dont explain clearly about count.

    Based on your question here my answer, first you need initialize input and sc value

    String sc = "WORD" *just example value*
    String str=editText.getText().toString();
    String first=str.substring(0,1);
    String sec=str.substring(1,2);
    String third=str.substring(2,3);
    String fourth=str.substring(3,4);
    

    Second, use if statement for check first letter is already filled

    if(!first.equals(""))
    

    Inside first if, create if statement for compare first letter with sc value

    textView1.setText(first);
    if(first==sc.substring(0,1))
        {
           textView1.setBackgroundColor(Color.GREEN);
        }
    else 
        {
           textView1.setBackgroundColor(Color.YELLOW);
        }
    

    This simple way for reach your goal

    Login or Signup to reply.
  2. == use when we have to compare integers. But you are using == to compare strings. Which is wrong. For comparing Strings you have to use string.matches() or string.equals().

    Make changes in your code as below

    if(count==0)
            {
                textView1.setText(first);
                textView2.setText(sec);
                textView3.setText(third);
                textView4.setText(fourth);
                if(first.matches(sc.substring(0,1))) // if "sc.substring(0,1)" throwing any error then parse it into "String.valueOf(sc.substring(0,1));"
                {
                    textView1.setBackgroundColor(Color.GREEN);
                }
                else if(first.matches(sc.substring(1,2))|| first.matches(sc.substring(2,3)) || first.matches(sc.substring(3,4)))
                {
                    textView1.setBackgroundColor(Color.YELLOW);
                }
    

    Let me know if the problem has not been solved yet.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search