0

This is my Data class:

public class Data {
    private String name;
    private int age;
    Data(String n,int a){
        name = n;
        age = a;
    }

    public String getName(){
        return(name);
    }

    public void setName(String n){
        name = n;
    }

    public int getAge(){
        return(age);
    }

    public void setAge(int a){
        age = a;
    }

    public void Print(){
        System.out.print(("("+GetName()));
        System.out.print(",");
        System.out.print(GetAge());
        System.out.print(") ");
    }
}

This is my CS1702_Lab5 class that uses ArrayList and that is supposed to print the content of ArrayList:

import java.util.ArrayList;

public class CS1702_Lab5 {

    public static void main(String args[]){
        ArrayList<Data> data = new ArrayList<Data>();
        data.add(new Data("Fred", 21));
    }

    private static void PrintDataArray(ArrayList<Data> data) {
        for(int i=0;i<data.size();++i){
            data.get(i).Print();
        }
    }
}

I am trying to add new Data which is a StringName and IntAge and then display it but it doesn't seem to work. The console is empty nothing is printed.

Thank you in advance!

2
  • 5
    please read the naming conventions and apply them Commented Feb 25, 2014 at 12:54
  • 1
    If you want to call the print method, you have to add it to your main, it won't automagically know what you meant it to do. Computers are extremely pedantic. Commented Feb 25, 2014 at 12:59

6 Answers 6

3

You never called your print method from main().

Sign up to request clarification or add additional context in comments.

Comments

1

You have to call PrintDataArray() method from main():

import java.util.ArrayList;

public class CS1702_Lab5 {
    public static void main(String args[]) {
        ArrayList<Data> data = new ArrayList<Data>();
        data.add(new Data("Fred", 21));
        PrintDataArray(data);  // YOU NEED TO ADD THIS LINE.
    }

    private static void PrintDataArray(ArrayList<Data> data) {
        for (int i = 0; i < data.size(); ++i) {
            data.get(i).Print();
        }
    }


}

Comments

1
public static void main(String args[])
{
    ArrayList<Data> data = new ArrayList<Data>();
    data.add(new Data("Fred", 21));
    PrintDataArray(data);
}

you never called PrintDataArray

Comments

0

Call the Print PrintDataArray method in the main method

public static void main(String args[]) {
{
ArrayList<Data> data = new ArrayList<Data>();
data.add(new Data("Fred", 21));
here>PrintDataArray(data);
}
}

Comments

0

You haven't call the method PrintDataArray(data) in your main method...

public static void main(String args[])
{
    ArrayList<Data> data = new ArrayList<Data>();
    data.add(new Data("Fred", 21));
    CS1702_Lab5.PrintDataArray(data);
}

Note: please read naming conventions in Java and try to use them.

Comments

0

Add PrintDataArray() inside your main method

public static void main(String args[])
{
    {
    ArrayList<Data> data = new ArrayList<Data>();
    data.add(new Data("Fred", 21));
    PrintDataArray(data);
    }
    }

    private static void PrintDataArray(ArrayList<Data> data) 
    {
        for(int i=0;i<data.size();++i)

        {
            data.get(i).Print();
        }
    }
}

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.