Doubles are somehow Strings?
Tried to compile the following code and got these errors:
Code :
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:
Code :
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";
}
}
Code :
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]);
//**********************************************
System.out.println("cStartH: " + cStartH);
System.out.println("cStartM: " + cStartM);
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:
Code :
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.
Where does the change get made?
Thanks.
Re: Doubles are somehow Strings?
Quote:
Originally Posted by
collegejavastudent
Tried to compile the following code and got these errors:
Code :
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);
^
Look in the java APIs for the method that is giving the error and check its signature (what types it accepts and what type it returns)
Check that you have honoured that signature.
Re: Doubles are somehow Strings?
I understand that it accepts doubles and seems to think it's taking Strings here. My problem is that I can't understand why it seems to be a String.
Re: Doubles are somehow Strings?
As mentioned, look at the API for that method
NumberFormat (Java Platform SE 6)
The method takes a double and returns a String. Your code is written such that the method takes a double and returns a double - and this method does not exist.
Re: Doubles are somehow Strings?
The error message texts you posted do NOT go with the code you posted.
Quote:
Course.java:43: incompatible types
found : java.lang.String
required: double
double cStart = df.format(cStart);
^
This message says the error is at line 43 in the Course.java source file.
When I do a Find for "double cStart = df.format(cStart);" nothing is found in the Course.java file???
Check for duplicate source files or recompile and then post the code and the errors that go with it.
Re: Doubles are somehow Strings?
Quote:
Originally Posted by
Norm
cStart = df.format(cStart);" nothing is found in the Course.java file???
Check for duplicate source files or recompile and then post the code and the errors that go with it.
It's simpler than that. What does DecimalFormat.format(double..) return?
Re: Doubles are somehow Strings?
Quote:
Originally Posted by
2by4
It's simpler than that. What does DecimalFormat.format(double..) return?
A string. Wasn't looking at that before. Thanks!
Re: Doubles are somehow Strings?
My problem is: How do you get that error message with the code posted???
The only solution I can find is:
Code :
DecimalFormat df = new DecimalFormat("#.##");
String cStart = "12";
double cStart = df.format(cStart);
This code generates two error messages. The OP stripped off the first one.
Code :
Running: F:\Java\jdk1.6.0_29\bin\javac.exe -Xlint -g -deprecation -classpath D:\JavaDevelopment\;.;..\. TestCode8.java
TestCode8.java:269: cStart is already defined in main(java.lang.String[])
double cStart = df.format(cStart);
^
TestCode8.java:269: incompatible types
found : java.lang.String
required: double
double cStart = df.format(cStart);
^
Re: Doubles are somehow Strings?
Quote:
Originally Posted by
Norm
My problem is: How do you get that error message with the code posted???
The only solution I can find is:
Code :
DecimalFormat df = new DecimalFormat("#.##");
String cStart = "12";
double cStart = df.format(cStart);
This code generates two error messages. The OP stripped off the first one.
Code :
Running: F:\Java\jdk1.6.0_29\bin\javac.exe -Xlint -g -deprecation -classpath D:\JavaDevelopment\;.;..\. TestCode8.java
TestCode8.java:269: cStart is already defined in main(java.lang.String[])
double cStart = df.format(cStart);
^
TestCode8.java:269: incompatible types
found : java.lang.String
required: double
double cStart = df.format(cStart);
^
I don't know why you have defined cStart twice.
cStart is a double that is being passed into DecimalFormat.format(), which returns String. But he has assigned this String to the double cStart, hence the error.
He tried to recycle cStart through the format method, but format returns something of a different type.
Re: Doubles are somehow Strings?
Quote:
I don't know why you have defined cStart twice.
How can you generate the error message shown in post #1?
Can you write some code to generate the error message as posted in #1?
I think that the OP's error message and code do not go together.
Re: Doubles are somehow Strings?
Quote:
Originally Posted by
Norm
How can you generate the error message shown in post #1?
Can you write some code to generate the error message as posted in #1?
I think that the OP's error message and code do not go together.
Code :
import java.text.DecimalFormat;
class Some{
public static void main(String[] args){
double a = 1.0;
a = new DecimalFormat("#.#").format(a);
}
}
Re: Doubles are somehow Strings?
That is not quite the same as the code shown in the error message:
double cStart = df.format(cStart);
Re: Doubles are somehow Strings?
Quote:
Originally Posted by
Norm
That is not quite the same as the code shown in the error message:
double cStart = df.format(cStart);
Code :
import java.text.DecimalFormat;
class Some{
public static void main(String[] args){
double a = new DecimalFormat("#.#").format(a);
}
}
I'm sure you could have done the honours yourself?
Re: Doubles are somehow Strings?
Thank you. It never occurred to me to use the variable I'm defining in the expression that defines it.
A blind spot.
Re: Doubles are somehow Strings?
Quote:
Originally Posted by
Norm
Thank you. It never occurred to me to use the variable I'm defining in the expression that defines it.
A blind spot.
No problem :-)
This is why I find these forums helpful. I learn to spot those bugs that may not spring immediately to mind.
In this case, the big clue was in the compiler message.
And even if you don't intend to use a variable in its own definition, it can still happen by accident if you are rehashing code. So it's a sort of bug worth having an eye open for (pardon my English).