How can I call an array that is inside a method to another method within the same class?

1

This is what I've been doing so far

public class Proyecto_progra1 {
public static void main(String[] args) {
    String opcion;
    int num = 0;

    do{

    opcion = JOptionPane.showInputDialog("1- Nombre de conductores.\n"
            +"2- Placas de autobuses.\n"+"3- Rutas y precios. \n"+"4- Simulacion \n"
    +"5- Ganancias del dia.\n"+"6- Unidades y rutas\n"+"7- Salir");//Opciones para el usuario.

    if (opcion.matches ("[0-9]*"))//Se valida valor ingresado si es numero o letra
    {
    num = Integer.parseInt(opcion);//

    switch (num)
    {
        case 1 : nombre_conductores(); //Llamar metodo de nombre de conductores
        break;
        case 2: placas_buses(); //Llamar metodo donde se almacenan las placas de los buses.
        break;
        case 3: rutas_precios();// Llamar metodo donde se definen las rutas y los precios.
        break;
        case 4: simulacion();
        break;
        case 5: ganancia();
        break;
        case 6: unidades();
        break;
        case 7: System.exit(0);//Salir del programa
        break;
        default: JOptionPane.showMessageDialog(null, "Digite algunas de las opciones disponibles.");
    }
    }
    else

{
JOptionPane.showMessageDialog(null, "Digite un numero");
}
    } while(num!=4);//Se encicla programa hasta que el usuario decida salir.
}


 public static void nombre_conductores()
 {
  String [] nombre = new String[5]; //Se define arreglo.


 for( int i = 0; i<5; i++)
 {
     nombre[i]  = JOptionPane.showInputDialog((i+1)+"-Digite nombre");// Se ingresan nombres de los choferes

 }
 for (int i = 0; i<5; i++)
 {
 JOptionPane.showMessageDialog(null, "Los nombres son:\n"+(i+1)+"-"+nombre[i]); // Se imprimen el nombre de los choferes
 }

}

 public static void placas_buses()
 {
    String [] placas = new String[6]; // Se define arreglo

    for (int i = 0; i<6; i++)
    {
    placas[i] = JOptionPane.showInputDialog((i+1)+"-Digite numeros de placas"); // Se ingresan el numeros de las placas
    }

    for(int i = 0; i<placas.length; i++)
    {
    JOptionPane.showMessageDialog(null, "Las placas son:\n"+(i+1)+"-"+placas[i]);// Se imprimen numeros de placa
    }
 } 

public static void rutas_precios()
{
     String [] rutas = new String[5]; // Se definen arreglo

    for (int i = 0; i<5; i++)
    {
    rutas[i] = JOptionPane.showInputDialog((i+1)+"-Digite ruta"); // Se ingresan rutas.
    }

    for(int i = 0; i<rutas.length; i++)
    {
    JOptionPane.showMessageDialog(null, "Las rutas son:\n"+(i+1)+"-"+rutas[i]);//Se imprimen rutas.
    }

    int [] precios = new int[5];// Se define arreglo

    for( int i =0; i<precios.length; i++)
    {
    precios[i] = Integer.parseInt(JOptionPane.showInputDialog((i+1)+" -Digite precio"));// Se ingresan precios

    }

    for(int i =0; i<5; i++)
    {
    JOptionPane.showMessageDialog(null, "Los precios son:\n"+(i+1)+"-"+precios[i]);// Se imprimen precios
    }

    for(int i =0; i<5; i++)
    {
    JOptionPane.showMessageDialog(null, "Ruta"+(i+1)+"-"+rutas[i]+ "\n"+"Precio "+precios[i]);// Se concatenan rutas y precios
    }
}
  public static void simulacion()
    { 
        String pasajeros;
        int entero;

        pasajeros = JOptionPane.showInputDialog("Ingrese numeros de pasajeros");

        if(pasajeros.matches("[0-9]*"))
        {
        entero = Integer.parseInt(pasajeros);



        }
        else
        {
        JOptionPane.showMessageDialog(null, "Ingrese un numero");
        }

 }

  public static void ganancia()
  {
  JOptionPane.showMessageDialog(null, "Aún en construcion");
  }


   public static void unidades()
   {
   JOptionPane.showMessageDialog(null, "Aún en construcion");
   }
}

In the simulation method (), I need a value of the arrays of name of conductors, a value of the array of the plates and a value of the array of prices and routes, the times that I tried to call it, the method is executed again and I have to re-enter the values.

    
asked by Carlos Alberto Morales Ruiz 10.03.2018 в 02:59
source

1 answer

1

What could be done to have the arrays available between methods of the same class is to define them in the following way:

public class Proyecto_progra1 {

    private static String[] placas;
    private static String[] rutas;
    private static int[] precios;
    private static String[] conductores; // En tu código figura como nombres pero lo puse conductores por un tema de claridad

Then you can load these arrays with each of the options that appear in the menu. Finally when you arrive at the method called simulation if you loaded them with the other options previously, the values of each will already be available, otherwise what you should do is request the load (that is, if you request the loading of data it is because you did not choose the other menu options previously).

Taking as reference the code that you have, I leave you an example of what it would be like:

package programa;

import javax.swing.JOptionPane;

public class Proyecto_progra1 {

    private static String[] placas;
    private static String[] rutas;
    private static int[] precios;
    private static String[] conductores;


    public static void nombre_conductores()
    {
         conductores= new String[5];
         for( int i = 0; i<5; i++)
         {
            conductores[i]  = JOptionPane.showInputDialog((i+1)+"-Digite nombre");// Se ingresan nombres de los choferes

         }

         mostrarconductores();


    }



    public static void placas_buses()
     {
        placas = new String[6]; // Se define arreglo

        for (int i = 0; i<6; i++)
        {
        placas[i] = JOptionPane.showInputDialog((i+1)+"-Digite numeros de placas"); // Se ingresan el numeros de las placas
        }


        mostrarplacas();
     } 

    public static void mostrarplacas()
    {
         for(int i = 0; i<placas.length; i++)
            {
            JOptionPane.showMessageDialog(null, "Las placas son:\n"+(i+1)+"-"+placas[i]);// Se imprimen numeros de placa
          }
    }
    public static void mostrarrutas()
    {
        for(int i = 0; i<rutas.length; i++)
        {
        JOptionPane.showMessageDialog(null, "Las rutas son:\n"+(i+1)+"-"+rutas[i]);//Se imprimen rutas.
        }

    }
    public static void mostrarprecios()
    {

          for(int i =0; i<precios.length; i++)
            {
            JOptionPane.showMessageDialog(null, "Los precios son:\n"+(i+1)+"-"+precios[i]);// Se imprimen precios
            }
    }

    public static void mostrarconductores()
    {
         for (int i = 0; i<conductores.length; i++)
         {
         JOptionPane.showMessageDialog(null, "Los nombres son:\n"+(i+1)+"-"+conductores[i]); // Se imprimen el nombre de los choferes
         }

    }

    public static void rutas_precios()
    {
        rutas = new String[5]; // Se definen arreglo

        for (int i = 0; i<5; i++)
        {
        rutas[i] = JOptionPane.showInputDialog((i+1)+"-Digite ruta"); // Se ingresan rutas.
        }
        mostrarrutas();

        precios = new int[5];// Se define arreglo

        for( int i =0; i<precios.length; i++)
        {
        precios[i] = Integer.parseInt(JOptionPane.showInputDialog((i+1)+" -Digite precio"));// Se ingresan precios

        }    
        mostrarprecios();

    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String opcion;
        int num = 0;

        do{

        opcion = JOptionPane.showInputDialog("1- Nombre de conductores.\n"
                +"2- Placas de autobuses.\n"+"3- Rutas y precios. \n"+"4- Simulacion \n"
        +"5- Ganancias del dia.\n"+"6- Unidades y rutas\n"+"7- Salir");//Opciones para el usuario.

        if (opcion.matches ("[0-9]*"))//Se valida valor ingresado si es numero o letra
        {
        num = Integer.parseInt(opcion);//

        switch (num)
        {
            case 1 : nombre_conductores(); //Llamar metodo de nombre de conductores
            break;
            case 2: placas_buses(); //Llamar metodo donde se almacenan las placas de los buses.
            break;
            case 3: rutas_precios();// Llamar metodo donde se definen las rutas y los precios.
            break;
            case 4: simulacion();
            break;
            case 5: ganancia();
            break;
            case 6: unidades();
            break;
            case 7: System.exit(0);//Salir del programa
            break;
            default: JOptionPane.showMessageDialog(null, "Digite algunas de las opciones disponibles.");
        }
        }
        else

    {
    JOptionPane.showMessageDialog(null, "Digite un numero");
    }
        } while(num!=4);//Se encicla programa hasta que el usuario decida salir.
    }

      public static void simulacion()
        { 
            String pasajeros;
            int entero;

            pasajeros = JOptionPane.showInputDialog("Ingrese numeros de pasajeros");

            if(pasajeros.matches("[0-9]*"))
            {
            entero = Integer.parseInt(pasajeros);

            if (conductores!=null)
                     mostrarconductores();
            else nombre_conductores();

            if (placas!=null)
                  mostrarplacas();
            else  placas_buses();

            if (rutas==null && precios==null)
                rutas_precios();
            else {       
                     mostrarrutas();
                     mostrarprecios();
             }


            }
            else
            {
            JOptionPane.showMessageDialog(null, "Ingrese un numero");
            }

    }

      public static void ganancia()
      {
      JOptionPane.showMessageDialog(null, "Aún en construcion");
      }


       public static void unidades()
       {
       JOptionPane.showMessageDialog(null, "Aún en construcion");
       }



}

The small modification I made for a reuse issue is that you create the following methods to print the arrays, that adapt as you prefer.

  • show conductors ();
  • displayplates ();
  • showcases ();
  • show prices ();
  • Inside the simulation method, I put this code so that if you did not choose the load of some you request it directly inside this method, otherwise it will print the values loaded for each array on the screen.

             if (conductores!=null)
                     mostrarconductores();
            else nombre_conductores();
    
                if (placas!=null)
                      mostrarplacas();
                else  placas_buses();
    
                if (rutas==null && precios==null)
                    rutas_precios();
                else {       
                         mostrarrutas();
                         mostrarprecios();
                 }
    

    Greetings and I hope it is of your use.

        
    answered by 10.03.2018 в 05:24