4

I'm a beginner at Java and have a list of 25 students that include their name, age, income and IQ in a text file. I'm struggling with how to take this text file and put it in an Array so that I can sort them and such. So far I have:

File myFile = new File ("./src/Project2/StudentList");
Scanner myScan = new Scanner(myFile);

while (myScan.hasNext()) {

    String line = myScan.nextLine();
    Scanner scanner = new Scanner(line);
    scanner.useDelimiter(",");

    while (scanner.hasNext()) {
        String name = scanner.next();
        String age = scanner.next();
        String income = scanner.next();
        String smart = scanner.next();

        Student students = new Student(name, age, income, smart);


        System.out.println(students);
    }
}

I just want to know the easiest way to go about this. I'm so close, I can feel it! Thanks in advance.

3 Answers 3

1

Define your array:

Student[] students = new Student[25];
int i = 0;

then in your loop

Student student = new Student(name, age, income, smart);
students[i++] = student;

or dynamic array

List<Student> students = new ArrayList()<>;

and in loop:

Student student = new Student(name, age, income, smart);
students.add(student);
Sign up to request clarification or add additional context in comments.

Comments

1

So if the text file is being parsed correctly, and the Student class is implemented properly, I think the best way to do it would just be to insert students into a student array where the print statement is happening.

You would make this before the loop:

Student[] studentArray = new Student[25];
int count = 0;

And where the print statement is happening

studentArray[count] = student;
count++;

Also, I would rename the students variable inside the loop to be singular for clarity, as it will only represent one student at a time. Maybe currentStudent would be more appropriate actually.

4 Comments

Ah! Thank you! I do have one more question. I'm suppose to sort the 25 students based on their IQ (0-50) using a selectionSort or insertionSort. How would I go about that?
@StarLordCoderGuy11 you should google those two sorting algorithms and figure out how to do it yourself because doing it yourself is by far the best way to go about it
Like @nem said, implementing your own sort would be a very good way to learn. There is a sort implemented with Arrays.java however, I assume that would be either quicksort or mergesort. I think you would also need to build a comparator for your Student class. The way to go about that would be to have the Student class implement Comparable and then implement the compareTo method. h ttps://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html
I think implementing comparable will also work if you write your own sorting function, by the way. I believe having it implement that will allow you to properly compare them within a custom sort.
0

Initialize your array outside the loop: Student[] students = new Student[25]; Then, maintain a counter to keep track of what student you're on as you loop through the file. For each student, students[i] = new Student(name, age, income, smart);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.