윤성필
윤성필

Reputation: 85

read input file to linked list's node error

I'm trying to do is print the data(variable coefficient and degree) in linked list node.

But Somethings wrong when I read a line in input.txt file. the content of input.txt file is below

2
2 0
3 0
2
-2 0
3 4

But My code fails to read this, printing an error message like java.lang.NumberFormatException: For input string: " 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);
}
}

Thanks in advance

Upvotes: 0

Views: 108

Answers (2)

Tokazio
Tokazio

Reputation: 530

@Zeus answer

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

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

Or continue reading with the scanner

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

Upvotes: 0

Zeus
Zeus

Reputation: 2253

java.lang.NumberFormatException: basically means that the input that you're trying to parse as an int is not really an int. Also I'd use the Scanner API to read input which is simpler and takes less code.

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

If instead you wanted a String you could use

String firstLine = sc.nextLine();

Now keep fetching inputs using,

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

Also in the section where you're parsing a line of string into ints divider, coefficient and degree using substring, that can be done a lot easier.

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

Now you can use

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

Upvotes: 2

Related Questions