gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:46:39 24 4
gpt4 key购买 nike

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

但是当我读取 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。另外,我会使用 Scanner API 来读取输入,这更简单并且需要更少的代码。

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

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

String firstLine = sc.nextLine();

现在继续使用获取输入,

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

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

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

现在您可以使用

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

关于java - 读取输入文件到链表的节点错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36234890/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com