简体   繁体   中英

Return array to another class

Help me, I want to return result of HashMap But Show error in the end 'return lg'

public static void main(String[] langCookies) {

    if (langCookies.equals("ind")) {  
        Map<String, String> lg = new HashMap<>();
        lg.put("Login", "Masuk");
    } else if (langCookies.equals("eng")) {  
        Map<String, String> lg = new HashMap<>();
        lg.put("Login", "Login");
    } else {
        Map<String, String> lg = new HashMap<>();
        lg.put("Login", "Login"); 
    }

    return lg;    
}

Ok, so... There are multiple things wrong with your code snippet.

First of all, scope of variables. The HashMap that you want to return in your method is defined (and redefined) in every block of your if-clause. This then means, that its only valid in this very specific block. So even if you were able to return stuff in your main method, the HashMap woudn't even be valid there.

Second, you need to understand what the main method is for. It is the entry point of your program, and is only meant to do basic setup that needs to be done immediately, and then call other methods that do the heavy lifting for you. Because it is the entry point for every program in java, it has a fixed method signature, which is as follows:

public static void main(String[] args) {}

In your case, pay special attention to the word void , which means no return type, as in nothing can be returned. And because you're not allowed to change the signature of the main method, because then your program woudn't start anymore, there is no way to return anything in the main method itself.

What you could do is the following:

public static void main(String args) {
    //here comes generic stuff you may need to do on startup
    //also, you need to get your langCookies from somewhere, i suppose its meant to  
    //be passed as a parameter on startup?
    HashMap<String, String> langCookies = processLangCookies(args);
    //do stuff with langCookies, or whatever you want to call that variable.
}

public static HashMap<String, String> processLangCookies(String[] langCookies) {
    HashMap<String, String> lg = new HashMap<String, String>();
    if(langCookies.equals("ind")) {  
        lg.put("Login", "Masuk");
    } else if(langCookies.equals("eng")) {  
        lg.put("Login", "Login");
    } else {
        lg.put("Login", "Login"); 
    }
    return lg;
}

The above code is mainly based on assumptions though, because you didn't write, a) what exactly you wanted to do, b) what exactly your error was. You only wrote "there was an error there and there". As already asked in the comments on your question, what error are you talking about? What kind of error? In this case, it is fairly obvious to be honest, but in other cases, it may not be like this. So if you don't thoroughly describe your error and the expected behaviour and the wrong behaviour you got, you may just get a load of irrelevant answers, or none at all.

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