简体   繁体   中英

Java. Method won't return array

I'm trying to return an array from a method; it's not working. I'm getting the info of the array from a text file. The array gets the info successfully during the method, but won't return it to the main.

import javax.swing.JFrame;

public class TerminalVenta {

    public static void main (String[] args){
        Log k = new Log();
        String[][]array = new String[5][3];
        k.abrirLog();
        k.leerArchivo(array);
        System.out.println(array[0][1]);
        k.closeFile();
    }
}


import java.io.*;
import java.util.*;

public class Log {
    private Scanner x;
    String arreglo[][] = new String [5][3];

    public void abrirLog() {
        try {
            x = new Scanner(new File ("Log.txt"));
        }
        catch(Exception e) {
            System.out.println("No se pudo abrir el archivo");
        }
    }

    public String[][] leerArchivo(String array[][]) {
        String a = x.next();
        String b = x.next();
        String c = x.next();
        String d = x.next();
        String e = x.next();
        String f = x.next();
        String g = x.next();
        String h = x.next();
        String i = x.next();
        String j = x.next();
        String k = x.next();
        String l = x.next();
        String m = x.next();
        String n = x.next();
        String o = x.next();

        arreglo[0][0] = a;
        arreglo[0][1] = b;
        arreglo[0][2] = c;
        arreglo[1][0] = d;
        arreglo[1][1] = e;
        arreglo[1][2] = f;
        arreglo[2][0] = g;
        arreglo[2][1] = h;
        arreglo[2][2] = i;
        arreglo[3][0] = j;
        arreglo[3][1] = k;
        arreglo[3][2] = l;
        arreglo[4][0] = m;
        arreglo[4][1] = n;
        arreglo[4][2] = o;

        System.out.println(arreglo[0][0]);
        System.out.println(arreglo[0][1]);
        return arreglo;
    }

    public void closeFile() {
        x.close();
    }
}

I'm trying to return an array from a method but is not working,

Most likely you are not getting a result as you are ignoring the value returned. If you don't want to discard the returned value you can do this.

String[][]result = leerArchivo(array);

if you do just

leerArchivo(array);

it will discard the value returned.

Note: you are also ignoring the array passed in. You can drop this as it is not being used.

You are returning the array in leerArchivo, but you are not capturing it. In fact, you are asking for an array in that function, but doing nothing with it either. You should change

public String[][] leerArchivo(String array[][]){

to

public String[][] leerArchivo(){

and change

k.leerArchivo(array);

to

array=k.leerArchivo();

You are not using the returned value. You are updating a different array and not the one the sent to the leerArchivo method. Without changing much of your code, this is what you can try:

String[][]array = new String[5][3];
k.abrirLog();
String[][] retArray = k.leerArchivo(array);
System.out.println(retArray[0][1]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM