![](/img/trans.png)
[英]Getting a error for my Minecraft plugin and cant figure out how to solve it
[英]Cant figure out how to solve exception in java program
最終目標是創建一個程序,該程序讀取文本文件,然后確定文本文件中所有數字的最低,最高和平均值。 我的程序沒有錯誤,但是在運行時會引發以下錯誤:
Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:417)
at java.lang.Integer.parseInt(Integer.java:499)
at PrintWriter.main(PrintWriter.java:40)
我的代碼如下。 如果有人可以幫助我解決這個問題,將不勝感激!
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class PrintWriter
{
public static void main(String[] args)
{
System.out.println("Enter the name of the file you wish to open");
Scanner keyboard = new Scanner(System.in);
String fileName = keyboard.nextLine();
Scanner inputStream = null;
String[] numbers = new String[100];
int index = 0, count = 0, average = 0, total = 0;
String regex = "[0-9]+";
//Open and read text file
try
{
inputStream = new Scanner(new File(fileName));
}
catch(FileNotFoundException e)
{
System.out.println("Error opening the file " + fileName);
System.exit(0);
}
while (inputStream.hasNextLine())
{
String line = inputStream.nextLine();
numbers[index] = line;
index++;
count++;
}
int lowest = Integer.parseInt(numbers[0]);
int highest = Integer.parseInt(numbers[0]);
for (index = 0; index < numbers.length; index++)
{
if (Integer.parseInt(numbers[index]) <= lowest)
{
lowest = Integer.parseInt(numbers[index]);
}
else if (Integer.parseInt(numbers[index]) >= highest)
{
highest = Integer.parseInt(numbers[index]);
}
total = total + Integer.parseInt(numbers[index]);
count++;
}
average = Math.round(total / count);
System.out.println("\nThe average of all the numbers is " + average + ". The lowest number was " + lowest + ". The highest number was " + highest);
System.out.println("\nThe numbers are listed below");
for (index = 0; index < numbers.length; index ++)
{
if (numbers[index].matches(regex));
{
System.out.println(numbers[index]);
}
}
}
}
您的某些字符串可能不包含可解析的整數。 它們可能包含空格。 因此,使用trim
方法可以消除這種情況。
從javadoc :
公共字符串trim()
返回字符串的副本,省略前導和尾隨空格。
更改這部分代碼
int lowest = Integer.parseInt(numbers[0].trim());
int highest = Integer.parseInt(numbers[0].trim());
for (index = 0; index < numbers.length; index++)
{
if (Integer.parseInt(numbers[index].trim()) <= lowest)
{
lowest = Integer.parseInt(numbers[index].trim());
}
else if (Integer.parseInt(numbers[index].trim()) >= highest)
{
highest = Integer.parseInt(numbers[index].trim());
}
total = total + Integer.parseInt(numbers[index].trim());
count++;
}
當您將數組初始化為100個元素並且循環遍歷所有數組值(對於(index = 0; index <numbers.length; index ++))時,在某些時候,您將嘗試解析會導致異常的空值。 因此,循環應迭代直到計數而不是數組長度。 您不必在for循環內增加計數,因為您已經對上一個while循環中的所有元素進行了計數。 另外,您不必在循環中調用Integer.parseInt 5次,只需獲取一次該整數並將其存儲在int變量中,然后重用該變量即可:
for (index = 0; index < count; index++) {
int value = Integer.parseInt(numbers[index]);
if (value < lowest) {
lowest = value;
} else if (value > highest) {
highest = value;
}
total = total + value;
}
您的文本文件包含的行不是數字(帶有空格或字母),或者文件中沒有100個數字。
該文件未指定包含多少個數字。 因此,只有文件的字符串填充了數組的一部分。 這些被正確解析。 但是在某些時候,代碼到達了未用行填充的數組部分。 然后發生此調用: Integer.parseInt(someString)
,其中someString
為null
,從而導致NullPointerException
。 要解決此問題,您可以為讀取的行添加一個計數器,並使用該計數器代替numbers.length
,或者在從其解析Integer
之前檢查String
是否為null
。
Java代碼
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestProgram {
public static void main(String[] args) throws FileNotFoundException {
String line="";
ArrayList<Integer> arl = new ArrayList<Integer>();
Scanner inputStream = null;
String[] numbers = new String[100];
int index = 0, count = 0, average = 0, total = 0;
String regex = "[0-9]+";
// Open and read text file
try {
inputStream = new Scanner(new File("filename"));
} catch (FileNotFoundException e) {
System.out.println("Error opening the file "
+ "filename");
System.exit(0);
}
while (inputStream.hasNextLine()) {
line = inputStream.nextLine();
numbers[index] = line;
index++;
count++;
Pattern pattern = Pattern.compile("[0-9]+");
Matcher m = pattern.matcher(line);
while (m.find()) {
int n =Integer.parseInt(m.group());
System.out.println(n);
arl.add(n);
}
}
Collections.sort(arl);
System.out.println(arl);
int lowest = arl.get(0);
int highest = arl.get(arl.size()-1);
System.out.println("Lowest is: " + lowest );
System.out.println("highest is: " + highest );
total = arl.size();
Integer sum = 0;
for (Integer mark : arl) {
sum += mark;
}
int avg = sum /arl.size();
average = Math.round(total / count);
System.out.println("\nThe average of all the numbers is " + avg
+ ". The lowest number was " + lowest
+ ". The highest number was " + highest);
System.out.println("\nThe numbers are listed below");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.