arraylist with method

As my last question obviously was a little bit unclear (I applogise for that), I'm making a new try, and this time I will really try to be clear.

Here is the code I have written so far

Main class:

import java.util.*;

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

    String fname, lname;
    int choice;

    ArrayList<People>list = new ArrayList<>();

    People p1 = new People("Mia", "Wallace", "1111");
    People p2 = new People("Marcellus", "Wallace", "2222");

    list.add(p1);
    list.add(p2);

    System.out.println("Welcome");

    System.out.println("1) Add person \n2) Last name \n3) Print list");
    choice = scan.nextInt();
    switch(choice){
    case 1:
        People.walk(); //calling the method
        break;
    case 2:
        People.run();
        break;
    case 3:
        People.crawl();
        break;
    }   
}
}

People class:

import java.util.*;

public class People {
static Scanner scan = new Scanner(System.in);

private static String fname;
private static String lname;
private static String dob;

public People(String fname, String lname, String dob){
    this.fname = fname;
    this.lname = lname;
    this.dob = dob;
}
public String getFname(){
    return fname;
}
public String getLname(){
    return lname;
}
public String getDob(){
    return dob;
}
public static void walk(){
    System.out.println("Enter first name: ");
    fname = scan.next();
    System.out.println("Enter last name: ");
    lname = scan.next();
    System.out.println("Enter dob: ");
    dob = scan.next();
    list.add(new People(fname, lname, dob));  **//list cannot be resolved**     
}
public static void run(){
    System.out.println("Remove people");        
}
public static void crawl(){
    int peoplenumber = 0;
    System.out.println("\nNumber of peoples: " + list.size()); **//list cannot     be resolved**
    for(People p : list){ **//list cannot be resolved**
        peoplenumber += 1;
        System.out.println("#" + peoplenumber + "\n" + p.toString());
    }
}
public String toString(){
    return "First name: " + this.fname + "\nLast name: " + this.lname + "\nDateOfBirth: " + this.dob;
}
}

I do understand why I get the error, but I don't know how to get around it. Any help?

Am I right when I try to move code from the main-class to the costructors?

If you guys have any comments or ideas how I should solve this, or move on with java, I will gladly here them.

Thank you very much for your time and your help :)

Answers


The arraylist should be in the main class. If it were in the person class, it would be created once for every person object you create. To access a method for that person, you want to call the name of the OBJECT (not the type), the .method(parameters).

If you wanted to call run on p1 (not really sure what you want to call it on), you would use:

p1.run();

That would perform the code in the run method where it is defined.


Need Your Help

How to convert a char[] to a std::string

c++

I´m trying to convert a char[] into a std::string. Everywhere I looked, I found the same answer, that string has a constructor doing this exact thing.

How to update chart when data is changing?

excel vba excel-vba charts onchange

I have two columns were is information that I use in chart and it is changing according to what time range I choose. The main problem is that the columns length can be different, it can be 5 rows and