gpt4 book ai didi

java - 读取文件信息并将其放入数组列表中

转载 作者:行者123 更新时间:2023-12-01 13:52:27 25 4
gpt4 key购买 nike

我正在尝试编写一个程序,在其中读取 2 个文件中的所有整数并将它们放入 2 个单独的数组列表中。然后我必须将列表合并在一起并对合并的列表进行排序。当我运行程序时,我不断收到以下错误,但我不明白为什么

java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

有谁知道我该如何解决这个问题?

import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;

public class ArraySort{
ArrayList<Integer> numberList = new ArrayList<Integer>();
ArrayList<Integer> numberList2 = new ArrayList<Integer>();
ArrayList<Integer> numberList3 = new ArrayList<Integer>();

public static void main(String[] args){
ArraySort x = new ArraySort();
x.merge();
x.splitList(9);
}
public void ArraySort(){
Scanner s = new Scanner(System.in);
try {
s = new Scanner (new File ("list1.txt")).useDelimiter("\\s+");
} catch(FileNotFoundException fnfe) {
System.out.println("file not found");
}

while (s.hasNext()) {
if (s.hasNextInt()) { // check if next token is an int
numberList.add(s.nextInt());
} else {
s.next(); // else read the next token
}
}
try {
s = new Scanner (new File ("list2.txt")).useDelimiter("\\s+");
} catch(FileNotFoundException fnfe) {
System.out.println("file not found");
}

while (s.hasNext()) {
if (s.hasNextInt()) { // check if next token is an int
numberList2.add(s.nextInt());
} else {
s.next(); // else read the next token
}
}

}
public void ArraySort(String x, String y){
Scanner s = new Scanner(System.in);
try {
s = new Scanner (new File (x)).useDelimiter("\\s+");
} catch(FileNotFoundException fnfe) {
System.out.println("file not found");
}

while (s.hasNext()) {
if (s.hasNextInt()) { // check if next token is an int
numberList.add(s.nextInt());
} else {
s.next(); // else read the next token
}
}
try {
s = new Scanner (new File (y)).useDelimiter("\\s+");
} catch(FileNotFoundException fnfe) {
System.out.println("file not found");
}

while (s.hasNext()) {
if (s.hasNextInt()) { // check if next token is an int
numberList2.add(s.nextInt());
} else {
s.next(); // else read the next token
}
}
}
public void bubbleSort() {
for (int pass = 0; pass < numberList.size()-1; pass++) {
for (int i = 0; i < numberList.size()-1; i++) {
if (numberList.get(i) > numberList.get(i+1)) {
int temp = numberList.get(i);
numberList.set(i,numberList.get(i+1));
numberList.set(i+1,temp);
}
}
}
}
public void merge(){
int currentPosition = 0;

for( int i = 0; i < numberList.size(); i++) {
numberList3.set(currentPosition, numberList.get(i));
currentPosition++;
}
for( int j = 0; j < numberList2.size(); j++) {
numberList3.set(currentPosition, numberList.get(j));
currentPosition++;
}
}
public void splitList(int x){
int count1 = 0;
int count2 = 0;

for(int i=0;i<numberList.size();i++){
if(numberList.get(i)>=x){
numberList2.set(count1,numberList.get(i));
count1++;
}
else{
numberList3.set(count2,numberList.get(i));
count2++;
}
}
System.out.println();
for (int i = 0; i < numberList2.size(); i++){
System.out.print(numberList2.get(i) + " ");
}
System.out.println();
for (int i = 0; i < numberList3.size(); i++){
System.out.print(numberList3.get(i) + " ");
}
System.out.println();
}
}

最佳答案

应该是

public static void main

不是

public void main

(另外,恭喜您,您在莱斯大学的运行 Java 代码的系统中发现了一个错误。向他们报告!)

关于java - 读取文件信息并将其放入数组列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19868021/

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