1
import java.util.*;

public class TestClass {
static String name;
static String gender;
static String hairColor;

TestClass(String name, String gender, String hairColor){
    this.name=name;
    this.gender=gender;
    this.hairColor=hairColor;

}
public static void main (String[]args) {
    TestClass info = new TestClass(name, gender, hairColor);
    LinkedList<TestClass> linky = new LinkedList<TestClass>();
    Scanner input = new Scanner(System.in);

    System.out.println("Enter name ");
    name=input.nextLine();

    System.out.println("Enter gender ");
    gender=input.nextLine();

    System.out.println("Enter hair color ");
    hairColor=input.nextLine(); 

    }

}

I want to add user input into the linked list but I can't figure out how to. I have tried to use linky.add(name); but that gives an error. How can I add input into the linked list?

5
  • take a look of the type of linked list linky is, it looks like you need to add a new instance of your TestClass not a String Commented Dec 4, 2017 at 18:43
  • Also name, gender and hairColor shouldn't be static, it will help you in the long run Commented Dec 4, 2017 at 18:45
  • 1
    TestClass objects contain no data! All data members are static. While you can add multiple TestClass objects to the linked list, the objects are identical (ignoring reference identity). Commented Dec 4, 2017 at 18:45
  • You are using constructor to change static variables which defeats the purpose of static, you want to add object of type String into a LinkedList that accepts objects of type TestClass. Commented Dec 4, 2017 at 18:49
  • I tried to make them non-static variables but then when I did, I got errors in my main method that say Cannot make a static reference to the non-static field on my lines that assign the user's input to the variable. Commented Dec 4, 2017 at 20:10

5 Answers 5

1
import java.util.*;

public class TestClass {
    // These should (most likely) not be static
    // Read up on what 'static' means
    String name;
    String gender;
    String hairColor;

    TestClass(String name, String gender, String hairColor){
        this.name = name;
        this.gender = gender;
        this.hairColor = hairColor;
    }

    public static void main (String[]args) {
        LinkedList<TestClass> linky = new LinkedList<TestClass>();

        Scanner input = new Scanner(System.in);

        System.out.println("Enter name ");
        String name = input.nextLine();

        System.out.println("Enter gender ");
        String gender = input.nextLine();

        System.out.println("Enter hair color ");
        String hairColor = input.nextLine();

        TestClass info = new TestClass(name, gender, hairColor);

        linky.add(info);
    }
}

Since linky is a LinkedList<TestClass> with generic type TestClass it can only hold objects of type TestClass so when you try to linky.add(name) it wont work

To print out the contents of the list you will need to iterate through the list and print each element, if the list contained Strings or something with an Overridden toString method, then you could simply call:

for(String str : linky){
    System.out.println(str);
}

BUT this will not work because linky contains TestClasses. Therefor you could do this (but you should probably define geter methods for the fields of TestClass):

for(TestClass testClass : linky){
    System.out.println("Name: " + testClass.name);
    System.out.println("Gender: " + testClass.gender);
    System.out.println("Hair Color: " + testClass.hairColor);
}

OR in TestClass you could override the toString method inherited from its superclass Object

// In TestClass
@Override
public String toString(){
    // Format this however you like
    return "Name :" + this.name + "; Gender: " + this.gender + "; Hair Color: " + this.hairColor;
}

Then all you would need to do is this:

for(TestClass testClass : linky){
    // println(testClass) calls testClass.toString()
    // giving you the formatted data from TestClass
    System.out.println(testClass);
}
Sign up to request clarification or add additional context in comments.

2 Comments

How can I print the list? I tried to do System.out.println(linky.toString()); but that outputs [TestClass@55f96302]I know I need to use toString() but I'm not sure how to implement it.
@liquidOfficer see edits for your answer. For additional info look up overriding, and also iterating through collections.
1

You first need to store all the user inputs in the TestClass object and then add that object to your LinkedList

After taking inputs from user, pass all the variables that contain user input values in to TestClass constructor

TestClass test = new TestClass(name, gender, hairColor);

then store test object in your LinkedList

linky.add(test); 

Important Note:

Data member variables of class TestClass should not be static if you want to store multiple instances of TestClass each having its own data.

Comments

1

You need to add new instance of TestClass to LinkedList like that:

public static void main(String[] args) {
    LinkedList<TestClass> list = new LinkedList<>();
    Scanner input = new Scanner(System.in);

    System.out.println("Enter name ");
    String name = input.nextLine();

    System.out.println("Enter gender ");
    String gender = input.nextLine();

    System.out.println("Enter hair color ");
    String hairColor = input.nextLine(); 

    list.add(new TestClass(name, gender, hairColor));
}

Comments

0
LinkedList<TestClass> linky = new LinkedList<TestClass>();

You need to add your instance of TestClass

Comments

0

just change it :

LinkedList<String> linky = new LinkedList<String>();
    Scanner input = new Scanner(System.in);

    System.out.println("Enter name ");
    name=input.nextLine();
    linky.add(0, name);
    System.out.println("Enter gender ");
    gender=input.nextLine();
    linky.add(1, gender);
    System.out.println("Enter hair color ");
    hairColor=input.nextLine(); 
linky.add(2, hairColor);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.