OK. First of all, this will be my first post on this forum, I just found it and I'm pretty happy about it.
Short story of why I'm posting: I study information science at a University in Sweden and recently started a course in Java-programming(OOP).
We recieved an assignment bout a week ago and I've been trying to finish this project all week. Every time I start trying my brain freezes and I just dont know how to fix this problem.
The problem: We're making a highscore-list that sends strings to a external .txt-file. While doing so I need to sort the current data (in the textfile) with the new highscore I'm adding. Also, I need to erase anything that's not in the top 5 on the highscorelist.
Not sure if I've made myself clear, my english is not perfect so please ask if you dont understand what I need help with.
What I'm asking for: I really need some sort of guidance to finish this project, so help me please!
(I seem to have problems with indenting the code on the forums, that's why I've added pastie-links to all classes)
Just click the bold text.
The code:
MAIN:
Code:
public class main {
public static void main(String[] args) {
menu menu = new menu();
menu.display();
}
}
MENU:
Code:
import java.util.*;
public class menu {
highscores highscores = new highscores();
private Scanner input = new Scanner(System.in);
public void display() {
System.out.println("Make your selection!");
System.out.println("Select an option: \n" + " 1) Insert new score\n"
+ " 2) Print list\n" + " 3) Reset list \n" + " 4) Quit\n ");
int selection = input.nextInt();
input.nextLine();
switch (selection) {
case 1:
highscores.enterScore();
break;
case 2:
highscores.printList();
break;
case 3:
highscores.resetList();
break;
case 4:
System.out.println("Exiting program...");
System.exit(1);
default:
System.out.println("Try Again!");
break;
}
}
}
HIGHSCORELIST:
Code:
import java.io.*;
import java.util.*;
public class highscores {
public void enterScore() {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the players name!: ");
String name = scan.nextLine();
System.out.println("Enter the players score!: ");
String score = scan.nextLine();
System.out.println("Player " + name + " got: " + score
+ " points. Great job!");
try {
File file = new File("HighScores.txt");
PrintWriter writer;
writer = new PrintWriter(file);
writer.println("Player name: " +name +" - " +"Player score:" +score);
writer.close();
} catch (Exception e) {
System.out.println("Error #1");
}
}
private void sortScore() {
}
public void printList() {
try {
File file = new File("Highscores.txt");
Scanner scanner;
if (file.exists()) {
scanner = new Scanner(file);
for (int i = 0; i < 5; ++i) {
String line = scanner.nextLine();
System.out.println(line);
}
} else {
System.out.println("Error #2");
}
} catch (Exception e) {
System.out.println("Error #1");
}
}
public void resetList() {
try {
File file = new File("Highscores.txt");
PrintWriter writer;
writer = new PrintWriter(file);
for (int i = 0; i < 5; ++i) {
writer.println("Player name: x - Player score: x ");
}
writer.close();
} catch (Exception e) {
System.out.println("Error #1");
}
}
}