Search code examples
androidstringgettextreadlinesettext

Button text and string from text file (from asset) should be the same, but not in Android


I would like to make a quiz program. The questions are in a text file in asset folder. The answers are also in the asset folder called the number of the question (for example: the first question answers are in the text file called 1). I would like to give the questions and answers randomly (answers to a button). Until this everything is all right (maybe not the shortest solution, but works well). Then the user can answer the question clicking the correct button. And here is the problem. I get the text of the button and the first row of the answer file (always the first row is the right answer in the answer text file). It should be the same, and then I sign, this is the correct answer. But it's not the same, and I don't know why. A put text to the button from the answer file and get the first row from the answer file, so it should be the same. I print it out to log cat, and look like they are the same. I don't know what could be went wrong. Can anybody help me out.

This is where I set the text of the button (randomly) and compare the first rows and the text of the button:

BufferedReader br2 = new BufferedReader(is2, 8192);

                        for(int k2=0; k2<3; k2++){
                            try {
                                kerdes2[k2] =  br2.readLine();
                                final ArrayList <Integer> kerdesno2 = new ArrayList<Integer>();
                                for(int j=0;j<3;j++) kerdesno2.add(j);
                                Collections.shuffle(kerdesno2);
                                System.out.println(kerdesno2);
                                answ.setText(kerdes2[kerdesno2.get(0)]);
                                answ2.setText(kerdes2[kerdesno2.get(1)]);
                                answ3.setText(kerdes2[kerdesno2.get(2)]);
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                            answ.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    // TODO Auto-generated method stub
                                    InputStreamReader is3 = null;
                                    try {
                                        is3 = new InputStreamReader(am.open(i3), "ISO-8859-1");
                                    } catch (UnsupportedEncodingException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                    BufferedReader br3 = new BufferedReader(is3, 8192);
                                    try {
                                        String helyes = br3.readLine();
                                        System.out.println(helyes);
                                        String gomb = answ.getText().toString();
                                        System.out.println(gomb);

                                        for(int f=0; f<helyes.length(); f++)
                                        {
                                            char c = helyes.charAt(f);
                                            char d = gomb.charAt(f);
                                            if(c != d){
                                                System.out.println(c);
                                                System.out.println(((String) gomb).indexOf(c));
                                            }
                                        }

                                        if(gomb == helyes)                                  
                                        {
                                            x++;
                                            TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
                                            eredmeny.setText("Eredményed: " + Math.round(x*100/i2) + "%");
                                        }
                                        else
                                        {
                                            TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
                                            eredmeny.setText(gomb + " = " + helyes);
                                        }
                                    } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }
                            });

                            answ2.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    // TODO Auto-generated method stub
                                    InputStreamReader is3 = null;
                                    try {
                                        is3 = new InputStreamReader(am.open(i3), "ISO-8859-1");
                                    } catch (UnsupportedEncodingException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                    BufferedReader br3 = new BufferedReader(is3, 8192);
                                    try {
                                        String helyes = br3.readLine();
                                        System.out.println(helyes);
                                        String gomb = answ2.getText().toString();
                                        System.out.println(gomb);
                                        if(gomb == helyes)                                          
                                        {
                                            x++;
                                            TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
                                            eredmeny.setText("Eredményed: " + Math.round(x*100/i2) + "%");
                                        }
                                        else
                                        {
                                            TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
                                            eredmeny.setText(gomb + " = " + helyes);
                                        }
                                    } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }
                            });

                            answ3.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    // TODO Auto-generated method stub
                                    InputStreamReader is3 = null;
                                    try {
                                        is3 = new InputStreamReader(am.open(i3), "ISO-8859-1");
                                    } catch (UnsupportedEncodingException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                    BufferedReader br3 = new BufferedReader(is3, 8192);
                                    try {
                                        String gomb = answ3.getText().toString();
                                        String helyes = br3.readLine();
                                        System.out.println(gomb);
                                        System.out.println(helyes);
                                        if(gomb == helyes){
                                            x++;
                                            TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
                                            eredmeny.setText("Eredményed: " + Math.round(x*100/i2) + "%");
                                        }
                                        else
                                        {
                                            TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
                                            eredmeny.setText(gomb + " = " + helyes);
                                        }
                                    } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }
                            });

As You can see I try to iterate over the two strings to realize where the problem is, but I couldn't manage to find...


Solution

  • String is an object. When comparing objects, use .equals(), not ==.

    Your code:

    if(gomb == helyes)
    

    Should be:

    if(gomb.equals(helyes))
    

    By using == you're comparing memory, not the actual String objects. Sometimes you'll get the expected result, but other times you won't. .equals() will always test the Strings themselves.