eclipse - Java- reading from .data files -
i'm having couple of problems setting system read data java program 2 .data files...
i'm using eclipse ide, , have created project in folder 2 .data files want use. i've started project, still @ beginning...
the 2 .data files are: car.data , owner.data, , have start project.
i've created few classes: owner.java, car.java , readfile.java (to read data .data files).
at present, owner.java file looks this:
import java.io.*; public class owner { public static void main(string[] args) throws ioexception { // todo auto-generated method stub readfile rf = new readfile("owner.data"); rf.read("owner.data"); } file f; public string id; public string lastname; public string firstname; public string street; public string city; public void readowner() throws filenotfoundexception{ //file f = new file("smart stream associate software engineer (java) - bristol/assessment/src/owner.java"); //inputstream = new fileinputstream(f); } }
my car.java file looks this:
public class order { public string orderid; public string orderno; public string personid; }
and readfile.java file looks this:
import java.io.*; public class readfile { string[] columns = new string[]{"personid", "lastname", "firstname", "street", "city"}; string[] data = new string[100]; public void read() throws ioexception{ filereader fr = new filereader("person.data"); bufferedreader br = new bufferedreader(fr); string line; int = 0; while((line = br.readline())!= null){ data[i] = line; system.out.println(data[i]); i++; } br.close(); string[] datanew = new string[i]; system.arraycopy(data, 0, datanew, 0, i); data = datanew; system.out.println("data length: " + data.length); } }
currently, when try run program owner.java class (as that's main method is), i'm getting exception says:
exception in thread "main" java.lang.error: unresolved compilation problems: constructor readfile(string) undefined method read() in type readfile not applicable arguments (string)
the line it's complaining line:
readfile rf = new readfile("owner.data");
could point out me why i'm getting exception, , i've forgotten avoid getting it? many in advance.
edit 25/09/2013
so, i've tried editing code reflect changes suggested @sushain97 below, , have 'owner.java class looks this:
import java.io.*; public class person { public static void main(string[] args) throws ioexception { // todo auto-generated method stub readfile rf = new readfile("owner.data"); rf.read(); } file f; public string id; public string lastname; public string firstname; public string street; public string city; public void readperson() throws filenotfoundexception{ //file f = new file("smart stream associate software engineer (java) - bristol/assessment/src/person.java"); //inputstream = new fileinputstream(f); } }
and readfile.java class looks this:
import java.io.*; public class readfile { //file file; string[] columns = new string[]{"personid", "lastname", "firstname", "street", "city"}; string[] data = new string[100]; private file file; public readfile(string filename){ this.file = new file(filename); } public void read() throws ioexception{ filereader fr = new filereader(file); bufferedreader br = new bufferedreader(fr); string line; int = 0; while((line = br.readline())!= null){ data[i] = line; system.out.println(data[i]); i++; } br.close(); string[] datanew = new string[i]; system.arraycopy(data, 0, datanew, 0, i); data = datanew; system.out.println("data length: " + data.length); } }
however, when run code owner.java class, i'm getting error says:
exception in thread "main" java.io.filenotfoundexception: owner.data (the system cannot find file specified)
and
at java.io.fileinputstream.open(native method) @ java.io.fileinputstream.<init>(unknown source) @ java.io.filereader.<init>(unknown source) @ readfile.read(readfile.java:15) @ person.main(owner.java:8)
i assume means cannot find 'owner.data' file- file stored in same folder 'owner.java' , 'readfile.java' classes stored... ideas why cannot find file, , how can ensure does?
edit 25/09/2013 @ 09:45
i've edited code show changes suggested planetsaro in answer, understand them, have:
import java.io.*; import java.util.scanner; public class readfile { static file file; string[] columns = new string[]{"personid", "lastname", "firstname", "street", "city"}; string[] data = new string[100]; private static void readfile(string filename){ try{ file file = new file("person.data"); scanner scanner = new scanner(file); while(scanner.hasnextline()){ system.out.println(scanner.nextline()); } scanner.close(); } catch (filenotfoundexception e){ e.printstacktrace(); } } public void read(file file2) throws ioexception{ filereader fr = new filereader("person.data"); bufferedreader br = new bufferedreader(fr); string line; int = 0; while((line = br.readline())!= null){ data[i] = line; system.out.println(data[i]); i++; } br.close(); string[] datanew = new string[i]; system.arraycopy(data, 0, datanew, 0, i); data = datanew; system.out.println("data length: " + data.length); } }
but i'm still getting error says exception in thread "main" java.io.filenotfoundexception: owner.data (the system cannot find file specified)
i don't understand why is?
edit 25/09/2013 @ 10:35
ok, can't seem working of answers have been given far (that may because don't understand answers- i've commented on them effect, if case, please explain them more (or basically- beginner).
however, have managed reduce amount of errors being displayed in console when run program. 2 classes this:
readfile.java:
import java.io.*; import java.util.scanner; public class readfile { static file file; string[] columns = new string[]{"personid", "lastname", "firstname", "street", "city"}; string[] data = new string[100]; private static void readfile(file){ try{ file file = new file("d:\\users\\elgan frost\\desktop\\careers\\smart stream associate software engineer (java) - bristol\\assessment\\srcperson.data"); scanner scanner = new scanner(file1); while(scanner.hasnextline()){ system.out.println(scanner.nextline()); } scanner.close(); } catch (filenotfoundexception e){ e.printstacktrace(); } } public void read(file file2) throws ioexception{ filereader fr = new filereader("d:\\users\\elgan frost\\desktop\\careers\\smart stream associate software engineer (java) - bristol\\assessment\\srcperson.data"); bufferedreader br = new bufferedreader(fr); string line; int = 0; while((line = br.readline())!= null){ data[i] = line; system.out.println(data[i]); i++; } br.close(); string[] datanew = new string[i]; system.arraycopy(data, 0, datanew, 0, i); data = datanew; system.out.println("data length: " + data.length); } }
person.java:
import java.io.*; public class person { public static void main(string[] args) throws ioexception { // todo auto-generated method stub readfile rf = new readfile(); rf.read(readfile.file); } static file f; public string id; public string lastname; public string firstname; public string street; public string city; public void readperson() throws filenotfoundexception{ //file f = new file("smart stream associate software engineer (java) - bristol/assessment/src/person.java"); //inputstream = new fileinputstream(f); } }
i getting 1 console error, says:
"exception in thread "main" java.lang.error: unresolved compilation problems: syntax error on token "file", variabledeclaratorid expected after token file cannot resolved type"
and complaining line 9 in readfile.java, line:
private static void readfile(file){
and line 7 in person.java, line:
readfile rf = new readfile();
anyone have ideas why is, , how can put right?
it because, readfile object not have constructor argument string type , method read() not have argument of type string passed in.
Comments
Post a Comment