Java读取文件:列出无效数据
我想列出文本文件中的所有无效数据,如下所示:Java读取文件:列出无效数据,java,text-files,Java,Text Files,我想列出文本文件中的所有无效数据,如下所示: String score; int count=0; int number; TextIO.readFile("scores.txt"); System.out.println("reading the file now..."); while (!TextIO.eof()) { count ++; score = TextIO.getln();
String score;
int count=0;
int number;
TextIO.readFile("scores.txt");
System.out.println("reading the file now...");
while (!TextIO.eof())
{
count ++;
score = TextIO.getln();
number = Integer.parseInt(score);
if (number < 1000)
{
System.out.println(score);
}
1299
德维
1237
213
国民党
任何小于1000或小于1000的数字都是无效的。我的代码如下所示:
String score;
int count=0;
int number;
TextIO.readFile("scores.txt");
System.out.println("reading the file now...");
while (!TextIO.eof())
{
count ++;
score = TextIO.getln();
number = Integer.parseInt(score);
if (number < 1000)
{
System.out.println(score);
}
字符串分数;
整数计数=0;
整数;
readFile(“scores.txt”);
System.out.println(“现在读取文件…”);
而(!TextIO.eof())
{
计数++;
score=TextIO.getln();
number=Integer.parseInt(分数);
如果(数量<1000)
{
系统输出打印项次(分数);
}
但是它在列出所有非数字数据时失败。有人能帮我吗?谢谢!这里有一种方法:
try {
int number = Integer.parseInt(score);
if (number >= 1000) {
// number is valid, skip
continue;
}
} catch (NumberFormatException nfe) {
// not a number; fall through
}
System.out.println(score);
或者,您可以让它自然循环并复制打印语句:
try {
int number = Integer.parseInt(score);
if (number < 1000) {
System.out.println(score);
}
} catch (NumberFormatException nfe) {
System.out.println(score);
}
试试看{
int number=整数.parseInt(分数);
如果(数量<1000){
系统输出打印项次(分数);
}
}捕获(NumberFormatException nfe){
系统输出打印项次(分数);
}
这里有一种方法:
try {
int number = Integer.parseInt(score);
if (number >= 1000) {
// number is valid, skip
continue;
}
} catch (NumberFormatException nfe) {
// not a number; fall through
}
System.out.println(score);
或者,您可以让它自然循环并复制打印语句:
try {
int number = Integer.parseInt(score);
if (number < 1000) {
System.out.println(score);
}
} catch (NumberFormatException nfe) {
System.out.println(score);
}
试试看{
int number=整数.parseInt(分数);
如果(数量<1000){
系统输出打印项次(分数);
}
}捕获(NumberFormatException nfe){
系统输出打印项次(分数);
}
如果分数
不是一个数字,则它将无法转换为整数,并将引发异常。您需要捕获此异常
try {
number = Integer.parseInt(score);
if (number < 1000)
{
System.out.println(score);
}
} catch (NumberFormatException ex) {
System.out.println ("not a number");
}
试试看{
number=Integer.parseInt(分数);
如果(数量<1000)
{
系统输出打印项次(分数);
}
}捕获(NumberFormatException ex){
System.out.println(“不是数字”);
}
如果分数
不是一个数字,则它将无法转换为整数,并将引发异常。您需要捕获此异常
try {
number = Integer.parseInt(score);
if (number < 1000)
{
System.out.println(score);
}
} catch (NumberFormatException ex) {
System.out.println ("not a number");
}
试试看{
number=Integer.parseInt(分数);
如果(数量<1000)
{
系统输出打印项次(分数);
}
}捕获(NumberFormatException ex){
System.out.println(“不是数字”);
}
什么是您无法解决的错误?错误消息显示“线程中的异常”main“java.lang.NumberFormatException:对于输入字符串:“dwhie”您无法解决的错误是什么?错误消息显示“线程中的异常”main“java.lang.NumberFormatException:对于输入字符串:“dwhie”嘿,谢谢你的帮助!但是当我运行代码时,输出中没有出现任何问题。你知道这有什么问题吗?嘿,谢谢你的帮助!但是当我运行代码时,输出中没有出现任何问题。你知道这有什么问题吗?嘿,谢谢你的帮助!但是当我运行代码时,输出中没有出现任何问题。你知道这有什么问题吗这?你确定文件已打开吗?尝试将每一行打印为调试信息嘿,谢谢你的帮助!但是当我运行代码时,输出中没有任何内容。你知道这有什么问题吗?你确定文件已打开吗?尝试将每一行打印为调试信息