if I have set text in textview in such way, which is not problem:
tv.setText("" + ANS[i]);
this simply getting from this way.
String a = tv.getText().toString();
int A = Integer.parseInt(a);
But in case of setting value in textView.
tv1.setText(" " + X[i] + "\n" + "+" + " " + Y[i]);
which is like this
5
+9
I have problem , this value how to get.
If it is the sum of all the numbers you want, I made a little snippet for you that can handle both + and - using a regex (I left some print-calls in there to help visualise what happens):
final String string = " " + 5 + "\n" + "-" + " " + 9+"\n"+"+"+" "+5; //Or get the value from a TextView
final Pattern pattern = Pattern.compile("(-?).?(\\d+)");
Matcher matcher = pattern.matcher(string);
System.out.print(string);
System.out.print('\n');
int sum = 0;
while( matcher.find() ){
System.out.print(matcher.group(1));
System.out.print(matcher.group(2));
System.out.print('\n');
sum += Integer.parseInt(matcher.group(1)+matcher.group(2));
}
System.out.print("\nSum: "+sum);
This code prints the following:
5
- 9
+ 5
5
-9
5
Sum: 1
Edit: sorry if I misunderstood your question, it was a little bit unclear what you wanted to do. I assumed you wanted to get the sum of the numbers as an integer rather than a string.
Edit2: to get the numbers separate from each other, do something like this instead:
final String string = " " + 5 + "\n" + "-" + " " + 9+"\n"+"+"+" "+5; //Or get the value from a TextView
final Pattern pattern = Pattern.compile("(-?).?(\\d+)");
Matcher matcher = pattern.matcher(string);
ArrayList<Integer> numbers = new ArrayList<Integer>();
while( matcher.find() ){
numbers.add(Integer.parseInt(matcher.group(1)+matcher.group(2)));
}
You have to do the following:
a=a.replace("\n"," ");
a=a.trim();
String b[]=a.split("+");
int k=Integer.ValueOf(b[0]);
int l=Integer.ValueOf(b[1]);
int sum=k+l;
split with the + sign like this way
String a = tv.getText().toString();
String aa[];
if(a.contains("+"))
aa = a.split("+");
now convert the array
Integer.parseInt(aa[0]); // and so on
Try Like this.
tv1.setText(" " + Integer.toString(X[i]) + "\n" + "+" + " " + Integer.toString(Y[i]));
Source: Stackoverflow.com