Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将文本文件读入Java数组_Java_Arrays_Text - Fatal编程技术网

将文本文件读入Java数组

将文本文件读入Java数组,java,arrays,text,Java,Arrays,Text,所以我试图从一个文本文件中获取一系列“分数”放入一个数组,然后按顺序排序,四行,然后编写其他方法以获得最高、最低、平均值等。println命令在其中,但我还没有编写这些方法。我已经工作了一整天,我开始困惑自己,现在我在main方法中得到了一个NullPointerException错误。有什么帮助吗 package arrayops1d; import java.io.*; import java.util.*; public class ArrayOps1D { static i

所以我试图从一个文本文件中获取一系列“分数”放入一个数组,然后按顺序排序,四行,然后编写其他方法以获得最高、最低、平均值等。println命令在其中,但我还没有编写这些方法。我已经工作了一整天,我开始困惑自己,现在我在main方法中得到了一个NullPointerException错误。有什么帮助吗

package arrayops1d;

import java.io.*;
import java.util.*;

public class ArrayOps1D {

    static int scores[];
    public static void main(String[] args) throws Exception{
        FileReader file = new FileReader("C:/Users/Steve/Documents/"
                + "NetBeansProjects/ArrayOps1D/Scores.txt");
        BufferedReader reader = new BufferedReader(file);

        String scores = "";
        String line = reader.readLine();
        while (line != null){
            scores += line;
            line = reader.readLine();
        }


        System.out.println(scores);
        System.out.println(getTotal());
        System.out.println(getAverage());
        System.out.println(getHighest());
        System.out.println(getLowest());
        System.out.println(getMedian());
        System.out.println(getPosition());
        System.out.println(getDeviations);
        System.out.println(getStdDev);


}

代码的第一个问题: 如果您的程序希望在不同的平台上运行,则必须在文件路径中使用
/
,而不是使用
\
或更好的
file.separator

如果没有,您将拥有
java.io.FileNotFoundException

您正在逐行读取,因此可以使用
split
函数和
Integer.paraseInt
Float.parseFloat
转换每个拆分的元素并添加到数组中


  • 这里有一种方法,您可以使用a和a将文件中的
    int
    值读入
    Integer
    数组-

    Integer[]分数=null;
    File File=新文件(“C:/Users/Steve/Documents/”
    +“NetBeansProjects/ArrayOps1D/Scores.txt”);
    if(file.exists()&&file.canRead()){
    试一试{
    List al=新的ArrayList();
    扫描仪=新扫描仪(文件);
    while(scanner.hasNext()){
    if(scanner.hasNextInt()){
    al.add(scanner.nextInt());
    }否则{
    System.out.println(“不是int:+scanner.next());
    }
    }
    分数=al.toArray(新整数[al.size()]);
    }catch(filenotfounde异常){
    e、 printStackTrace();
    }
    }否则{
    System.out.println(“找不到文件:+file.getPath());
    }
    如果(分数!=null){
    System.out.println(“分数读取:“+Arrays.toString(分数));
    }
    
    你能上传你的文件内容吗?我不明白你的问题是什么?你能告诉我们是怎么回事吗?我在这里看不到太多的代码,看起来你想把整个文件读入一个
    字符串中。为什么?您能发布您收到的错误消息吗?由于看到的代码有限,似乎scores[]变量未初始化。否。你可以跨平台使用“/”。真的吗?为什么它对我不起作用?是的。你必须给出一个例子。另请参见答案。当OP向他或她的老师展示此代码时,老师会如何接受?给出答案是不对的。只要暗示一下把OP带到正确的地方,我相信你会同意的me@KickButtowski再次阅读OP的问题。除了将
    int
    (s)读入数组之外,这个任务还有很多。而OP实际发布的代码似乎至少尝试过它。要么就是这样,要么就是演示如何解析
    字符串中的所有
    int
    ,坦率地说,我认为这个
    扫描器
    示例非常简单。我一直很欣赏你,但我相信你可以把它作为一个提示。因为老师会发疯的,不会写函数的新手怎么会这样。如果你读了这个问题,op很难写出简单的高分函数。你的代码对op来说太超前了,不能帮助他
    Integer[] scores = null;
    File file = new File("C:/Users/Steve/Documents/"
            + "NetBeansProjects/ArrayOps1D/Scores.txt");
    if (file.exists() && file.canRead()) {
        try {
            List<Integer> al = new ArrayList<>();
            Scanner scanner = new Scanner(file);
            while (scanner.hasNext()) {
                if (scanner.hasNextInt()) {
                    al.add(scanner.nextInt());
                } else {
                    System.out.println("Not an int: " + scanner.next());
                }
            }
            scores = al.toArray(new Integer[al.size()]);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    } else {
        System.out.println("Can't find file: " + file.getPath());
    }
    if (scores != null) {
        System.out.println("Scores Read: " + Arrays.toString(scores));
    }