Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

java - Exercise With arraylist input with scanner

my exercise is with collect soccer cards inside an arraylist. inside my code I have to insert the methods: "add, search, remove, print and modify". From input I must save all data of name of player, surname and country after that from output someone can ask me about player so i can tell it from code. My problem is i can't add some thing from input and i don't know how to continue with another methods after input with Scanner.

MAIN:

package figurines;
import java.util.*;

public class Figurines {

    public  ArrayList<Giocatori> lista =  new ArrayList<Giocatori>();

    public static void main(String[] args) {
        System.out.println("1 Penaldo 2 Pessi 3 Neymar 4 Buffon");
        
        Scanner searchBar = new Scanner(System.in);
        System.out.println("Inserisci I dati del giocatore");
        String search = searchBar.nextLine().toUpperCase();
        for (Giocatori liste : lista) {  
            if (liste.getCognome().equalsIgnoreCase(search)) {
                System.out.println(" Nome = " + liste.getNome() +" Paese= " + liste.getPaese());
            }
        }
    }
}

CLASS WHERE I CAN ADD METHODS

package figurines;
import java.util.*;

public class Gestione {
    
    public void input(){
        ArrayList<Giocatori> list = new ArrayList<Giocatori>();
        Scanner tastiera = new Scanner(System.in);
        System.out.println("Inserisci il nome del giocatore: ");
        String name;
        String surname;
        Scanner input = new Scanner(System.in);
        System.out.println("Please Enter first name: ");
        name = input.nextLine();
        System.out.println("Please Enter last name: ");
        surname = input.nextLine();
    }
}

SOME ARRAY METHODS:

package figurines;
import java.util.*;

public class Giocatori {
    
    private String nome;
    private String cognome;
    private String paese;

    public Giocatori(String nome, String cognome, String paese) {
        this.nome = nome;
        this.cognome = cognome;
        this.paese = paese;
    }
    

    public String getNome() {
        return nome;
    }

    public String getCognome() {
        return cognome;
    }

    public String getPaese() {
        return paese;
    }
}
question from:https://stackoverflow.com/questions/65904369/exercise-with-arraylist-input-with-scanner

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well, your classes Gestione and Figurines don't make much sense. Why would you have List of players in your Figurines class? These are basics and you should know, how to make classes and functions.

This is my recommended implementation of Gestione class:

public class Gestione {

    private List<Giocatori> list = new ArrayList<>();

    public void add(Giocatori giocatori) {
        list.add(giocatori);
    }

    public Giocatori search(String cognome) {
        // search by surname
        for (Giocatori g : list) {
            if (g.getCognome().equals(cognome)) return g;
        }
        return null;
    }

    public void remove(Giocatori giocatori) {
        list.remove(giocatori);
    }

    public void print() {
        for (Giocatori g : list) {
            System.out.println(g);
            // create toString method in Giocatori
        }
    }

    public void modify() {
        // I don't know what modify should do
    }

    public List<Giocatori> getList() {
        return list;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...