未知 "NumberFormatting" 问题
unknown "NumberFormatting" Issue
(提前,抱歉冗长且有点小众 post 但我被卡住了)完成 Java 编程新手在这里,我一直在关注 "Java ALL-IN-ONE for Dummies" 书,我遇到了一个似乎无法克服的障碍。出于某种原因,我的代码以及从本书下载网站获取的代码抛出了 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 (.95)1972: Young Frankenstein (.95)1973: Star Wars (.95)1987: The Princess Bride (.95)1989: Glory (.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)`
我的问题是为什么会这样,我该如何解决?或者我该如何捕捉不会破坏代码的异常?
(此外,如果有人能告诉我为什么我的代码不会拆分行,那也很棒)
谢谢!!
根据错误消息,这是文本文件中的字符串格式,因此下面的代码根据 space 分隔符拆分行,并通过删除所有额外字符
来过滤值
注意:如果文本文件中的每条记录都是采用这种格式的单独一行,则此代码有效
String s ="1946: It's a Wonderful Life (.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
price = Double.parseDouble(data[2]);
给您一个错误,因为您 Movie.txt
文件中的价格值包含一个 $
符号。从文件中删除 $
或在您的代码中替换它,如下面的代码所示
String p = data[2].toString().replace("$","");
price = Double.parseDouble(p);
实际发生的错误,来自:
year = Integer.parseInt(data[1]);
不是:
price = Double.parseDouble(data[2]);
如:
For input string: "14.95"
at java.base/java.lang.Integer.parseInt(Unknown Source)
错误告诉您:您尝试将 14.95 解析为 int 值。整数值没有浮点数。
所以问题是:您正在尝试将浮点数解析为 int。
根本原因:你的拆分是错误的,你是putting/parsing错误的拆分数据。
deadpool 的精彩回答中解释了如何修复输入数据的错误解析。
(提前,抱歉冗长且有点小众 post 但我被卡住了)完成 Java 编程新手在这里,我一直在关注 "Java ALL-IN-ONE for Dummies" 书,我遇到了一个似乎无法克服的障碍。出于某种原因,我的代码以及从本书下载网站获取的代码抛出了 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 (.95)1972: Young Frankenstein (.95)1973: Star Wars (.95)1987: The Princess Bride (.95)1989: Glory (.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)`
我的问题是为什么会这样,我该如何解决?或者我该如何捕捉不会破坏代码的异常?
(此外,如果有人能告诉我为什么我的代码不会拆分行,那也很棒)
谢谢!!
根据错误消息,这是文本文件中的字符串格式,因此下面的代码根据 space 分隔符拆分行,并通过删除所有额外字符
来过滤值注意:如果文本文件中的每条记录都是采用这种格式的单独一行,则此代码有效
String s ="1946: It's a Wonderful Life (.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
price = Double.parseDouble(data[2]);
给您一个错误,因为您 Movie.txt
文件中的价格值包含一个 $
符号。从文件中删除 $
或在您的代码中替换它,如下面的代码所示
String p = data[2].toString().replace("$","");
price = Double.parseDouble(p);
实际发生的错误,来自:
year = Integer.parseInt(data[1]);
不是:
price = Double.parseDouble(data[2]);
如:
For input string: "14.95"
at java.base/java.lang.Integer.parseInt(Unknown Source)
错误告诉您:您尝试将 14.95 解析为 int 值。整数值没有浮点数。
所以问题是:您正在尝试将浮点数解析为 int。
根本原因:你的拆分是错误的,你是putting/parsing错误的拆分数据。
deadpool 的精彩回答中解释了如何修复输入数据的错误解析。