How can I add a name (String) within a predefined array in java?

0
  • Load an array with 3 predefined fruit names

    String fruits [] = {"apple", "orange", "melon"};

  • 2.Add a name to the list by keyboard.

        
    asked by Marck 01.04.2018 в 04:26
    source

    2 answers

    0

    It is not possible to add an element to an array that is already full. If you have a 3-position arrangement, you can not modify it later. You can replace the name of a position with another name, but do not add more. I can think of two options to solve your problem.

    Option 1: Create an arrangement with one more position to save the name of the fruit received by the keyboard in the last one.

    import java.util.Scanner;
    
    public class Ejemplo {
    
        public static void main(String[] args) {
    
            Scanner scanner = new Scanner(System.in);
    
            // Arreglo con 1 dimension más para el String solicitado
            String[] frutas = new String[4];
            frutas[0] = "manzana";
            frutas[1] = "naranja";
            frutas[2] = "melon";
    
            // Variable para guardar el nombre de la fruta ingresada
            String nuevaFruta;
    
            // Pedir el nombre de una fruta por teclado
            System.out.println("Ingrese el nombre de una fruta: ");
            nuevaFruta = scanner.nextLine();
    
            // Cerrar Scanner
            scanner.close();
    
            // Guardar el nombre de la nueva fruta en la última posición del arreglo
            frutas[3] = nuevaFruta;
    
            // Imprimir arreglo
            for (String nombre : frutas) {
                System.out.println(nombre);
            }
        }
    }
    

    Option 2: Use ArrayList , its advantage is that the number of elements it stores, it does so dynamically, that is, it is not necessary to declare its size as it happens with the Arrays .

    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class Ejemplo {
    
        public static void main(String[] args) {
    
            Scanner scanner = new Scanner(System.in);
    
            // ArrayList de tipo String
            ArrayList<String> frutas = new ArrayList<>();
    
            // Arregar frutas al ArrayList
            frutas.add("manzana");
            frutas.add("naranja");
            frutas.add("melon");
    
            // Pedir el nombre de una fruta por teclado
            System.out.println("Ingrese el nombre de una fruta: ");
    
            // Guardar el nombre de la fruta en el ArrayList
            frutas.add(scanner.nextLine());
    
            // Cerrar Scanner
            scanner.close();
    
            // Imprimir arreglo
            for (String nombre : frutas) {
                System.out.println(nombre);
            }
        }
    }
    

    I hope I have helped you.

        
    answered by 01.04.2018 / 05:31
    source
    0

    Arrays in java are not resizable, so it is not possible to add one more position to the frutas array, since its definition restricts its size to 3 elements.

    If the requirements of your application indicate that the user should be allowed to add elements to the collection indefinitely, I recommend that you use a list-type data structure, for example ArrayList .

        
    answered by 01.04.2018 в 04:37