Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-03-2013, 10:08 PM   PM User | #1
Wizard0
New to the CF scene

 
Join Date: Feb 2013
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Wizard0 is an unknown quantity at this point
Sort existing data with new data (external txt-file)

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");
		}

	}
}

Last edited by Fou-Lu; 02-04-2013 at 02:51 PM..
Wizard0 is offline   Reply With Quote
Old 02-04-2013, 03:02 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote tags don't use preformatted text elements. Using the [php][/php] or [code][/code] will retain the formatting. For which yours is very good.

So let me see if I understand this correctly. You only need to keep in the file the top five entries is that correct? If I add another entry that is < any of the top 5, it is just abandoned, and if I insert an entry > any of the top 5 than it shifts the lowest out and inserts the new one?
Are you allowed to use in-memory variable allocations for the class? The easiest way to do this involves an array or collection, for which you can sort and makes it easy to tell if an item can fit in it. A *slight* pain when it comes to shifting an array such as removing the lowest score and inserting a new one in the middle which pushes everything else down, but that can be worked around by simply overwriting the lowest and then sorting it again. If you're familiar with collections, a LinkedList is the one for this.
Operation wise for an application this small I'd load from a file during an initialization command. Keep what I can in memory for usage, then on quit I'd save it back in.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-04-2013, 07:38 PM   PM User | #3
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
Also- I was reading that class (PrintWriter) documention and from my understanding it will always truncate the file. Also it will create the file if it does not exists (ie you can comment out your new File statement since you are not doing anything special with the File object before passing it to the printwriter)


Edit:
What I mean is that you will always be overwriting it so if you only write one player/score then there would only be 1 entry- so I would suggest in memory collection as Fou-Lu stated. But I could have misread that Class document as well
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE
alykins is offline   Reply With Quote
Old 02-06-2013, 04:27 PM   PM User | #4
Wizard0
New to the CF scene

 
Join Date: Feb 2013
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Wizard0 is an unknown quantity at this point
Thanks for your reply.

Yes Fou, that's exactly what I need. I think I will use an array for it. I'm sitting with it right now trying to fix things up. What I first did was creating a new class called Player. In the player-class I will create an array to both place and sort the highscore list.

Also I've been adding a method for reading the file, saving to memory so it can re-apply the data before it starts sorting/erasing all players that's >5.

I will let you know how that worked out for me later today
Wizard0 is offline   Reply With Quote
Old 02-06-2013, 11:12 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You bet.
Let us know if you have any issues we can help you with.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:56 AM.


Advertisement
Log in to turn off these ads.