简体   繁体   中英

Why arent my Student strings printing out?

I have three small java classes, and I am trying to read some data in from a file and print it out. Here are my codes, and they are pretty short.

Student.java

package students;

import java.text.*;

public class Student{
    private String name;
    private int age;
    private double gpa;

    public Student(String name, int age, double gpa){
        this.name = name;
        this.age = age;
        this.gpa = gpa;
    }

    public String toString(){
        String gpa_string = new DecimalFormat("#.##").format(gpa);
        return name + " " + Integer.toString(age) + " " + gpa_string + "\n";
    }
}

Students.java

package students;

import java.util.*;
import java.io.*;

public class Students {
    public static final int MAX = 100;
    private Student students[];
    private int count;

    public void readFile(String path) throws IOException{
        students = new Student[MAX];
        Scanner x = new Scanner(new File(path));
        count = 0;
        while (x.hasNextLine() && count < MAX){
            String name = x.nextLine();
            if("".equals(name)){
                System.out.println("Breaking out of loop");
                break;
            }
            System.out.println(name);
            int age = x.nextInt();
            System.out.println(age);
            double gpa = x.nextDouble();
            System.out.println(gpa);
            students[count] = new Student(name, age, gpa);
            count += 1;
            System.out.println(count)
        }
        x.close();
    }

    public String toString(){
        String result = "";
        for(int i = 0; i < count; ++i)
            result += students[i].toString();
        return result;
    }

Driver.java

package students;

import java.io.*;

public class Driver {
    public static void main(String[] args) throws IOException{
        Students students = new Students();
        students.readFile("Students.txt");
        System.out.print(students.toString());
    }
}

and this is what Students.txt looks like:

Name0
22
1.2
Name1
22
2.71
Name2
19
3.51

I'm expecting to see this: Name0 22 1.2 Name1 22 2.71 Name2 19 3.51

But instead I get no output at all. I am not sure what is going on. Is the file not being found? I am coming from python, and it seems like a try: ... except: pass sort of thing deal is going on with the throws IOException stuff.

I am using Eclipse, and I have Students.txt in the src folder, the bin folder, and at the top of the workspace because I don't know where it needs to go.

EDIT:

After changing the conditions of the while loop and adding a bunch of print statements, I got it to read a single student in. The output now is: Name0 22 1.2 1 Breaking out of loop Name0 22 1.20

I don't understand why it is stopping after 1 loop.

The following condition:

 x.hasNextLine() &&x.hasNextInt() && x.hasNextDouble() && count < MAX

will always evaluate to false . How can x have next Int and Double simultaneosly? Think about the format of input file. Maybe you can make it tab-delimited (or any other delimiter), with assumption one line - one Student? In such a case it's much easier to implement reading method with only one condition inside if x.hasNextLine

while (x.hasNextLine() &&x.hasNextInt() && x.hasNextDouble() && count < MAX)

Think about this conditional...

hasNextInt( ) and hasNextDouble( ) are mutually exclusive, so it will always return false.

Conceptually, understand that x.hasNexInt() doesn't mean " x has another int somewhere" , it means that the next element in x is an int.

Also, unrelated but: Generally, when you find yourself with a conditional statement like this (with more than one && , as a rule of thumb), you should ask yourself if you really can simplify that conditional by breaking it into multiple statements.

EDIT: (in response to your update) Try running it without the if("".equals(name)) clause. It's not immediately apparent to me why it's being triggered before it's expected to, but your output indicates that it is, and in any case this clause seems redundant: Your hasNextLine( ) conditional at the top already provides a mechanism for exiting the loop at the end of the dataset. There are situations where a secondary check like this is necessary, but ask yourself if you're in one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM