0

I'm trying to get the int value from a textView, the value inside will always be a number just in string, I set it like this

int randomNumber = rng.nextInt(6) + 1;
    switch (randomNumber) {
        case 1:
            imageViewDice.setImageResource(R.drawable.dice1);
            textView.setText("Tiraste un 1!");

            if (player1.getText().equals("")) {
                player1.setText("1");
            } else {
                player2.setText("1");
            }
            break;
//rest of cases 2, 3, 4, 5, & 6

I need the int value of this to be able to compare then to see which player (1 or 2) has the highest number, something like this

//this doesnt work 
int value1 = Integer.parseInt(player1.getText().toString());
int value2 = Integer.parseInt(player2.getText().toString());

if (value1 > value2) {
    result.setText("Jugador 1");
} else if (value1 < value2) {
    result.setText("Jugador 2");
} else {
    result.setText("Empate!");
}

What is the correct way to get the value so that the comparison can be made?

Nancy
  • 1,021
  • 4
  • 23
  • 45
  • i think your code is most case the two player TextView have same value because you set initial value for player one same to player two in your switch statement and in your code compare between values will print empty – Mahmoud Abu Alheja Dec 13 '19 at 20:44
  • `if (player1.getText() == "")` -> https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – a_local_nobody Dec 13 '19 at 20:47
  • @MahmoudAbuElheja what it's doing it checking if player one has already been filled if not then it fills player one and then it fills player two, what I'm trying to do is take `"1"` and turn it in a int `1`, so that the comparion between those values will work since they need to be an int to check if one is greater than the other one – Nancy Dec 13 '19 at 20:47
  • @a_local_nobody yes I did change that in my code a little while ago, I'll edit the code in the question – Nancy Dec 13 '19 at 20:48
  • @Nancy can add your full code to show the logic between textView – Mahmoud Abu Alheja Dec 13 '19 at 20:53
  • @a_local_nobody I'm displaying what each player got in a textView according to the randomNumber, how would I use the randomNumber like you're suggesting? – Nancy Dec 13 '19 at 20:54
  • Before parsing string value to integer, please check / debug if your string value can be a valid integer. Non-empty, or with non-numeric alpha character etc. – Farruh Habibullaev Dec 13 '19 at 20:58

1 Answers1

1

if you set static, you can very easily read in various classes:

player1.setText(Fraction.getP1()+"");
player2.setText(Fraction.getP2()+"");

if(Fraction.getP1()>Fraction.getP2())
   result.setText("Jugador 1");

new class

public class Fraction {

    private static int p1=0;
    private static int p2=0;

    public static int getP1(){
        return p1;
    }
    public static int getP2(){
        return p2;
    }
    public static void setP1(int p1fraction){
        Fraction.p1 = p1fraction; //Fraction is your class name
    }
    public static void setP2(int p2fraction){
        p2 = p2fraction;
    }

}
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Ruyut
  • 151
  • 11