Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Doubles are somehow Strings?

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Doubles are somehow Strings?

    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]);
     
    				//**********************************************
    				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:
    		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.


  2. #2
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Doubles are somehow Strings?

    Quote Originally Posted by collegejavastudent View Post
    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);
                                            ^
    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.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Doubles are somehow Strings?

    The error message texts you posted do NOT go with the code you posted.
    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.

  6. #6
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Doubles are somehow Strings?

    Quote Originally Posted by Norm View Post
    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?

  7. #7
    Junior Member
    Join Date
    Dec 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Doubles are somehow Strings?

    Quote Originally Posted by 2by4 View Post
    It's simpler than that. What does DecimalFormat.format(double..) return?
    A string. Wasn't looking at that before. Thanks!

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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:
          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.
    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);
                                             ^

  9. #9
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Doubles are somehow Strings?

    Quote Originally Posted by Norm View Post
    My problem is: How do you get that error message with the code posted???
    The only solution I can find is:
          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.
    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.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Doubles are somehow Strings?

    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.

  11. #11
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Doubles are somehow Strings?

    Quote Originally Posted by Norm View Post
    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.
    import java.text.DecimalFormat;
     
    class Some{
    	public static void main(String[] args){
    		double a = 1.0;
    		a = new DecimalFormat("#.#").format(a);
    	}
    }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Doubles are somehow Strings?

    That is not quite the same as the code shown in the error message:
    double cStart = df.format(cStart);

  13. #13
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Doubles are somehow Strings?

    Quote Originally Posted by Norm View Post
    That is not quite the same as the code shown in the error message:
    double cStart = df.format(cStart);
    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?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  15. #15
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Doubles are somehow Strings?

    Quote Originally Posted by Norm View Post
    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).

Similar Threads

  1. Adding doubles and ints.
    By SkyAphid in forum Java Theory & Questions
    Replies: 6
    Last Post: September 8th, 2011, 05:54 PM
  2. Addition of doubles (newbie question)
    By archyb in forum Java Theory & Questions
    Replies: 2
    Last Post: April 21st, 2011, 09:52 AM
  3. Reading doubles from file
    By fawx in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 24th, 2009, 11:57 PM
  4. Strings
    By BeSwift21 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 13th, 2009, 07:02 PM
  5. Replies: 2
    Last Post: June 19th, 2008, 03:58 AM