0

i am trying to write code outputs something like this:

NodeA (id: 7000, parentID: 0)

NodeB (id: 123, parentID: 7000)

NodeC (id: 9, parentID: 123)

NodeD (id: 2, parentID: 7000)

NodeE (id: 25, parentID: 7000)

NodeF (id: 3, parentID: 0)

NodeG (id: 10, parentID: 3)

where the childs parentId is the same a parent node id and below is my code

import java.util.ArrayList;
import java.util.Scanner;


public class CustomArrayList {


    // custom class which has data type 
    // class has defined the type of data ArrayList 
    // size of input 3
    int n = 3;
    // the custom datatype class    
    class Data {
        // global variables of the class
        int id;
        int parentId;
        String label;
         // constructor has type of data that is required 
        public Data(int id, int parentId, String label) {
             // initialize the input variable from main 
            // function to the global variable of the class 
            this.id = id;
            this.parentId = parentId;
            this.label = label;
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        while(sc.hasNext()){
        System.out.print("Enter how many friends: ");   
        int id[] = {sc.nextInt()};

        int parentId[];
        System.out.println("please enter the of the node");
        String label[] = {sc.nextLine()};

        CustomArrayList customList = new CustomArrayList();

        customList.addData(id, parentId, label);
        }
    }
    public void addData(int id[], int parentId[], String label[]) {
        ArrayList<Data>list = new ArrayList<>();

        for(int i = 0; i < n; i++){
            list.add(new Data(id[i], parentId[i], label[i]));

        }
        printValues(list);


    }
    private void printValues(ArrayList<Data> list) {

        for(int i=0; i<n; i++){
            Data data = list.get(i);

            System.out.println(data.id + "" + data.parentId + "" + data.label);
        }

    }
}

it should prompt the user user enter label of the node and id after which it will display them in a hierachy

i thank you all for reading this

Mickael
  • 4,003
  • 2
  • 25
  • 37
langton
  • 9
  • 4
  • What question do you have about your current code ? – Arnaud Sep 21 '18 at 12:10
  • `int id[] = {sc.nextInt()};` Creates an array where `length == 1`. – Johnny Mopp Sep 21 '18 at 12:14
  • on y current code that where i am stuck i nolonger know where to go from there – langton Sep 21 '18 at 12:14
  • sorry i had copied wrong from the ide – langton Sep 21 '18 at 12:18
  • on line 35 it should be: System.out.print("Enter id "); not friends – langton Sep 21 '18 at 12:20
  • Hello. Welcome to SO. Could you please take a closer look to the code you provided ? It doesn't seem to compile... You can check [here](https://ideone.com/Mn8t96) . I recommand you to read [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Mickael Sep 21 '18 at 12:27
  • Mickaël B . thanx for pointing out the reason why i had negleted the parentId is because its suppose to use the id of the parent node so that was one of the issues i am having i dont know how to go about it – langton Sep 21 '18 at 12:34

1 Answers1

0

The CustomArrayList class is meant contain a list of Data items, so the ArrayList<Data>list variable needs to be a class member of the CustomArrayList class.

You want to read a Data item input from the user then add that data to the list. Then repeat until each item has been read.

public class CustomArrayList {
    // the custom datatype class    
    class Data {
        // Code removed for brevity.....
    }

    // This is now a class member
    ArrayList<Data>list = new ArrayList<>();

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Just create this once (outside the loop)
        CustomArrayList customList = new CustomArrayList();

        System.out.print("Enter how many friends: ");   
        int count = sc.nextInt();

        // You asked how many, so use a for loop
        for (int i = 0; i < count; i++) {
            // Arrays not needed here as we are creating one at a time
            // TODO: Add error handling for bad user input
            // Get the data
            System.out.println("please enter the id of the node");
            int id = sc.nextInt();
            System.out.println("please enter the parent id of the node");
            int parentId = sc.nextInt();
            // see https://stackoverflow.com/a/13102066/669576
            // for why there is a nextLine call here
            sc.nextLine();
            System.out.println("please enter the label of the node");
            String label = sc.nextLine();

            // Add single item to list
            customList.addData(id, parentId, label);
        }

        // Now we have all the data, print it
        customList.printValues();
    }

    // Not arrays
    public void addData(int id, int parentId, String label) {
        // Just adding a single - no need to loop
        //for(int i = 0; i < n; i++){
        list.add(new Data(id, parentId, label));
        // printValues(list); not here
    }
    //private void printValues(ArrayList<Data> list) {
    public void printValues() {
        for (int i = 0; i < list.size(); i++){
            Data data = list.get(i);
            // Maybe override toString() in the Data class
            System.out.println(data.id + " " + data.parentId + " " + data.label);
        }
    }
}

I have just fixed the issues with your code. This is not a tree structure. You will need to make that yourself. But now that you can input the data correctly, changing the ArrayList to a tree should not be too difficult. (Hint: the Data class will need to hold references to its parent and/or children Data objects).

Johnny Mopp
  • 10,608
  • 4
  • 33
  • 58