从.txt文件中读取列

时间:2018-01-14 10:26:03

标签: java

我有一个文本文件(archive.txt),它有9列数据,由一个标签分隔。我想读取列并执行简单的数学运算。

在下面的示例中,我想通过在cData列中添加高(3)或无限(4)的所有策略然后除以高和无限的总和来找到平均成本(iCost)。高和无限分别由归档文件中的3和4表示。

有两个System.out.println()编号为1和2.它们用于查看程序的位置。它没有超过第一个System.out.println。

public int highUnlimited() {
    Scanner input = new Scanner(System.in);
    input = new Scanner("archive.txt");
    int iLargeBundle = 0;
    int iCost = 0;

    System.out.println("1");
    String cDate = input.next();
    int cMinutes = input.nextInt();
    int cData = input.nextInt();
    int cLength = input.nextInt();
    boolean cIntCalls = input.nextBoolean();
    String cReference = input.next();
    int cCostPerMonth = input.nextInt();
    String cFirstName = input.next();
    String cSecondName = input.next();

    System.out.println("2");
    if (cData == 3 || cData == 4) {
        iLargeBundle = iLargeBundle++;
        iCost = iCost + cCostPerMonth;
    }

    int iTotal = iLargeBundle / iCost;
    return iTotal;
}

这些是Archive文件的前两行。他们通常没有标题

2016年9月15日2 1 12 N MT230N 617 C Clark

2016年10月25日1 1 12 N ED641N 475 Z Clark

2 个答案:

答案 0 :(得分:0)

input = new Scanner("archive.txt");

这会在字符串"archive.txt"上打开扫描程序,而不是具有该名称的文件。

如果您想扫描文件,则需要执行以下操作:

input = new Scanner(new File("archive.txt"));

答案 1 :(得分:0)

在这里,你的代码被重写了评论,告诉你如何去做,我已经添加了一个循环,以防你想要读取你能够做到的文件的所有行(在我添加的那个循环中)方法hasNextLine(),如果有一行next和nextLine()则返回true,它跳转到下一行)。导入java.io. ;这一点很重要;导入类文件并导入java.util。;导入该类Scanner。我希望我能帮到你。

public int highUnlimited(){
try{ //you all ways need to try catch the Exceptions when you use Scanner and File.

          File f = new File("path");      //We need to create first the file object this is 
                                          //done with the constructor File(String path)

          Scanner input = new Scanner(f); //You dont need the Scanner.in, the Scanner.in 
                                          //scanns input data from the terminal and we want
                                          //to scann it from a File. Th constructor 
                                          //Scanner(String path) that you used doesn't exist 
                                          //on java.
          int iLargeBundle = 0;
          int iCost = 0;
          String cDate, cReference, cFirstName, cSecondName; // I declare the variables outside so there
          int cMinutes, cData, cLength, cCostPerMonth;       //is no problem with the while
          boolean cIntCalls;
          int iTotal = 0; //I've used 0 as default value for iTotal 

          System.out.println("q");
      while(input.hasNextLine()){ //I use hasNextLine in case you have various lines of data in your file

                  cDate = input.next();
                  cMinutes = input.nextInt();
                  cData = input.nextInt();
                  cLength = input.nextInt();
                  cIntCalls = input.nextBoolean();
                  cReference = input.next();
                  cCostPerMonth = input.nextInt();
                  cFirstName = input.next();
                  cSecondName = input.next();
                  input.nextLine(); //To jump a line so it doesn't block on the while

              if (cData == 3 || cData ==4){
                  iLargeBundle++; //if you only want to add 1 to the variable with this is enough
                  iCost = iCost + cCostPerMonth;
              }
            }

              iTotal = iLargeBundle/iCost; //as iCost is = if it hasn't been modified here is going
                                               //to show an error because you can't divide by 0 so you have
                                               //to change this in cas iCost is 0
            }
            catch(Exception e){
              System.out.println(e.getMessage());
            } 
    return iTotal;
}