0

hi i have problem in displaying a value into my TextView.. For example i will input
1,2,3,4 then i like to display the output in this manner in my TextView..How can i do that? please help me, thank you in advance
1 appeared 1 times
2 appeared 1 times
3 appeared 1 times
4 appeared 1 times
here's my code:

String []values = ( sum.getText().toString().split(","));
double[]  convertedValues = new double[values.length];

    Arrays.sort(convertedValues);
    int i=0;
    int c=0;
            while(i<values.length-1){
            while(values[i]==values[i+1]){
            c++; 
                i++;  
             }   

            table.setText(values[i] + " appeared " + c + " times");          
        c=1;
        i++;
            if(i==values.length-1)
            table.setText(values[i] + " appeared " + c + " times");  
4

2 回答 2

1

让你的 textView 支持多行,然后在代码中创建一个 StringBuffer 并将结果附加到它上面,比如

resultString.append(result).append(" appeared").append(c).append(" times\n");

之后,您为 textView 设置文本,例如:

textView.setText(resultString.toString());
于 2013-11-07T08:55:53.187 回答
1

这是想法:

    // this is test string, you can read it from your textView
    String []values = ( "2, 1, 3, 5, 1, 2".toString().split(","));
    int [] intValues = new int[values.length];
    // convert string values to int
    for (int i = 0; i < values.length; ++i) {
        intValues[i] = Integer.parseInt(values[i].trim());
    }

    // sort integer array
    Arrays.sort(intValues);

    StringBuilder output = new StringBuilder();
    // iterate and count occurrences
    int count = 1;
    // you don't need internal loop, one loop is enough
    for (int i = 0; i < intValues.length; ++i) {
        if (i == intValues.length - 1 || intValues[i] != intValues[i + 1]) {
            // we found end of "equal" sequence
            output.append(intValues[i] + " appeared " + count + " times\n");
            count = 1; // reset count
        } else {
            count++; // continue till we count all equal values
        }
    }

    System.out.println(output.toString()); // prints what you extected
    table.setText(output.toString()); // display output
于 2013-11-07T09:15:04.327 回答