2

Hola tengo un problema con un programa que estoy haciendo, el cual consiste en ingresar datos en un array, sin embargo cuando muestro por pantalla la lista, se me repite todo el rato el último dato que he introducido.

Dejo aquí un ejemplo de lo que he hecho. He intentado hacerlo con un vector pero me ocurre el mismo problema y no se cual es el método mas recomendable para hacerlo si con un array o con un vector.

package Estudio;

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

public class Ejemplo {

    private static int datos;
    private static String word;

    public Ejemplo(int datos, String word) {
        this.datos = datos;
        this.word = word;
    }

    public static int getDatos() {
        return datos;
    }

    public String getData() {
        return this.word + " - " + this.datos;
    }

    public static void setDatos(int datos) {
        Ejemplo.datos = datos;
    }

    static List<Ejemplo> nums = new ArrayList<Ejemplo>();


    public static void showList() {
        for(Ejemplo e:nums) {
            System.out.println(e.getData());
        }
    }

    public static void IntroduceInf() {
        Scanner scanner = new Scanner(System.in);
        int a; String b;
        System.out.println("Introduce a number");
        a = scanner.nextInt();
        scanner.nextLine();
        System.out.println("Introduce a word");
        b = scanner.nextLine();
        nums.add(new Ejemplo(a, b));
    }


    public static void main(String[] args) {
        int a = 3;
        Scanner scanner = new Scanner(System.in);
        while(a!=0) {
            System.out.println("Do you want to introduce more information?");
            System.out.println("1.-Yes");
            System.out.println("0.-No");
            a = scanner.nextInt();
            if(a==1) {
                IntroduceInf();
            }else {
                if(a==0) {
                    showList();
                }
            }
        }

    }
}

En el Programa saldria algo parecido a esto, repitiendo el ultimo dato introducido en todos los datos anteriores hello- 3 hello- 3 hello- 3

Agradeceria mucho si alguien supiese decirme mas o menos donde esta el problema y orientarme para poder solucionarlo ya que estoy empezando a usar java y no conozco muy bien el lenguaje todavia. Muchas gracias de antemano.

1 respuesta 1

3

Los atributos de tú objecto están como static, cuando defines un atributo con static todas las instancias de la clase la comparten.

Elimina la directiva static y obtendras el resultado esperado.

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

public class Ejemplo {

    private int datos;
    private String word;

    public Ejemplo(int datos, String word) {
        this.datos = datos;
        this.word = word;
    }

    public int getDatos() {
        return datos;
    }

    public String getData() {
        return this.word + " - " + this.datos;
    }

    public void setDatos(int datos) {
        this.datos = datos;
    }

    static List<Ejemplo> nums = new ArrayList<Ejemplo>();


    public static void showList() {
        for(Ejemplo e:nums) {
            System.out.println(e.getData());
        }
    }

    public static void IntroduceInf() {
        Scanner scanner = new Scanner(System.in);
        int a; String b;
        System.out.println("Introduce a number");
        a = scanner.nextInt();
        scanner.nextLine();
        System.out.println("Introduce a word");
        b = scanner.nextLine();
        nums.add(new Ejemplo(a, b));
    }


    public static void main(String[] args) {
        int a = 3;
        Scanner scanner = new Scanner(System.in);
        while(a!=0) {
            System.out.println("Do you want to introduce more information?");
            System.out.println("1.-Yes");
            System.out.println("0.-No");
            a = scanner.nextInt();
            if(a==1) {
                IntroduceInf();
            }else {
                if(a==0) {
                    showList();
                }
            }
        }

    }
}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.