简体   繁体   中英

Can't change Char

When I'm trying to change char in my word when I click on for example button A. I want a set char on A and change in my invisible word from (--) set A there where is his position. I'm generating code in NetBeans.

if(hadanka.contains(pismeno))
{
    for(int i=0;i!=hadanka.length();i++)
    if(pismeno.equals(Character.toString(hadanka.charAt(i))))
    {
        prepsani[i]=hadanka.charAt(i);

    }

    System.out.println(prepsani);;
}

Everything is ok. Hadanka is a guessing word and pismeno is the letter that I chose. When I use it in my application for Android errors occur.

Here is my Android code:

char zvolenePismeno;
public void OnStart (View v)

{
    if(odpoved.contains(zvolenePismeno))
    {
        for(int i=0;i!=odpoved.length();i++)
        if(zvolenePismeno.equals(Character.toString(odpoved.charAt(i))))
        {
            prepsani[i]=odpoved.charAt(i);

        }
        labOdpoved.setText(String.valueOf(prepsani));
    }

}

public void OnA(View v)
{
    zvolenePismeno = 'A';
    kliknuti(btn_A);
}

The errors I get is the following:

the method contains(charsequence) in the type string is not applicable for the arguments (char)

at the line if(odpoved.contains(zvolenePismeno)) and

cannot invoke equals (String) on the primitive type char

at the line if(zvolenePismeno.equals(Character.toString(odpoved.charAt(i)))) .

UPDATE 1

it is doesn't work :/ Im change my code on

 if(odpoved!=null)
         {
               for(int i=0;i!=odpoved.length();i++)
                    if(zvolenePismeno==odpoved.charAt(i))
                      {
                           prepsani[i]=odpoved.charAt(i);

                      } 
               labOdpoved.setText(String.valueOf(prepsani));
         }

Here is my whole code (with error )¨

public class MainActivity extends Activity implements OnClickListener {
    int lvl1;
    String[] Level1 = {"APPLE", "SAMSUNG", "NOKIA", "HTC"};
    String odpoved, pokus;
    char zvolenePismeno;

    public void OnStart(View v) {
        btnStart = (Button) findViewById(R.id.btnStart);
        labOdpoved = (TextView) findViewById(R.id.labOdpoved);
        Random rand = new Random();
        lvl1 = Math.abs(rand.nextInt() % 3);
        odpoved = Level1[lvl1];

        char[] prepsani = new char[odpoved.length()];
        for (int i = 0; i != odpoved.length(); i++) {
            prepsani[i] = '-';
        }
        labOdpoved.setText(String.valueOf(prepsani));
        if (odpoved.contains(zvolenePismeno)) {
            for (int i = 0; i != odpoved.length(); i++)
                if (zvolenePismeno.equals(Character.toString(odpoved.charAt(i)))) {
                    prepsani[i] = odpoved.charAt(i);
                }
            labOdpoved.setText(String.valueOf(prepsani));
        }
    }

    public void kliknuti(Button btn) {
        btn.setClickable(false);
        btn.setVisibility(View.INVISIBLE);
    }

    public void OnA(View v) {
        zvolenePismeno = 'A';
        btn_A = (Button) findViewById(R.id.btn_A);
        kliknuti(btn_A);
    }
}

char != Char
Class Char has method equals but not primitive type char .

if (odpoved!=null) 
 for(int i=0;i!=odpoved.length();i++) 
   if (zvolenePismeno==odpoved.charAt(i))

Should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM