if I have set text in textview in such way, which is not problem:
tv.setText(\"\" + ANS[i]);
this simply getting from this way.
I haven't tested this - but it should give you a general idea of the direction you need to take.
For this to work, I'm going to assume a few things about the text of the TextView
:
TextView
consists of lines delimited with "\n"
.TextView
which will all include one operator and one number.Char
of a line.First we get the text:
String input = tv1.getText().toString();
Then we split it up for each line:
String[] lines = input.split( "\n" );
Now we need to calculate the total value:
int total = Integer.parseInt( lines[0].trim() ); //We know this is a number.
for( int i = 1; i < lines.length(); i++ ) {
total = calculate( lines[i].trim(), total );
}
The method calculate should look like this, assuming that we know the first Char
of a line is the operator:
private int calculate( String input, int total ) {
switch( input.charAt( 0 ) )
case '+':
return total + Integer.parseInt( input.substring( 1, input.length() );
case '-':
return total - Integer.parseInt( input.substring( 1, input.length() );
case '*':
return total * Integer.parseInt( input.substring( 1, input.length() );
case '/':
return total / Integer.parseInt( input.substring( 1, input.length() );
}
EDIT
So the above as stated in the comment below does "left-to-right" calculation, ignoring the normal order ( + and / before + and -).
The following does the calculation the right way:
String input = tv1.getText().toString();
input = input.replace( "\n", "" );
input = input.replace( " ", "" );
int total = getValue( input );
The method getValue
is a recursive method and it should look like this:
private int getValue( String line ) {
int value = 0;
if( line.contains( "+" ) ) {
String[] lines = line.split( "\\+" );
value += getValue( lines[0] );
for( int i = 1; i < lines.length; i++ )
value += getValue( lines[i] );
return value;
}
if( line.contains( "-" ) ) {
String[] lines = line.split( "\\-" );
value += getValue( lines[0] );
for( int i = 1; i < lines.length; i++ )
value -= getValue( lines[i] );
return value;
}
if( line.contains( "*" ) ) {
String[] lines = line.split( "\\*" );
value += getValue( lines[0] );
for( int i = 1; i < lines.length; i++ )
value *= getValue( lines[i] );
return value;
}
if( line.contains( "/" ) ) {
String[] lines = line.split( "\\/" );
value += getValue( lines[0] );
for( int i = 1; i < lines.length; i++ )
value /= getValue( lines[i] );
return value;
}
return Integer.parseInt( line );
}
Special cases that the recursive method does not handle:
Also the fact the we're using Integers
might give some "odd" results in some cases as e.g. 5/3 = 1.
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;
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)));
}
Try Like this.
tv1.setText(" " + Integer.toString(X[i]) + "\n" + "+" + " " + Integer.toString(Y[i]));
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