使用扫描程序读取文件和组数据

时间:2014-11-09 13:38:57

标签: java text file-io text-files filestream

我使用扫描仪来读取文件和分组数据,我已经做了一些工作但在遇到问题时面临问题

数据集为this

您可以看到一个示例here,它解释了所有内容。

我所做的就是:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

公共类TextScanner {

// static File f = new File("nrp1.txt");

public static void main(String[] args) throws FileNotFoundException {
    TextScanner ts = new TextScanner();

    ts.readfile();

}

public void readfile() throws FileNotFoundException {

    FileInputStream fis = new FileInputStream("nrp1.txt");
    Scanner scanner = new Scanner(fis);
    int lineCount = 0;

    String firstline = scanner.nextLine();
    String secondline = scanner.nextLine();
    String thirdline = scanner.nextLine();
    String fourthline = scanner.nextLine();
    String fifthline = scanner.nextLine();
    String sixthline = scanner.nextLine();
    String seventhline = scanner.nextLine();
    String eighththline = scanner.nextLine();
    List<Integer> intArrRIDA = new ArrayList<Integer>();
    List<Integer> intArrRIDB = new ArrayList<Integer>();
    int[] intRIDA = new int[Integer.parseInt(eighththline)];
    int[] intRIDB = new int[Integer.parseInt(eighththline)];
    List<Integer> intArrPOC = new ArrayList<Integer>();
    List<Integer> intArrNORBC = new ArrayList<Integer>();
    List<Integer> intArrRL = new ArrayList<Integer>();
    String NoC="";

    // COR arrays
    thirdline.trim();
    String[] CoR1 = thirdline.split(" ");

    int[] intCoR1 = new int[CoR1.length];

    for (int i = 0; i < intCoR1.length; i++) {
        intCoR1[i] = Integer.parseInt(CoR1[i]);
    }
    fifthline.trim();
    String[] CoR2 = fifthline.split(" ");

    int[] intCoR2 = new int[CoR2.length];

    for (int i = 0; i < intCoR2.length; i++) {
        intCoR2[i] = Integer.parseInt(CoR2[i]);
    }

    seventhline.trim();
    String[] CoR3 = seventhline.split(" ");

    int[] intCoR3 = new int[CoR3.length];

    for (int i = 0; i < intCoR3.length; i++) {
        intCoR3[i] = Integer.parseInt(CoR3[i]);
    }

    while (scanner.hasNextLine()) {
        lineCount++;
        String line = scanner.nextLine().trim();

        if (lineCount >= 1 && lineCount <= Integer.parseInt(eighththline)) {
            // System.out.println("line: " + line);
            String[] RID = line.split(" ");

            for (int i = 0; i < 1; i++) {
                intArrRIDA.add(Integer.parseInt(RID[0]));

                intArrRIDB.add(Integer.parseInt(RID[1]));

                // intRIDA[i] = Integer.parseInt(RID[0]);
                // intRIDB[i] = Integer.parseInt(RID[1]);
                // System.out.println("Id of RequirementA : " + intRIDA[i]);
                // System.out.println("Id of RequirementB : " + intRIDB[i]);
            }

        }
        if (lineCount == Integer.parseInt(eighththline)) {
            NoC = scanner.nextLine();
        }

            //System.out.println("Number of Customer : " + NoC);
            if (lineCount > Integer.parseInt(eighththline)) {
                // System.out.println("line: " + line);
                String[] Customers = line.split(" ");
                System.out.println("Details of Customer : " +Arrays.toString(Customers) );
                intArrPOC.add(Integer.parseInt(Customers[0]));

                intArrNORBC.add(Integer.parseInt(Customers[1]));


                for (int i = 0; i < 6; i++) {
                    intArrRL.add(Integer.parseInt(Customers[2]));
                    if (Customers[3] != null) {
                        intArrRL.add(Integer.parseInt(Customers[3]));
                    }
                    else if (Customers[4] != null) {
                        intArrRL.add(Integer.parseInt(Customers[4]));
                    }
                    else if (Customers[5] != null) {
                        intArrRL.add(Integer.parseInt(Customers[5]));
                    }
                    else if (Customers[6] != null) {
                        intArrRL.add(Integer.parseInt(Customers[6]));
                    }
                    else
                        continue;


                }

            }


    }

    System.out.println("Level of requirements, t: " + firstline);
    System.out.println("Number of requirements in Level 1: " + secondline);
    System.out.println("Costs of requirements in Level 1  : "
            + Arrays.toString(intCoR1));
    System.out.println("Number of requirements in Level 2: " + fourthline);
    System.out.println("Costs of requirements in Level 2  : "
            + Arrays.toString(intCoR2));
    System.out.println("Number of requirements in Level 3: " + sixthline);
    System.out.println("Costs of requirements in Level 3  : "
            + Arrays.toString(intCoR3));
    System.out.println("Number of dependencies: " + eighththline);
    // System.out.println("Id of RequirementA : " +
    // Arrays.toString(intRIDA));
    // System.out.println("Id of RequirementB : " +
    // Arrays.toString(intRIDB));
    System.out.println("Id of RequirementA : " + intArrRIDA);
    System.out.println("Id of RequirementB : " + intArrRIDB);
    System.out.println("Number of Customer : " + NoC);
    System.out.println("Profit of Customer : " + intArrPOC);
    System.out.println("Number of requests by Customer: " + intArrNORBC);
    System.out.println("Requirements List : " + intArrRL);

}

private boolean isNumeric(String number) {
    try {
        Integer.parseInt(number);
        return true;
    } catch (NumberFormatException e) {
        return false;
    }
}
}

但是你可以看到:

for (int i = 0; i < 6; i++) {
    intArrRL.add(Integer.parseInt(Customers[2]));
    if (Customers[3] != null) {
        intArrRL.add(Integer.parseInt(Customers[3]));
    }
    else if (Customers[4] != null) {
        intArrRL.add(Integer.parseInt(Customers[4]));
    }
    else if (Customers[5] != null) {
        intArrRL.add(Integer.parseInt(Customers[5]));
    }
    else if (Customers[6] != null) {
        intArrRL.add(Integer.parseInt(Customers[6]));
    }
    else
        continue;               
}

这部分不正确,我需要将它们与客户的利润和客户的请求数量相关联,当我实施更多的东西时,我可以找到相应的需求清单和客户利润的请求数量...

我还没有开始使用这种方法,但如果你能帮助我,那就太棒了!

谢谢!

0 个答案:

没有答案