gpt4 book ai didi

java程序编译没有错误但无法运行

转载 作者:行者123 更新时间:2023-12-01 08:06:10 25 4
gpt4 key购买 nike

这是我在 stackoverflow 成员建议的更改后的代码。代码编译时没有错误,但当我运行“java Realtor11”时,出现以下错误。我正在导入正确的库,所以不确定问题是什么。

该程序的目标是读取两个值(第一行是字符串 (John),第二行是 double 值 (100)),并将它们与一些计算一起输出到 JOption 消息。

Exception in thread "main" java.lang.NullPointerException at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at java.lang.Double.parseDouble(Unknown Source) at Realtor11.main(Realtor11.java:49)

// 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 = 0;
double cost, commission;
// seller's name
String seller = "name";



// 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 = Double.parseDouble(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.

}

}

最佳答案

问题出在这一行。

while (in.read()!= -1);

由于末尾有分号,这是一种“退化循环”。它继续从文件中读取,直到没有任何内容可供读取。之后,就没有要解析的 double 值了。

关于java程序编译没有错误但无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21417407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com