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

时间:2016-03-26 11:46:05

标签: java file linked-list

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

但是当我在input.txt文件中读取一行时,Somethings错了。 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);
}
}

提前致谢

2 个答案:

答案 0 :(得分:2)

form1.FindControl("txtrelat1")基本上意味着您尝试解析为int的输入实际上不是int。 此外,我还使用扫描仪API来读取更简单且占用更少代码的输入。

java.lang.NumberFormatException:

如果你想要一个String,你可以使用

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

现在继续使用

获取输入
String firstLine = sc.nextLine();

此外,在您使用子字符串将一行字符串解析为ints divider,coefficient和degree的部分中,可以轻松完成。

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

现在你可以使用

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

答案 1 :(得分:0)

@Zeus回答

@IBAction func DeletePressed(sender: UIButton) {
    let deleteIndices = getIndexToDelete()
    navigationController?.popViewControllerAnimated(true)
    return;
    [...]

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

或继续使用扫描仪阅读

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