Results 1 to 4 of 4
  1. #1
    thooom is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default Problems with FileReading/FileWriting.

    Hello Java society!

    I was just on NetBeans trying to make a Yatzee game - and it works like a charm. Problem is I wanna add a "Top 10 Highscores" as well. And here the problems begin. I have this code to generate entries for my top 10 list:

    Java Code:
        public void typeScore(String name, int points) throws FileNotFoundException {
            BufferedWriter bw = null;
            try {
                bw = new BufferedWriter(new FileWriter("highscore.txt", true));
                bw.write(name + "###" +  points);
                bw.newLine();
                bw.flush();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            } finally {
                if (bw != null) {
                    try {
                        bw.close();
                    } catch (IOException ioe2) {
                        // just ignore it
                    }
                }
            }
        }
    Furthermore I've tried various codes to read the .txt file but can not figure out a way to do following:
    1. Read .txt file into an array
    2. Explode on ### to separate playername from score
    3. Sort by score, descending.
    4. Use the top 10 scores in a jLabel1.setText();

    Java Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class FileArrayProvider {
    
        public String[] readLines(String filename) throws IOException {
            FileReader fileReader = new FileReader("highscore.txt");
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            List<String> lines = new ArrayList<String>();
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                lines.add(line);
            }
            bufferedReader.close();
            return lines.toArray(new String[lines.size()]);
        }
    }
    This I found on JAVA: Reading a file into an array - Stack Overflow but I'm having trouble using this array, plus I don't understand what the lines.size are doing.

    Any suggestions on how to make this work, or maybe proof that my pseudocode is way off and how to improve it would be highly appreciated.

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    Hello,

    That example from Stack Overflow works just fine.
    What kind of trouble you have with arrays?

    --
    plus I don't understand what the lines.size are doing.
    Java Code:
    lines.toArray(new String[lines.size()]);
    this line has to return array of Strings.

    toArray() methods takes a String array as argument and initial values is
    lines.size.
    --

    Maybe you can make new approach to players themselves and use new class
    that keeps together players names and scores:


    Java Code:
    @SuppressWarnings("unchecked")
    public class Player  implements Comparable{
    
    	private String name;
    	
    	private Integer score;
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public Integer getScore() {
    		return score;
    	}
    
    	public void setScore(Integer score) {
    		this.score = score;
    	}
    
    	@Override
    	public int compareTo(Object o) {
    		
    		Player p = (Player) o;
    		
    		if (score > p.score)
    			return 1;
    		
    		return -1;
    	}
    	
    	
    	
    }
    This is some Player util class just go give you idea :

    Java Code:
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class PlayerListUtil {
    
    	public static List<Player> createList() {
    
    		List<Player> playerList = new ArrayList<Player>();
    
    		/*p1*/
    		Player p1 = new Player();
    		p1.setName("Mike");
    		p1.setScore(150);
    
    		/*p2*/
    		Player p2 = new Player();
    		p2.setName("Joe");
    		p2.setScore(99);
    
    		/*p3*/
    		Player p3 = new Player();
    		p3.setName("Steve");
    		p3.setScore(113);
    
    		playerList.add(p1);
    		playerList.add(p2);
    		playerList.add(p3);
    
    
    		//printPlayerList(playerList);
    
    		return playerList;
    	}
    
    
    	public static void printPlayerList(List<Player> playerList) {
    
    		for (Player member : playerList){
    			System.out.print( member.getName() );
    			System.out.print(":");
    			System.out.println(member.getScore());
    
    		}
    
    	}
    
    
    	public static void sortPlayerList(List<Player> playerList){
    
    		Collections.sort(playerList, null);
    
    		printPlayerList(playerList);
    	}
    
    
    	public static List<Player> createTopList (List<Player> playerlist) throws TopTenException  {
    
    		//short version
    		//return  playerlist.subList(0,10); //short version for TOP TEN
    
    		
    		//longer version with list size taken in consideration
    
    		List<Player> topList;
    
    		int currentListSize = playerlist.size();
    
    		/*list size < 1 */
    		if (currentListSize < 1)
    			throw new TopTenException("List to small", new Throwable());
    		else
    			/* 1 < list size < 10 */
    			if (currentListSize < 10)
    				topList = playerlist.subList(0, currentListSize);
    			else {
    				/*list size > 10*/
    				topList = playerlist.subList(0,10);
    			}
    		
    		printPlayerList(topList);
    		return topList;
    
    	}
    
    
    
    	public static void main(String[] args) {
    
    		//PlayerListUtil.createList();
    		
    		PlayerListUtil.sortPlayerList(PlayerListUtil.createList());
    
    		try {
    			PlayerListUtil.createTopList(PlayerListUtil.createList());
    		} catch (TopTenException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    
    
    
    
    	}
    Hope this will help, but it needs refining...

    regards !

  3. #3
    thooom is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    Wow I'm speechless. Thank you very much for all your troubles. I do like this setup alot. Looks easier to manage/understand, allthough I gotta read up on a few of those codes to understand exactly what they do. Not really been working with the object class yet. But what you did is highly appreciated :)

  4. #4
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    You are very welcome, it's not big deal at all.

    Please describe your solution when you finish your work
    so all readers of this thread can find useful info.

    regards

Similar Threads

  1. Problems, problems, problems!
    By Empower5315 in forum New To Java
    Replies: 2
    Last Post: 08-28-2009, 04:27 AM
  2. Few Problems Help!
    By elad_bj in forum New To Java
    Replies: 4
    Last Post: 04-07-2009, 03:45 AM
  3. having problems
    By pinkdiamondgail in forum New To Java
    Replies: 6
    Last Post: 02-06-2009, 11:35 PM
  4. Problems in JSP : Need help
    By raj4u in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 02-07-2008, 10:06 AM
  5. gui problems
    By bluebirdjc in forum Advanced Java
    Replies: 2
    Last Post: 07-23-2007, 05:38 PM

Posting Permissions

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