user3339618
user3339618

Reputation: 25

String manupilation

Here I am reading a string by opening my database and then trying to plot a graph based on that.I will really appreciate if anyone could help me solve this.Thanks Method from MyDatabase which I am reading;

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;

// The string from the database looks like "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);

            }

Upvotes: 1

Views: 63

Answers (2)

Denis Itskovich
Denis Itskovich

Reputation: 4523

You cannot access i outside the for loop. It's not related to try/catch

As a work around you can define i before the loop

Upvotes: 2

Cedric Simon
Cedric Simon

Reputation: 4659

The variable i is not available outside the for loop.

You could declare a variable i before the for loop and use it, but it would not help you in this case.

If you want to populate the arr[], try this:

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) {};
  }

Upvotes: 2

Related Questions