I have a text file with entries of numbers in the format of 00:00. I know how to read this String from the file. What I don't know is how to parse it so that the left side of the decimal knows to join with the right side as one number.

If I do a split within a split, the inner split gives me two values when I only want one.


File database = new File (FILE);        
Scanner read = new Scanner (database);
String [] dataEntry;
String [] times;
float [] correctTime = null;

    while (read.hasNext ())
    {
        dataEntry = read.nextLine().split(",");  

        times = dataEntry[0].split(":");
        correctTime = new double[times.length];
        //I get stuck here, I know the above line is incorrect              

    }

2 Answers

0
Community On

I am assuming that you got a string in 00:00 format so you can first replace ':' with '.' then you will get a string in 00.00 format, after that parse that string to double as follows.

Double.parseDouble(str.replace(':','.'))

here 'str' is the String which you got in 00:00 format.

0
Joop Eggen On

I guess you want to have a double for something like work time management.

// 06:30 -> 6.5
// 07:45 -> 7.75
String[] hhmm = dataEntry[0].split(":");
int hh = Integer.parseInt(hhmm[0]);
int mm = Integer.parseInt(hhmm[1]);
double decimalTime = hh + mm / 60.0; // Floating point division because of 60.0

Alternatively one could use the new java time API.