java - Why arent my Student strings printing out? -
I have three small java classes, and I'm trying to read some data from a file and print it . These are my codes, and they are very few.
Students.Java
Package students; Import java.text *; Public class student {name of private string; Personal gaps ages; Private Double GPA; Public student (string name, int era, double GPA) {this.name = name; This.age = age; This.gpa = GPA; } Public string toString () {string gpa_string = new decimalFormat ("####). Format (GPA); Name of return + "+ + + integer .strosting (age) +" + + gpa_string + "\ n"; }}
student.java
package students; Import java.util. *; Import java.io * *; Public class students {public fixed final int MAX = 100; Private student student []; Private count; Public Zero Read File (string path) throws IOException {students = new student [Max]; Scanner X = new scanner (new file (path)); Calculation = 0; While (x.hasNextLine () & amp; amp;; & lt; MAX) {string name = x.nextLine (); If ("". Par (name)) {System.out.println ("Exit the loop"); break; } System.out.println (name); Int age = x.nextInt (); Println (age); Double GPA = x.nextDouble (); Println (GPA); Students [math] = new students (name, age, gpa); Calculation + = 1; System.out.println (calculation)} x.close (); } Public string toString () {string result = ""; For (int i = 0; i
driver. Java
Package students; Import java.io * *; Public class driver {public static zero main (string [] args throws IOException {Student student = new student (); Students.readFile ("Students.txt"); System.out.print (students.toString ()); }}
And this is what students.txt looks like:
Name 22 22 Name 1 22 2.71 Name 2 19 3.51
I hope to see: Name 22 22 Name 1 22 2.71 Name 2 19 3.51
But instead I got no output. I'm not sure what the file is going on? I'm coming from a dragon, and it looks like try: ... except:
deal with
throws IOException
stuff.
I am using Eclipse, and I have student tax at the top of the src folder, bin folder and workspace because I do not know where it should go.
Edit:
After changing the conditions of the loop and adding a group of print statement, I read it a student. The output is now: Name0 22 1.2 1 Exit the loop name 0 22 1.20
I do not understand why it is stopping after 1 loop.
The following condition:
x.hasNextLine () & amp; Amp; X.hasNextInt () & amp; Amp; X.hasNextDouble () & amp; Amp; Count & lt;
andtwice
and < / Code> How can be simultaneosly? Think of the format of the input file, maybe you can make it tab-delimited (or any other delimiter), a line with perception - a student? In this case,if x.hasNextLine
Comments
Post a Comment