简体   繁体   中英

Reading integer columns (Java) without using Arrays?

I'm stumped on how I can read 3 different columns from a text file in Java to use them to a "grading" scheme.

46813  5  100.00
80795  4  86.52
34961  3  77.52
40059  2  85.61
80463  1  70.16
64088  0  80.52
65400  6  90.23
80775  2  70.16
573S1  3  83.03
21076  4  54.47
32911  5  -10.90

The first column is the student ID, the second being extra credit as an integer (resembling a percent. 1 = 1%), and the third is the grade.

I have:

static String inputFileName = "gradeInput.txt";
static File inputFile = new File(inputFileName);

    public static void main(String[] args) throws IOException
    {
        //validateFile(inputFile);
        validateData(inputFile);
    }
    public static void validateData(File inputFile) throws IOException
    {
        Scanner dataID = new Scanner(inputFile);
        int studentID = dataID.nextInt();
    }

For the validateData method, I just read the first int (student ID), not sure how to read the rest.

**Edit: **I just figured out on reading into the next int after "studentID". If I do like

int studentID = dataID.nextInt();
int extraCredit = dataID.nextInt();
double grades = dataID.nextDouble();

Can I go to the next line and repeat using a while loop?

Yes you can loop over, java.util.Scanner offer hasNextLine method.

int sudentID,
    extraCredit;
double grade;
while(dataID.hasNextLine())
{
    studentID = dataID.nextInt();
    extraCredit = dataID.nextInt();
    grade = dataID.nextDouble();
}

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