java - How to use data stored in a variable located in a seperate file? -


i have 2 files. 1 file counts number of listed events have in text file , stores number of events variable "count". want use value in variable computation in second file. how do this? have create object of class in first file , reference it? need example please, cannot seem work. here have tried.

my first file:

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         }     } 

my second file:

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;         (i=0; i<count; i++) { //why count not exist?         } 

my second file not know count variable first file :-(

you seem have process flow backwards. class main method created , run jvm - therefore it's entry point.

your readeventfile class therefore needs told count when created. add constructor:

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;         (i = 0; < count; i++) {         }         return datatable;     } } 

now eventcounter needs create readeventfile instance once knows count , call openfile method on it:

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(); } 

the readeventfile it's work , returns string[] eventcounter.

you don't close of resources when done them. asking trouble. have added java 7 try-with-resources around scanner in eventcounter.

the design of program seem little odd. there no logical reason why eventcounter should entry point application. recommend create bootstrap class holds main method , entry point calls both eventcounter , readeventfile.

further, openfile method on readeventfile class isn't named - more that. maybe processeventfile or along lines more appropriate.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -