Java 读取文本文件并为变量赋值

Java 读取文本文件并为变量赋值,java,Java,Java初学者在这里,但我真诚地尝试。该程序的目标是从Realtor11.txt文件中读取两个值,并将它们分配给变量 Realtor11.txt的内容为(无空格): 约翰 一百 请参阅第//Read Realtor11.txt节不确定我做错了什么,但当前错误是 Realtor11.java:48:错误:不兼容的类型 price=in.readLine(); ^ 所需:双人 找到:字符串 1错误 错误:无法找到或加载主类Realtor11 [以1.1s完成] // java class for k

Java初学者在这里,但我真诚地尝试。该程序的目标是从Realtor11.txt文件中读取两个值,并将它们分配给变量

Realtor11.txt的内容为(无空格):

约翰

一百

请参阅第//Read Realtor11.txt节不确定我做错了什么,但当前错误是

Realtor11.java:48:错误:不兼容的类型 price=in.readLine(); ^ 所需:双人 找到:字符串 1错误 错误:无法找到或加载主类Realtor11 [以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. 

    }
}
方法
readLine()。必须将
字符串
值转换为
双精度
,如下所示:

price = Double.parseDouble(in.readLine());
方法
readLine()。必须将
字符串
值转换为
双精度
,如下所示:

price = Double.parseDouble(in.readLine());
函数的作用是:返回一个字符串。您需要做的是手动将该价格字符串转换为double double price=double.parseDouble(price的字符串版本)

readline()函数返回一个字符串。您需要做的是手动将该价格字符串转换为double
double price=double.parseDouble(price的字符串版本)

谢谢大家!!我不知道我能做到。我阅读了Oracle文档,没有发现这种语法。没问题。只要记住,你必须在需要时转换。此外,如果您发现这个答案是正确的,请将它标记为正确:谢谢!我不知道我能做到。我阅读了Oracle文档,没有发现这种语法。没问题。只要记住,你必须在需要时转换。此外,如果您发现此答案正确,请将其标记为正确: