将输入文件读取到链表的节点错误



我正在尝试打印链表节点中的数据(可变系数和度数)。

但是,当我读input.txt文件中的一行时,出现了一些错误。input.txt文件的内容低于

2
2 0
3 0
2
-2 0
3 4

但是我的代码无法读取,打印了一条错误消息java.lang.NumberFormatException:对于输入字符串:"01"


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class Assignment21 {
    public static void main(String[] args) {
    try {
        LinkedList polynomialN = new LinkedList();
        LinkedList polynomialM = new LinkedList();
        BufferedReader in = new BufferedReader(new FileReader("input.txt"));
        String firstEntryNum;
        firstEntryNum = in.readLine();
        int entryNum = Integer.valueOf(firstEntryNum); 
        for(int i = 0; i < entryNum; i++){ 
            String temp = in.readLine(); 
            int divider = temp.indexOf(" ");
            int coefficient = Integer.parseInt(temp.substring(0, divider));
            int degree = Integer.parseInt(temp.substring(divider) + 1); //지수 입력받기
            polynomialN.add(new Node(coefficient, degree));
        }
        String secondEntryNum;
        secondEntryNum = in.readLine();
        entryNum = Integer.valueOf(secondEntryNum);
        for(int i = 0; i < entryNum; i++){
            String temp = in.readLine();
            int divider = temp.indexOf(" "); 
            int coefficient = Integer.parseInt(temp.substring(0, divider)); //계수 입력받기
            int degree = Integer.parseInt(temp.substring(divider) + 1); //지수 입력받기
            polynomialM.add(new Node(coefficient, degree));
        }
        polynomialN.print();
        polynomialM.print();
        in.close();
        BufferedWriter out = new BufferedWriter(new FileWriter("output.txt"));
        out.close();
    } catch (Exception e) {
        System.err.println(e);
        System.exit(1);
        // TODO: handle exception
    }
}
}
class Node{
int coefficient;
int degree;
Node nextNode;
public Node(int coefficient, int degree) {
    this.coefficient = coefficient;
    this.degree = degree;
}
}
class LinkedList {
private Node head;
public void add(Node newNode){
    if (head == null)
        head = newNode;
    else {
        Node tail = head;
        while(tail.nextNode != null){
            tail = tail.nextNode;
        }   
        tail.nextNode = newNode;
    }
}
public void print() {
    StringBuffer sb = new StringBuffer();
    Node current = head;
    int size = 0;
    while (current != null){
        sb.append(current.coefficient);
        sb.append(" ");
        sb.append(current.degree);
        current = current.nextNode;
        size++;
    }
    System.out.println("[" + sb + "]");
    System.out.println("size: " + size);
}
}

提前感谢

java.lang.NumberFormatException:基本上意味着您试图解析为int的输入实际上不是int。此外,我会使用扫描仪API来读取输入,这更简单,占用更少的代码。

Scanner sc = new Scanner(System.in);
int firstLine = Integer.parseInt(sc.nextLine());

如果你想要一个字符串,你可以使用

String firstLine = sc.nextLine();

现在继续使用获取输入

while(sc.hasNextLine()){
   String something = sc.nextLine();
}

此外,在使用子字符串将一行字符串解析为int除法器、系数和度数的部分中,这可以更容易地完成。

String[] arr = something.split(" ");

现在你可以使用了

int divider = Integer.parseInt(arr[0])
int coefficient = Integer.parseInt(arr[1])
int degree = Integer.parseInt(arr[2])

@Zeus回答

int divider = Integer.parseInt(arr[0])

int coefficient=Integer.parseInt(arr[1])int degree=Integer.parseInt(arr[2])

或者使用扫描仪继续读取

//3 int on a row
int divider = scan.nextInt();
int coefficient = scan.nextInt();
int degree = scan.nextInt();
//next row
scan.nextLine();

相关内容

  • 没有找到相关文章

最新更新