Home > Blockchain >  How can I fix my output in my toString method?
How can I fix my output in my toString method?

Time:10-18

I am working on a project for my Data Structures class that asks me to write a class to implement a linked list of ints.

  • Use an inner class for the Node.
  • Include the methods below.
  • Write a tester to enable you to test all of the methods with whatever data you want in any order.

I have to create a toString method called "public String toString()". This method is meant to "Print the List but in the format of [1, 2, 3] with the commas and brackets in the right place." I have my code for this method down below. However, when I use the toString method there's a bit of an issue. For example, if I have a list [17, 14, 8, 11, 19, 6, 15, 11, 5, 5]. My output is [17, 14, 8, 11, 19, 6, 15, 11, 5, 5, ]. After the last element, I get an extra comma and an extra space at the end. How do I fix this?

import java.util.Random;
import java.util.Scanner;

public class LinkedListOfInts {
    Node head;
    Node tail;

    private class Node {
        int value;
        Node nextNode;

        public Node(int value, Node nextNode) {
            this.value = value;
            this.nextNode = nextNode;
        }

    }

    public LinkedListOfInts(LinkedListOfInts other) {
        Node tail = null;
        for (Node n = other.head; n != null; n = n.nextNode) {
            if (tail == null)
                this.head = tail = new Node(n.value, null);
            else {
                tail.nextNode = new Node(n.value, null);
                tail = tail.nextNode;
            }
        }
    }

    public LinkedListOfInts(int[] other) {
        Node[] nodes = new Node[other.length];
        for (int index = 0; index < other.length; index  ) {
            nodes[index] = new Node(other[index], null);
            if (index > 0) {
                nodes[index - 1].nextNode = nodes[index];
            }
        }

        head = nodes[0];
    }

    public LinkedListOfInts(int N, int low, int high) {
        Random random = new Random();
        for (int i = 0; i < N; i  )
            this.addToFront(random.nextInt(high - low)   low);
    }

    public void addToFront(int x) {
        head = new Node(x, head);
    }

    public String toString() {
        String result = "";
        for (Node ptr = head; ptr != null; ptr = ptr.nextNode)
            result  = ptr.value   ", ";
        return "["   result   "]";
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        LinkedListOfInts list = new LinkedListOfInts(10, 1, 20);
        boolean done = false;
        while (!done) {
            System.out.println("1. toString");
            switch (input.nextInt()) {
            case 1:
                System.out.println("toString");
                System.out.println(list.toString());
                break;
            }
        }
    }
}

CodePudding user response:

Use a StringJoiner, for example...

public String toString() {
    StringJoiner sj = new StringJoiner(", ", "[", "]");;
    for (Node ptr = head; ptr != null; ptr = ptr.nextNode) {
        sj.add(Integer.toString(ptr.value));
    }
    return sj.toString();
}

Then using something like...

LinkedListOfInts list = new LinkedListOfInts(new int[]{17, 14, 8, 11, 19, 6, 15, 11, 5, 5});
System.out.println(list.toString());

Will print...

[17, 14, 8, 11, 19, 6, 15, 11, 5, 5]

Now, if you can't use a StringJoiner

  • Related