字符串操作

时间:2014-03-03 18:39:30

标签: java android arrays string

这里我正在通过打开我的数据库然后尝试根据它绘制图表来阅读字符串。如果有人能帮我解决这个问题,我将非常感激。谢谢 我正在阅读MyDatabase的方法;

public String getData() {
    // TODO Auto-generated method stub

    String[] columns = new String[]{KEY_SYSTOLIC,KEY_DIASTOLIC,KEY_PULSE};
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result =" ";


    int iSys = c.getColumnIndex(KEY_SYSTOLIC);


    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){

        result = result + c.getString(iSys) + ",";

    }

    return result;

//数据库中的字符串看起来像“1,2,3,4,5”;

MyDatabase myDb = new MyDatabase(this);
             myDb.open();
             String pull = myDb.getData();
             pull = pull.substring(0, pull.length() - 1);
             myDb.close();

            String test = '"' + pull +  '"';
            String[] items = test.split(",");

            int[] results = new int[items.length];
            for (int i = 0; i < items.length; i++) {
                try {
                    results[i] = Integer.parseInt(items[i]);

                } catch (NumberFormatException nfe) {};
            }

// doesn't plots a graph on int[] results.             


              int[] yAxis = new int[]{11,12,13};

                TimeSeries series = new TimeSeries("X-Axis");
                for(int s=0; s< results.length; s++){

                    series.add(results[s], yAxis[s]);

            }

                XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
                dataset.addSeries(series);

                XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
                XYSeriesRenderer renderer = new XYSeriesRenderer();
                mRenderer.addSeriesRenderer(renderer);

                 LinearLayout chart_container=(LinearLayout)findViewById(R.id.Chart_layout);

                mChart=(GraphicalView)ChartFactory.getLineChartView(getBaseContext(), dataset, mRenderer);
                chart_container.addView(mChart);

            }

2 个答案:

答案 0 :(得分:2)

变量i在for循环之外不可用。

您可以在i循环之前声明变量for并使用它,但在这种情况下它不会帮助您。

如果你想填充arr [],试试这个:

String[] items = test.split(",");
int[] arr = new int[items.length];
  for (int i = 0; i < items.length; i++) {
    try {
       arr[i] = Integer.parseInt(items[i]);
         } catch (NumberFormatException nfe) {};
  }

答案 1 :(得分:2)

您无法在i循环之外访问for。它与try / catch

无关

作为一种解决方法,您可以在循环之前定义i