问题:
创建 FractionClass 对象数组后,我尝试通过读取如下所示的 txtfile 为每个 Fraction 赋予分子和分母:
11 12
7 24
2 3
5 6
7 9
...
第一个数字是分子,第二个是分母,为此我使用 Scanner 类。在 readFractions 方法下,我在 NULLpointerexception 所在的位置放置了注释。
主文件:
package labo2oef5;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Labo2Oef5 {
private static Breuk [] fractionArray;
private static int amountOfLines;
public static void main(String[] args) throws FileNotFoundException {
lengteBestand(); // stel amountOfLines al in
readFractions();
System.out.println(fractionArray[4].getNumerator()+" / "+ fractionArray[4].getDenominator());`
}
public static void readFractions() throws FileNotFoundException{
Scanner lezer = new Scanner(new File("breuken01.txt"));
fractionArray = new Breuk[amountOfLines];
for(int i=0; i<amountOfLines;i++){
double numerator = (double) lezer.nextInt();
double denominator = (double) lezer.nextInt();
fractionArray[i].setNumerator(numerator); // HERE IT GOES WRONG, THE NULLPOINTEREXCEPTION IS HERE
fractionArray[i].setDenominator(denominator);// THIS LINE SUFFERS AS WELL FROM THE NULLPOINTEREXCEPTION
lezer.nextLine();
}
}
public static int lengteBestand() throws FileNotFoundException{
Scanner lezer = new Scanner(new File("breuken01.txt"));
amountOfLines=0;
while(lezer.hasNextLine()){
amountOfLines++;
lezer.nextLine();
}
return amountOfLines;
}
}
Breuk(是分数的荷兰语)类:
package labo2oef5;
public class Breuk {
private double numerator, denominator;
public Breuk() {
numerator = 0;
denominator = 1;
}
public Breuk(double numerator, double denominator) {
this.numerator = numerator;
this.denominator = denominator;
}
@Override
public String toString() {
String breuk = numerator + "/" + denominator;
if (numerator == 0) {
breuk = "0";
}
return breuk;
}
private static double grootsteGemeneDeler(double a, double b) {
if (a < 0 || b < 0) {
return grootsteGemeneDeler(Math.abs(a), Math.abs(b));
}
if (b == 0) {
return a;
}
if (a < b) {
return grootsteGemeneDeler(b, a);
}
if (a % b != 0) {
return grootsteGemeneDeler(b, a % b);
} else {
return b;
}
}
public String normaliseer() {
double numerator2 = numerator / (grootsteGemeneDeler(numerator, denominator));
double denominator2 = denominator / (grootsteGemeneDeler(numerator, denominator));
return numerator2 + "/" + denominator2;
}
public double decimaleVorm() {
double decim = (double) numerator / denominator;
System.out.println(decim);
return decim;
}
public boolean isGroterDan(Breuk br) {
if (this.decimaleVorm() > br.decimaleVorm()) {
return true;
} else {
return false;
}
}
public void vermeerderBreuk(Breuk br) {
this.numerator = this.numerator * br.denominator + this.denominator * br.numerator;
this.denominator = this.denominator * br.denominator;
}
public void produkt(Breuk br, Breuk breu) {
numerator = br.numerator * breu.numerator;
denominator = br.denominator * breu.denominator;
}
public void setNumerator(double numerator) {
this.numerator = numerator;
}
public void setDenominator(double denominator) {
this.denominator = denominator;
}
public double getNumerator() {
return numerator;
}
public double getDenominator(){
return denominator;
}
}
两个答案都是正确的答案,我只需要在他们的答案下面添加这段代码: if (lezer.hasNextLine()) { lezer.nextLine(); }