2013-04-01 48 views
-1

我有兩個文件。一個文件計算我在文本文件中列出的事件數量,並將事件數量存儲到變量「count」中。然後我想使用這個變量中的值在第二個文件中進行計算。我該怎麼做呢?我是否必須在第一個文件中創建該類的對象,然後引用它?我需要一個例子,我似乎無法得到這個工作。這是我嘗試過的。如何使用存儲在位於單獨文件中的變量中的數據?

我的第一個文件:

import java.util.*; 
import java.io.*; 
    public class EventCounter { 
     public static void main (String [] args) throws IOException{ 

      Scanner file = new Scanner(new File("event.txt")); 
      int count = 0; 
      while (file.hasNextLine()) { 
       count++; 
       file.nextLine(); 
      } 

      System.out.println(count); //test 
     } 
    } 

我的第二個文件:

import java.io.IOException; 
import java.io.FileReader; 
import java.io.BufferedReader; 

public class ReadEventFile { 

    private String path; 
    public ReadEventFile(String file) { 
     path = file; 
    } 

    public String[] OpenFile() throws IOException { 
     FileReader fr = new FileReader(path); 
     BufferedReader textReader = new BufferedReader(fr); 

     EventCounter method = new EventCounter(); //make object? 
     String[] dataTable = new String[count]; 

     int i; 
     for (i=0; i<count; i++) { //Why count does not exist? 
     } 

我的第二個文件不知道那算不算是我的第一個文件的變量:-(

+0

愚人節.......... – Zelldon

回答

1

您似乎讓您的流程向後流動。帶有main方法的類將由JVM創建並運行 - 因此它是您的入口點。

因此,您的ReadEventFile類需要在創建時告知count。只需將它添加到構造:

public static class ReadEventFile { 

    private final File eventFile; 
    private final int count; 

    public ReadEventFile(final int count, final File eventFile) { 
     this.eventFile = eventFile; 
     this.count = count; 
    } 

    public String[] openFile() throws IOException { 
     String[] dataTable = new String[count]; 
     int i; 
     for (i = 0; i < count; i++) { 
     } 
     return dataTable; 
    } 
} 

現在你EventCounter需求一旦知道count創建ReadEventFile實例,並調用其openFile方法:

public static void main(String[] args) throws IOException { 

    final File eventFile = new File("event.txt"); 
    int count = 0; 
    try (Scanner file = new Scanner(eventFile)) { 
     while (file.hasNextLine()) { 
      count++; 
      file.nextLine(); 
     } 
    } 
    final ReadEventFile readEventFile = new ReadEventFile(count, eventFile); 
    final String[] dataTable = readEventFile.openFile(); 
} 

ReadEventFile做它的工作,然後將String[]返還給您的EventCounter

當你完成它們後,你不會關閉任何資源。這是在尋求麻煩。我已在EventCounterScanner附近添加了Java 7試用資源。

這個程序的設計看起來有點奇怪。爲什麼EventCounter應該是應用程序的入口點沒有合乎邏輯的原因。我建議您創建一個BootStrap類,該類包含main方法,並且是入口點,然後調用EventCounterReadEventFile

此外,ReadEventFile類中的openFile方法沒有很好的名稱 - 它不止於此。也許processEventFile或者沿這些方向的東西會更合適。

+0

我會努力的。對不起,這是我編程的第二天。我只是想要處理事情。我對這種混亂表示抱歉。 – portfoliobuilder

0

你的第一個程序

package farzi; 

import java.util.*; 
import java.io.*; 
    public class EventCounter { 
     public static void main (String [] args) throws IOException 
     { 
      EventCounter object = new EventCounter(); 
      System.out.println(object.returnCount()); 
     } 

     public int returnCount() throws FileNotFoundException 
     { 
      Scanner file = new Scanner(new File("event.txt")); 
      int count = 0; 
      while (file.hasNextLine()) { 
       count++; 
       file.nextLine(); 
      } 
      System.out.println(count); //test 
      return count; 
     } 
    } 

您的第二個程序

package farzi; 
import java.io.File; 
import java.io.IOException; 
import java.io.FileReader; 
import java.io.BufferedReader; 

public class ReadEventFile 
{ 

    private String path; 
    public ReadEventFile(String file) 
    { 
     String path = file; 
    } 

    public String[] OpenFile() throws IOException { 

     EventCounter eventCounterObject = new EventCounter(); 
     int countLocal = eventCounterObject.returnCount(); 

     FileReader fr = new FileReader(path); 
     BufferedReader textReader = new BufferedReader(fr); 

     EventCounter method = new EventCounter(); //make object? 
     String[] dataTable = new String[countLocal]; 

     int i; 
     String[] textData = null; 
     for (i=0; i<countLocal; i++) { //Why count does not exist? 

      textData[i] = textReader.readLine(); 
     } 
     return textData; 

    } 

} 
+0

這是很奇怪的 - 它應該如何工作。應用程序啓動,調用'main'。那又怎麼樣?其他類('ReadEventFile')甚至從哪裏調用。 –

+0

我想我現在看到過程是如何工作的。謝謝,通過這個例子,我可以繼續。 – portfoliobuilder

+0

@ bmorris591:我已經調用第一類的對象來使用返回計數器方法的第二類,並且主要方法僅用於在您執行第一類檢查的情況下的演示 –

相關問題