簡體   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