Reputation: 11
I'm trying to figure out how to sort the arrayLists that come out of a .txt file. I want to be able to sort them alphabetically by name. Here is an example of how the txt file is listed:
Alvarez, Eliezer
74
2B
IA
22
Bowman, Matt
67
P
A
26
Each piece is on a single line by itself (except lastName, firstName being on a line together).
Is there a way to do a collections sort that will adjust the rest of the Arraylists based off the name Arraylist? Thanks.
Scanner keyboard = new Scanner(System.in);
String filename;
fileName = "cardinals.txt";
File baseball = new File(fileName);
if (!baseball.exists()) {
System.out.println("The input file was not found.");
System.exit(0);
}
ArrayList<String> name = new ArrayList<>();
ArrayList<Integer> number = new ArrayList<>();
ArrayList<String> position = new ArrayList<>();
ArrayList<String> status = new ArrayList<>();
ArrayList<Double> age = new ArrayList<>();
Scanner stats = new Scanner(baseball);
if (!stats.hasNext()) {
System.out.println("The file is empty.");
}
else {
while (stats.hasNext()) {
name.add(stats.nextLine());
number.add(stats.nextInt());
position.add(stats.next());
status.add(stats.next());
age.add(stats.nextDouble());
try {
stats.nextLine();
} catch (Exception e) {
}
}
} stats.close();
sortArrayPosition();
sortArrayPosition (ArrayList<String> name, ArrayList<Integer> number, ArrayList<String> position, ArrayList<String> status, ArrayList<Double> age) {
Upvotes: 1
Views: 580
Reputation: 1042
When organizing and managing complex data, using an object to group the data together is the best approach.
Here is an example using an object to store the information:
package example;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
class StatisticsReader {
private static class PlayerStats {
String name;
Integer number;
String position;
String status;
Double age;
public PlayerStats(String name, Integer number, String position, String status, Double age) {
this.name = name;
this.number = number;
this.position = position;
this.status = status;
this.age = age;
}
@Override
public String toString() {
return "PlayerStats{" +
"name='" + name + '\'' +
", number=" + number +
", position='" + position + '\'' +
", status='" + status + '\'' +
", age=" + age +
'}';
}
}
public static void main(String[] args) {
File baseball = new File("cardinals.txt");
if (!baseball.exists()) {
System.out.println("The input file was not found.");
System.exit(0);
}
List<PlayerStats> statsList = new ArrayList<>();
try (Scanner stats = new Scanner(baseball)) {
if (!stats.hasNext()) {
System.out.println("The file is empty.");
}
else {
while (stats.hasNext()) {
String name = stats.nextLine();
Integer number = stats.nextInt();
String position = stats.next();
String status = stats.next();
Double age = stats.nextDouble();
statsList.add(new PlayerStats(name, number, position, status, age));
stats.nextLine();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
statsList.sort(Comparator.comparing(o -> o.name));
statsList.forEach(System.out::println);
}
}
You can customize the toString() method to change the output if necessary.
Upvotes: 3