Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,742
    Thanks
    41
    Thanked 191 Times in 190 Posts
    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

  • #4
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You bet.
    Let us know if you have any issues we can help you with.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •