0

Java beginner here but I am sincerely trying. The goal of this program is to read two values from the Realtor11.txt file and assign them to variables.

The contents of Realtor11.txt is (no spaces):

John

100

See section // Read Realtor11.txt Not sure what I'm doing wrong but currently the error is

Realtor11.java:48: error: incompatible types price = in.readLine(); ^ required: double found: String 1 error Error: Could not find or load main class Realtor11 [Finished in 1.1s]

// java class for keyboard I/O
import java.util.Scanner;
// java class for JOption GUI
import javax.swing.JOptionPane;
// File reader
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;

public class Realtor11
{
    public static void main(String[] args)
    {
    // Keyboard and file input
        Scanner console = new Scanner(System.in); 
        Scanner inputStream = null;
    // price of the house, the cost to sell the house and the commission
        double price, cost, commission;
    // seller’s name
        String seller; 

    // GUI diplay message declaration
        String display_message = "This program calculates the cost to sell a home\n" 
        + "and the commission paid to an individual sales agent.\n\n"
        + "The user is asked for the last name of the seller and the\n"
        + "sales price.\n\n";

    // Output descriptive messages
        JOptionPane.showMessageDialog(null, display_message, "Lab 1 Description", JOptionPane.INFORMATION_MESSAGE);

    // Read Realtor11.txt
        try {
            BufferedReader in = new BufferedReader(new FileReader("Realtor11.txt"));
            while (in.read()!= -1);
            seller = in.readLine();
            price = in.readLine();
            in.close();
            } 
            catch (IOException e) {}

    // calculate the cost and the commission
        cost = 0.06 * price;
        commission = 0.015 * price;
    // display the input and results
        String 
            out1 = String.format("%nThe " + seller + "’s" + " home sold for $%.2f%n", price),
            out2 = String.format("The cost to sell the home was $%.2f%n", cost),
            out3 = String.format("The selling or listing agent earned $%.2f%n", commission);

        JOptionPane.showMessageDialog(null, out1 + out2 + out3, seller + "'s Home Sale", JOptionPane.INFORMATION_MESSAGE);

    // Output to file
    // still writing this. 

    }
}

2 Answers 2

5

The method readLine() returns a String value where you're expecting a double value (see API). You must convert the String value to a double as so:

price = Double.parseDouble(in.readLine());
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you ! I had no idea I could do that. I read the Oracle docs and did not find this kind of syntax.
No problem. Just remember you have to convert when needed. Also, if you found this answer to be correct, mark it correct as so: meta.stackexchange.com/questions/5234/…
0

the readline() function returns a string. What you would need to do is to manually convert that price string to a double double price = Double.parseDouble(string version of price);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.