Tried to compile the following code and got these errors:
Course.java:43: incompatible types
found : java.lang.String
required: double
double cStart = df.format(cStart);
^
Course.java:44: incompatible types
found : java.lang.String
required: double
double cEnd = df.format(cEnd);
^
Here are my two classes:
import java.text.DecimalFormat;
public class Course implements Comparable<Course>{
private String name;
private double cStart;
private double cEnd;
public Course(String cName, double cStart, double cEnd) {
this.name = cName;
this.cStart = cStart;
this.cEnd = cEnd;
}
public String getName() {
return name;
}
public double getSTime() {
return cStart;
}
public double getETime() {
return cEnd;
}
public int compareTo(Course c) {
if(cEnd > c.getETime())
return 1;
else if(cEnd < c.getETime())
return -1;
return 0;
}
public String toString() {
//Decimal to two places
DecimalFormat df = new DecimalFormat("#.##");
cStart = df.format(cStart);
cEnd = df.format(cEnd);
//Change back to strings with colons for time
String cStartA = Double.toString(cStart);
String cEndA = Double.toString(cEnd);
cStartA = cStartA.replace('.',':');
cEndA = cEndA.replace('.', ':');
return name + "\n" + cStartA + "\n" + cEndA + "\n\n";
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Fitter {
private ArrayList<Course> courses;
private ArrayList<Course> fittedCourses;
private double cEndTime;
private double cStartTime;
private double lastEnd = 8;
public Fitter(File courseList) throws FileNotFoundException, NumberFormatException{
//initialize variables
courses = new ArrayList<Course>();
fittedCourses = new ArrayList<Course>();
Scanner courseScanner = new Scanner(courseList);
//scan lines in File
while(courseScanner.hasNextLine()) {
//get name and start/ end times
String cName = courseScanner.nextLine();
String cStartS = courseScanner.nextLine();
String cEndS = courseScanner.nextLine();
//*********************************************
System.out.println("cName: " + cName);
System.out.println("cStartS: " + cStartS);
System.out.println("cEndS: " + cEndS);
//convert times into decimals, first checking number formatting
String[] cStartSplit = cStartS.split(":");
double cStartH = Double.parseDouble(cStartSplit[0]);
double cStartM = Double.parseDouble(cStartSplit[1]);
if ((cStartH < 8) || (cStartH > 17))
throw new NumberFormatException();
if (cStartM > 60)
throw new NumberFormatException();
cStartS = cStartSplit[0] + "." + cStartSplit[1];
double cStart = Double.valueOf(cStartS.trim()).doubleValue();
String[] cEndSplit = cEndS.split(":");
double cEndH = Double.parseDouble(cEndSplit[0]);
double cEndM = Double.parseDouble(cEndSplit[1]);
if ((cEndH < 8) || (cEndH > 17))
throw new NumberFormatException();
if (cEndM > 60)
throw new NumberFormatException();
cEndS = cEndSplit[0] + "." + cEndSplit[1];
double cEnd = Double.valueOf(cEndS.trim()).doubleValue();
Course newCourse = new Course(cName, cStart, cEnd);
courses.add(newCourse);
//skip blank line
if(courseScanner.hasNextLine())
courseScanner.nextLine();
}
}
public void fit() throws NumberFormatException{
//sort by size
Collections.sort(courses);
//for each course (starting with earliest end time) add
//if no conflict exists
for(Course course : courses) {
if(lastEnd < course.getSTime()) {
fittedCourses.add(course);
lastEnd = course.getETime();
}
}
}
public String toString() {
String info = "We made the following schedule: \n\n";
for(Course course : fittedCourses) {
info += course.toString() + "\n";
}
return info;
}
}
Now, I'm new to using DecimalFormat, but the problem that I seem to be encountering is the type that cStart and cEnd wind up as. Before I added all the code aside from the last line to the Course class's toString method
(code read as:
return name + "\n" + cStart + "\n" + cEnd + "\n\n";
), the code was compiling and running as it should (obviously printing as 9.2 rather than 9:20), and it seemed as if both cStart and cEnd were being treated as doubles.
If I try to do something to manage cStart and cEnd as anything other than a double, I'm told that the variables are in fact a double... What's going on?
Thanks.