0

I have a textview in my app that contains numbers and letters and what I want to do is only get the numbers from it and apply a if statement. Something along the lines of;

    if (number == 1) {

        // do something

    } else if (number == 2) {

        // do something

    } else if (number >= 8) {

        // do something

    }

I have declared my textview like this;

TextView textview = (TextView) findViewById(R.id.number);

and the textview will contain texts similar to theses

textview.setText("1\nperson");
textview.setText("2\nperson");
textview.setText("3\nperson");
....
....
....

What I want to do is only get the text and then use the if statement shown below to carry out a task depending on the number. Any help would be nice.

Henry
  • 1,042
  • 2
  • 17
  • 47
  • "if (number = 1)" - this won't compile – Egor Jan 22 '16 at 16:01
  • so, what is your questions ? to be more specific ? what is the problem you are facing ? – Chim Ly Jan 22 '16 at 16:05
  • sorry i had to re-write this and forgot to writet two ==. My question is how do I extract int from the strings. for example, if the text is 1 person in the format 1\nperson (\n = new line) how do I only get the number 1 from this string and then use the if statement above. – Henry Jan 22 '16 at 19:59

4 Answers4

1

You should split the string from the textview and convert numbers part to int and handle with try/catch if there's input error

TextView textview = (TextView) findViewById(R.id.number);
String text = textview.getText();
String[] parts = text.split("\\");
int number = Integer.parseInt(parts[0]);
if (number == 1) {
    // do something
} else if (number == 2) {
    // do something
} else if (number > 8) {
    // do something
}

+ You must use == not = It seems like you're new to java i think you should check java operators !

Ziad Alzarka
  • 333
  • 1
  • 2
  • 12
  • Thanks, i will try this now. and yea your right it won't run with one = :) silly mistake – Henry Jan 22 '16 at 20:00
  • Hi, when I write this code, the if statements do not run. Do I by any chance need to add listeners? Basically the if statement should be checked when the app opens. For example, one task could be if (number == 2) {Snackbar.make(view, "number 2" , Snackbar.LENGTH_LONG).setAction("Action", null).show();} – Henry Jan 22 '16 at 20:19
  • you should put the code in onCreate void in the main activity to run when the app starts but how can you get the value if the TextView was empty o.O – Ziad Alzarka Jan 24 '16 at 11:00
1

You can try this:

String text = textView1.getText().toString();

int number = Integer.parseInt(text.substring(0, 1));

if(number == 1){
    ....
} else if(number == 2){
    ....
}
....
hongbochen
  • 123
  • 6
  • Hi, when I write this code, the if statements do not run. Do I by any chance need to add listeners? Basically the if statement should be checked when the app opens. For example, one task could be if (number == 2) {Snackbar.make(view, "number 2" , Snackbar.LENGTH_LONG).setAction("Action", null).show();} – Henry Jan 22 '16 at 20:20
1

You could extract a number from string by using a regex like this.

//This pattern will find all number groups in a string
Pattern p = Pattern.compile("(\\d+)"); 
Matcher m = p.matcher("3\nperson"); //The string you should get from your TextView
int number = 0;
if(m.find()) { //If a number in the string is found
    number = Integer.parseInt(m.group()); //Sets number to first found number group
}

Now you can use variable number in your if statement. I hope this helped you.

EgorDm
  • 26
  • 1
  • 5
1

Since you are wanting to allow alphanumeric characters I would go with the following:

    String value = textView.getText().toString();
    value = value.trim().replaceAll("[^0-9]","|").replaceAll("(\\D)\\1+","$1");
    String values[] = value.trim().split("\\|");
    int intValues[] = new int[values.length];
    for(int i = 0; i < values.length; i ++){
        intValues[i] = Integer.parseInt(values[i]);
    }
    //assuming you only care about the first number
    int number = intValues[0];
    if(number == 1){
        //do something
    }else if(number == 2){
        //do something else
    }// etc.

The first line simply gets the raw text in your input. The second line has two parts to it, the first thing it does is substitute any non digit(0-9) characters sequences with a pipe "|". After this is done we go back and trim away any sequences of pipe with a single pipe.

Imaging this is your input: 1w22ee333rrr

After the first pass we leave it as: 1|22||333|||

After the second pass we leave it as 1|22|333|

We then split and store the values into a String array, loop over that array parsing all the numbers and storing them into an integer array. This will give you access to all the numbers being input, a little overkill but I had the time on my hands so I just gave you this as an option in case you find another application for it.

aemorales1
  • 312
  • 3
  • 13
  • Hi, when I write this code, the if statements do not run. Do I by any chance need to add listeners? Basically the if statement should be checked when the app opens. For example, one task could be if (number == 2) {Snackbar.make(view, "number 2" , Snackbar.LENGTH_LONG).setAction("Action", null).show();} – Henry Jan 22 '16 at 20:19
  • I have an onFocusChangeListener so that it will fire as soon as you leave the input field. – aemorales1 Jan 22 '16 at 20:57
  • glad you were able to get it running – aemorales1 Jan 22 '16 at 22:07
  • 1
    I have 1 more question, please can you have a look incase you might be able to provide and help: http://stackoverflow.com/questions/34956796/making-first-word-character-of-string-in-textview-bigger – Henry Jan 22 '16 at 22:11
  • Looks like you got your answer :) – aemorales1 Jan 23 '16 at 04:53
  • yep :) btw, do you know how I can sort out my listview based on the numbers. For example, I want to recylerview to be sorted out in number order from smallest to biggest and if there are two same numbers e.g. 1 and 1 then is it possible to sort them using the person's name (in my recylerview I will be data like this 1 John Smith | 2 Adam Hanly | 1 Breti Tanson) – Henry Jan 23 '16 at 14:08
  • There are several ways to do this, I would say it depends on where your data is coming from. – aemorales1 Jan 23 '16 at 18:18