0

A game is played in my code, but first a file is read and the leaderboard is kept in a linkedlist. After playing the game, a score is obtained and this score is added in the order of the linked list. All I have to do is write this new list to the txt file.

Here is the list before playing the game .
Pelin;30
Kaan;15
Ali;50
Yeliz;25
Cem;40
Can;35
Ece;5
Sibel;30
Remzi;20
Nazan;10

Here is my linked list node class:

public class Node {
    private Object data;
    private Node link;

    public Node(Object dataToAdd) {
        data = dataToAdd;
        link = null;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Node getLink() {
        return link;
    }

    public void setLink(Node link) {
        this.link = link;
    }

}

And here is my SLL class:

public class SingleLinkedList {
    Node head;

    public void insertScore(Object dataToAdd) {
        Node newNode = new Node(dataToAdd);
        if (head == null) {
            head = newNode;
        } else {
            int score = getScore(dataToAdd);
            int headObjectScore = getScore(head.getData());
            if (score > headObjectScore) {
                Node temp = head;
                head = newNode;
                newNode.setLink(temp);
            } else {
                Node currentNode = head;
                // to handle if the newNode has the biggest score
                if (currentNode.getLink() == null) {
                    currentNode.setLink(newNode);
                    currentNode = newNode;
                }
                while (currentNode.getLink() != null) {
                    Node oldNode = currentNode;
                    currentNode = currentNode.getLink();
                    if (score > getScore(currentNode.getData())) {
                        oldNode.setLink(newNode);
                        newNode.setLink(currentNode);
                        break;
                    }
                    // to handle if the newNode has the biggest score
                    if (currentNode.getLink() == null) {
                        currentNode.setLink(newNode);
                        currentNode = newNode;
                    }
                }

            }

        }
    }
private int getScore(Object data) {
        return Integer.valueOf(((String) data).split(";")[1]);
    }

I need to print the list txt file
enter image description here

I have completed to manage high score table but no idea to how can i change .txt file with the new linkedlist

Bashir
  • 2,057
  • 5
  • 19
  • 44
  • 3
    So where do you have a problem? – user Jun 12 '20 at 14:13
  • I have completed display the linkedlist but i have no idea to change .txt file I need a method to overwrite my .txt file – Mert Pınarbaşı Jun 12 '20 at 14:15
  • 2
    That's a little broad. You'll have to look up how to modify files and stuff - there are lots of good articles and tutorials out there – user Jun 12 '20 at 14:39
  • It seems you want to learn how to write or append to an existing file. Check [Java File - Open A File And Write To It](https://stackoverflow.com/questions/10667734/java-file-open-a-file-and-write-to-it) link out. – Nazim Jun 12 '20 at 20:13

1 Answers1

0
List<String> lines = Arrays.asList("The first line", "The second line");
Path file = Paths.get("the-file-name.txt");
Files.write(file, lines, StandardCharsets.UTF_8);

For Java 7+ you can use this way. The link below lists a few way to create files and print to them in Java. Consider how much you're writing and wether it needs to:

  1. Be appended tho the existing file
  2. Be in the new file and delete the old content
  3. Be in an entirely new file

From File IO in Java

SchnJulian
  • 182
  • 1
  • 11