3

I am attempting to retrieve data from a table and convert them to double values in order to graph them. column 0 is filled with time values in the format of "00:00" and column 1 is filled with decimal values.

With this code I seem to have properly retrieved the data and converted the contents on column 1 to double value, but do not know how to convert a String with time values into double values in order to represent number of hours:

int row = tableRadio.getRowCount();
    for (int r = 0; r  < row; r++) {
        double r2 = Double.parseDouble( tableModel.getValueAt(r, 0).toString() );
        double r3 = Double.parseDouble( tableModel.getValueAt(r, 1).toString() );
        series.add(r2, r3);
    }

I keep getting java.lang.NumberFormatException: For input string: "0:00" when attempting to graph this data, which from what I understand is because a colon can not be equated with a number value. But how can I get say, "00:21" to instead show up as "0.21" or something that I can use to graph my data? Any suggestions and examples are welcome.

hnoel13
  • 58
  • 12
  • So, I guess you probably need to (1) separate out that `String` into the two numeric parts - maybe using the `split` method of the `String` class; (2) convert both parts to numbers - maybe using the `parseInt` method of the `Integer` class; (3) do some arithmetic - divide the number of minutes by 60 (watch out for integer division problems) and add the number of hours. Do those seem like three steps you can do by yourself? – Dawood ibn Kareem Apr 29 '17 at 06:52
  • @DawoodibnKareem Steps 2 and 3 both seem like something I could figure out myself. Step 1 however is a bit confusing to me, as I've only used the split method once and am not sure how to do it in this situation. previously I split the data contained in a scanner. Would you mind showing me how this step is done? – hnoel13 Apr 29 '17 at 06:59
  • OK, why don't you look up the Javadoc for the String class to see how the`split` method works? – Dawood ibn Kareem Apr 29 '17 at 07:40
  • @DawoodibnKareem I read up on the split method, looked back at the one split method I had ever used, and followed your steps. It's all working perfectly now and is graphing all of my data! Thank you so much for explaining things to me and being very patient in your replies! You were so incredibly helpful. If you'd like, feel free to put those steps into an actual answer so that I can accept it and you can get some points. – hnoel13 Apr 29 '17 at 08:27
  • You can [answer your own question](http://meta.stackoverflow.com/q/17463/163188) or defer to @DawoodibnKareem; either would be useful. This related [answer](http://stackoverflow.com/a/30110071/230513) compares the two approaches. – trashgod Apr 29 '17 at 19:22
  • 1
    You should post your answer as an answer, so that it can be voted on - not as an edit to the question. – Dawood ibn Kareem Apr 29 '17 at 22:08

2 Answers2

2

Use SimpleDateFormat to parse() the time column and create a time series chart, as shown here. Use setDateFormatOverride() on the resulting DateAxis to format the time, as shown here. If necessary, you can change the DateFormatSymbols, as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

So I got it working using @Dawood ibn Kareem's method. Here's the final code that worked for me:

    //get number of rows
    int row = tableRadio.getRowCount();
    for (int r = 0; r  < row; r++) {

        //get column0's values and convert to String
        String time = ( tableModel.getValueAt(r, 0).toString() );

        //split time into two arrays
        String[] timeSplit = time.split(":");

        //convert arrays to doubles 
        double hours = Double.parseDouble(timeSplit[0]);
        double min = Double.parseDouble(timeSplit[1]);

        //represent column0 values by number of hours
        double col0 = (min / 60) + hours;

        //get column1's values and convert to double
        double col1 = Double.parseDouble( tableModel.getValueAt(r, 1).toString() );

        //add values to series
        series.add(col0, col1);
    }//end of for loop`

Now all of the table values are converted to doubles and I have gotten them to graph successfully.

hnoel13
  • 58
  • 12