Java NumberFormatException跳过行
我正在写一个程序,读取一系列数字。例如:Java NumberFormatException跳过行,java,io,numberformatexception,Java,Io,Numberformatexception,我正在写一个程序,读取一系列数字。例如: 45 63 74g 34.7 75 我只是想让我的程序跳过包含任何字母的行。任何帮助都将不胜感激。谢谢 如果有区别,下面是我的代码: import java.io.*; public class ScoreReader { public static void main(String[] args) { BufferedReader reader = null; try { Stri
45
63
74g
34.7
75
我只是想让我的程序跳过包含任何字母的行。任何帮助都将不胜感激。谢谢
如果有区别,下面是我的代码:
import java.io.*;
public class ScoreReader {
public static void main(String[] args) {
BufferedReader reader = null;
try {
String currentLine;
reader = new BufferedReader(new FileReader("QuizScores.txt"));
while ((currentLine = reader.readLine()) != null) {
int sum = 0;
String[] nums = currentLine.split("\\s+");
for (int i = 0; i < nums.length; i++) {
int num = Integer.parseInt(nums[i]);
if (num != -1) {
sum += num;
}
}
System.out.println(sum / nums.length);
}
} catch (IOException err) {
err.printStackTrace();
}
catch (NumberFormatException err) {
}
finally {
try {
if (reader != null)
reader.close();
} catch (IOException err) {
err.printStackTrace();
}
}
}
}
import java.io.*;
公共类记分器{
公共静态void main(字符串[]args){
BufferedReader reader=null;
试一试{
串电流线;
reader=newbufferedreader(newfilereader(“QuizScores.txt”);
while((currentLine=reader.readLine())!=null){
整数和=0;
字符串[]nums=currentLine.split(\\s+);
对于(int i=0;i
使用正则表达式怎么样?例如:
if (currentLine.matches(".*[a-zA-Z].*")) {
//letters contained.
} else {
//no letters contained.
}
参见正则表达式演示:使用正则表达式如何?例如:
if (currentLine.matches(".*[a-zA-Z].*")) {
//letters contained.
} else {
//no letters contained.
}
请参阅正则表达式演示:当抛出异常时,执行跳转到catch块。在您拥有的内容中,这是在循环之后,因此循环不会继续,只需在
parseInt
周围添加一个try
try {
String currentLine;
reader = new BufferedReader(new FileReader("QuizScores.txt"));
while ((currentLine = reader.readLine()) != null) {
int sum = 0;
String[] nums = currentLine.split("\\s+");
for (int i = 0; i < nums.length; i++) {
try{
int num = Integer.parseInt(nums[i]);
if (num != -1) {
sum += num;
}
} catch( NumberFormatException nfe )
{
// maybe log it?
}
}
System.out.println(sum / nums.length);
}
} catch (IOException err) {
err.printStackTrace();
}
// catch (NumberFormatException err) {}
finally {
try {
if (reader != null){
reader.close();
} catch (IOException err) {
err.printStackTrace();
}
}
试试看{
串电流线;
reader=newbufferedreader(newfilereader(“QuizScores.txt”);
while((currentLine=reader.readLine())!=null){
整数和=0;
字符串[]nums=currentLine.split(\\s+);
对于(int i=0;i
另外请注意,您使用的是
Integer.parseInt
,它将抛出一个异常,输入“34.7”
,因此您可能希望使用Double.parseDouble
当抛出异常时,执行跳转到catch块。在现有的情况下,这是在循环之后,因此循环不会继续,只需在parseInt
周围添加一个try
try {
String currentLine;
reader = new BufferedReader(new FileReader("QuizScores.txt"));
while ((currentLine = reader.readLine()) != null) {
int sum = 0;
String[] nums = currentLine.split("\\s+");
for (int i = 0; i < nums.length; i++) {
try{
int num = Integer.parseInt(nums[i]);
if (num != -1) {
sum += num;
}
} catch( NumberFormatException nfe )
{
// maybe log it?
}
}
System.out.println(sum / nums.length);
}
} catch (IOException err) {
err.printStackTrace();
}
// catch (NumberFormatException err) {}
finally {
try {
if (reader != null){
reader.close();
} catch (IOException err) {
err.printStackTrace();
}
}
试试看{
串电流线;
reader=newbufferedreader(newfilereader(“QuizScores.txt”);
while((currentLine=reader.readLine())!=null){
整数和=0;
字符串[]nums=currentLine.split(\\s+);
对于(int i=0;i
另外请注意,您使用的是Integer.parseInt
,它将对输入“34.7”
引发异常,因此您可能希望使用Double.parseDouble
您可以尝试以下方法:
import java.io.*;
public class ScoreReader {
public static void main(String[] args) {
BufferedReader reader = null;
try {
String currentLine;
reader = new BufferedReader(new FileReader("QuizScores.txt"));
while ((currentLine = reader.readLine()) != null) {
int sum = 0;
String[] nums = currentLine.split("\\s+");
for (int i = 0; i < nums.length; i++) {
if (isInt(num)) {
sum += num;
}
}
System.out.println(sum / nums.length);
}
} catch (IOException err) {
err.printStackTrace();
}
finally {
try {
if (reader != null)
reader.close();
} catch (IOException err) {
err.printStackTrace();
}
}
}
public boolean isInt(String num)
{
boolean flag=false;
try
{
int i=Integer.parseInt(num);
flag=true;
}
catch(NumberFormatException e)
{
e.printStackTrace();
}
return flag;
}
}
import java.io.*;
公共类记分器{
公共静态void main(字符串[]args){
BufferedReader reader=null;
试一试{
串电流线;
reader=newbufferedreader(newfilereader(“QuizScores.txt”);
while((currentLine=reader.readLine())!=null){
整数和=0;
字符串[]nums=currentLine.split(\\s+);
对于(int i=0;i
您可以尝试以下方法:
import java.io.*;
public class ScoreReader {
public static void main(String[] args) {
BufferedReader reader = null;
try {
String currentLine;
reader = new BufferedReader(new FileReader("QuizScores.txt"));
while ((currentLine = reader.readLine()) != null) {
int sum = 0;
String[] nums = currentLine.split("\\s+");
for (int i = 0; i < nums.length; i++) {
if (isInt(num)) {
sum += num;
}
}
System.out.println(sum / nums.length);
}
} catch (IOException err) {
err.printStackTrace();
}
finally {
try {
if (reader != null)
reader.close();
} catch (IOException err) {
err.printStackTrace();
}
}
}
public boolean isInt(String num)
{
boolean flag=false;
try
{
int i=Integer.parseInt(num);
flag=true;
}
catch(NumberFormatException e)
{
e.printStackTrace();
}
return flag;
}
}
import java.io.*;
公共类记分器{
公共静态void main(字符串[]args){
BufferedReader reader=null;
试一试{
串电流线;
reader=newbufferedreader(newfilereader(“QuizScores.txt”);
while((currentLine=reader.readLine())!=null){
整数和=0;
字符串[]nums=currentLine.split(\\s+);
对于(int i=0;i