(预先,对冗长且有点利基的帖子表示抱歉,但我被深深地卡住了)在这里完成Java编程新手,并且我一直在跟着《 Java虚拟人傻瓜全书》,以及我遇到了我似乎无法过去的障碍。由于某种原因,我的代码以及从本书下载网站获取的代码都引发NumberFormatException。我的代码如下。
`package videoRead;
import java.io.*;
import java.text.NumberFormat;
public class reader
{
public static void main(String[] args)
{
NumberFormat cf = NumberFormat.getCurrencyInstance();
BufferedReader in = getReader("Movie.txt");
Movie movie = readMovie(in);
while (movie != null)
{
String msg = Integer.toString(movie.year);
msg += ": " + movie.title;
msg += " (" + cf.format(movie.price) + ")";
System.out.print(msg);
movie = readMovie(in);
}
}
private static BufferedReader getReader(String name)
{
BufferedReader in = null;
try
{
File file = new File(name);
in = new BufferedReader(
new FileReader("C:\\Users\\hunte\\Desktop\\Movie.txt") );
}
catch (FileNotFoundException e)
{
System.out.print(
"the file doesn't exist.");
System.exit(0);
}
return in;
}
private static Movie readMovie(BufferedReader in)
{
String title;
int year;
double price;
String line = "";
String[] data;
try
{
line = in.readLine();
}
catch (IOException e)
{
System.out.print("I/O Error");
System.exit(0);
}
if (line == null)
return null;
else
{
data = line.split("\t");
title = data[0];
year = Integer.parseInt(data[1]);
price = Double.parseDouble(data[2]);
return new Movie(title, year, price);
}
}
private static class Movie
{
public String title;
public int year;
public double price;
public Movie(String title, int year, double price)
{
this.title = title;
this.year = year;
this.price = price;
}
}
}
,错误代码为
`1946: It's a Wonderful Life ($14.95)1972: Young Frankenstein ($16.95)1973: Star Wars ($17.95)1987: The Princess Bride ($14.95)1989: Glory ($14.95)Exception in thread "main" java.lang.NumberFormatException: For input string: "14.95"
at java.base/java.lang.NumberFormatException.forInputString(Unknown Source)
at java.base/java.lang.Integer.parseInt(Unknown Source)
at java.base/java.lang.Integer.parseInt(Unknown Source)
at videoRead/videoRead.reader.readMovie(reader.java:65)
at videoRead/videoRead.reader.main(reader.java:20)`
我的问题是为什么会这样,怎么解决?还是我该如何捕获不会破坏代码的异常?
(此外,如果有人可以告诉我为什么我的代码不会拆分行,那也很棒)
谢谢!
答案 0 :(得分:1)
根据错误消息,这是文本文件中字符串的格式,因此下面的代码根据空格分隔符分割行,并通过删除所有多余的字符来过滤值
注意:如果每个记录都是文本文件中具有这种格式的单独一行,则此代码有效
String s ="1946: It's a Wonderful Life ($14.95)";
String[] ar = s.split(" ");
System.out.println(ar[0].substring(0, ar[0].length()-1));
String str = String.join("," ,Arrays.copyOfRange(ar, 1, ar.length-2)).replaceAll(",", " ");
System.out.println(str);
System.out.println(ar[ar.length-1].substring(2, ar[ar.length-1].length()-1));
输出:
1946
It's a Wonderful
14.95
答案 1 :(得分:0)
price = Double.parseDouble(data[2]);
给您一个错误,因为您的Movie.txt
文件中的价格值包含一个$
符号。从文件中删除$
或将其替换为代码,如下面的代码
String p = data[2].toString().replace("$","");
price = Double.parseDouble(p);
答案 2 :(得分:0)
实际发生的错误来自:
year = Integer.parseInt(data[1]);
不是:
price = Double.parseDouble(data[2]);
如:
对于输入字符串:“ 14.95”
在java.base / java.lang.Integer.parseInt(未知来源)
该错误告诉您:您尝试将14.95解析为 int 值。整数值没有浮点数。
所以问题是:您试图将浮点数解析为int。
根本原因:您的拆分错误,您正在放置/解析错误的拆分数据。
死池中一个不错的答案说明了如何解决输入数据解析错误的问题。