繁体   English   中英

使用 Java arraylist 存储来自文件扫描的数据

[英]using Java arraylist for storing data from scan from file

我是 Java 新手,但不会编码。 我试图弄清楚 java 因为它是我这个学期的课程的一部分,我在掌握它的想法和在 java 中实现事物时遇到了一个非常困难的问题。

我的问题是我不确定我是否正确使用数组列表从文件扫描中获取数据并将其输入到数组列表中以便稍后进行排序和打印。 我只是在学习 Java 时遇到问题,因为我是 Java 新手,所以任何帮助都会很棒。

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.*;
public class MissionCount
{
    private static  ArrayList<String> list = new ArrayList<String>();
    // returns an InputStream that gets data from the named file
    private static InputStream getFileInputStream(String fileName) throws Exception {
        InputStream inputStream;
        try {
            inputStream = new FileInputStream(new File(fileName));
        }
        catch (FileNotFoundException e) {       // no file with this name exists
            inputStream = null;
            throw new Exception("unable to open the file -- " + e.getMessage());
        }
        return inputStream;
    }

    public static void main(String[] args) {
        if (args.length != 1) {
            System.out.println("USage: MissionCount <datafile>");
            //System.exit(1);
        }

        try {
            System.out.printf("CS261 - MissionCount - Chad Dreher%n%n");
            int crewcount = 0;
            int misscount = 0;
            InputStream log = getFileInputStream(args[0]);
            Scanner sc = new Scanner(log);
            sc.useDelimiter(Pattern.compile(",|\n"));

            while (sc.hasNext()) {
                String crewMember = sc.next();
                list.add(crewMember);
                String mission = sc.next();
                list.add(mission);
             }
             sc.close();
             // Add code to print the report here
         }catch (Exception e) {
             System.out.println("Error: " + e.getMessage());
         }
    }
}
InputStream log = getFileInputStream(args[0]);

将该行更改为如下:-

File log = new File(args[0])

那应该工作!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM