Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class RankingTest {
- private String id[];
- private int score[];
- public RankingTest() {
- File prog408a = new File("prog408a.dat");
- try {
- Scanner scanner = new Scanner(prog408a);
- ArrayList<String> lines = new ArrayList<>();
- // parse file into list
- while (scanner.hasNextLine()) {
- lines.add(scanner.nextLine());
- }
- id = new String[lines.size()];
- score = new int[lines.size()];
- // fill arrays
- for (int i = 0; i < lines.size(); i++) {
- String[] line = lines.get(i).split(" ");
- id[i] = line[0];
- score[i] = Integer.parseInt(line[1]);
- }
- this.bubbleSort();
- this.printScores();
- } catch (FileNotFoundException e) {
- System.out.println("File not found!");
- e.printStackTrace();
- }
- }
- private void bubbleSort() {
- int swaps = 0;
- do {
- swaps = 0;
- for (int i = 1; i < score.length; i++) {
- if (score[i - 1] < score[i]) {
- int temp = score[i - 1];
- score[i - 1] = score[i];
- score[i] = temp;
- String temp2 = id[i - 1];
- id[i - 1] = id[i];
- id[i] = temp2;
- swaps++;
- }
- }
- } while (swaps > 0);
- // stop after a full pass without swapping
- }
- private void printScores() {
- System.out.println("ID Score");
- for (int i = 0; i < id.length; i++) {
- System.out.println(id[i] + " " + score[i]);
- }
- }
- public static void main(String[] args) {
- new RankingTest();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement