Two-dimensional arrays [closed]

0

I have to make a program in which I use two-dimensional arrays.

The problem is like this: Program that gives the option of entering 5 names and 5 types of fruits in a two-dimensional matrix and at the end that the option appears to the user of choice "What do you want to show: the names or fruits entered ? " and print. Either switch option or with for.

I am a beginner and this is my wrong approach.

import java.util.Scanner;
import javax.swing.JOptionPane;

public class Opt {



    public static void main(String[] args) {

        String [][] matriz= new String [2][5];

        Scanner scan= new Scanner (System.in);
        System.out.println ("Introduzca  nombre");

        for (int i=0; i<=3; i++){
            System.out.println (" ");
            for (int j=0; j<=5; j++){
                matriz [i] [j] = scan.NextLine();
            }

            System.out.println ("Introduzca fruta");

            for (int i1=0; i1<=3; i1++){
                System.out.println (" ");
                for (int  j=0; j<=5; j++){
                Object [] frutas;   
                matriz [i] [j]= scan.NextLine;
                System.out.println ("QUE QUIERES QUE TE MUESTRE LOS NOMBRES O FRUTAS INGRESADOS");
            }
        }
    }
}
    
asked by COCO 03.11.2017 в 19:40
source

3 answers

4

good morning the code of yours had some problems in the for part since I had it badly placed in the following way for (int i1=0; i1<=3; i1++) and that's why it can generate a failure at the moment of compiling, besides that it had the conditions of the fix, since when you have an arrangement the size of the areglo is the length that can be a value n , but the size of the arrangement is n-1 I leave you an image in case If you have questions you can consult Documentation of java on the subject

  

a tip that you can use to place a for if you have an array is length that helps you know the size of the array

import java.util.Scanner;
import javax.swing.JOptionPane;

public class Opt {



    public static void main(String[] args) {

        String [][] matriz= new String [2][5];

        Scanner scan= new Scanner (System.in);
        System.out.println ("Introduzca  nombre");

        for (int i=0; i<2; i++){
            System.out.println (" ");
            for (int j=0; j<5; j++){
                matriz [i] [j] = scan.NextLine();
            }

            System.out.println ("Introduzca fruta");

            for (int i=0; i<2; i1++){// esta mal colocado el for en esta linea   ya que la variable esta acompañada por un numero  
                System.out.println (" ");
                for (int  j=0; j<=5; j++){
                Object [] frutas;   
                matriz [i] [j]= scan.NextLine;
                System.out.println ("QUE QUIERES QUE TE MUESTRE LOS NOMBRES O FRUTAS INGRESADOS");
            }
        }
    }
}
    
answered by 03.11.2017 в 21:07
3

Here is a brief explanation of the failures in the 1st part of your problem:

  
  • The limits of the for loops exceeded the boundary of the array.

  •   
  • The way to use the loops also did not solve the problem.

  •   

A correct form could be:

import java.util.Scanner;
import javax.swing.JOptionPane;

public class Opt {

    public static void main(String[] args) {

        String [][] matriz= new String [2][5];

        Scanner scan= new Scanner (System.in);

        for (int i=0; i<2; i++){
            for (int j=0; j<5; j++){
                if (i==0){
                     // en la matriz[0][x] van los nombres
                     System.out.println ("Introduzca  nombre: ");
                }else{
                      // en la matriz[1][x] van las frutas
                      System.out.println ("Introduzca fruta: ");
                }
               matriz [i] [j] = scan.nextLine();
            }
         }
   }
}
    
answered by 03.11.2017 в 21:24
3

Possible solution (the one I would give):

public static void main(String[] args) {
    String[][] matriz = new String[2][5];

    Scanner scan = new Scanner(System.in);

    System.out.println("Introduzca 5 nombres");

    for (int i = 0; i < matriz[0].length; i++) {
        matriz[0][i] = scan.nextLine();
    }

    System.out.println("introduce 5 frutas");

    for (int i = 0; i < matriz[1].length; i++) {
        matriz[1][i] = scan.nextLine();
    }

    System.out.println("Te muestro los nombres o las frutas?");

    String respuesta = scan.nextLine();

    switch (respuesta) {
    case "nombres":
        System.out.println(Arrays.toString(matriz[0]));
    case "frutas":
        System.out.println(Arrays.toString(matriz[1]));
    }
}
    
answered by 04.11.2017 в 01:07