vendredi 25 juin 2021

what is the scope of string Variable in a JAva Method? [duplicate]

I have a simple Java method here Below , which passes a string , but inside the method another string variable is to be assigned a value. But its not assigning new value- based on the String Input. Instead returning "NA".

Kindly have a Look and help me find the fault in the code. Suppose if h is "NRM" even then aId = "NA", not "01"

 private static String getAID(String h) {

    System.out.print("AId : 01 to 25 for "+h);     
     String aId = "NA";
     if(h == "NRM") {
        aId= "01";
        System.out.print("inside Nrm of fn"+aId);
     } 
     Scanner scanner = new Scanner(System.in);
     if(h=="EPB") {
            System.out.print("Emergency ON/OFF press 1 or 0  - ");
             String in= scanner.next();
             if(in == "0") aId="11";
             else if(in=="1") aId="10";
             else { aId="10";}           
        }
     if (h=="CRT") {
            aId="16";
            System.out.print("Critical Alert Enter 16/03/17/22/23/20/21 - ");
            String in= scanner.next();
            String[]  arr = {"16","03","17","22","23","20","21","13","14","15","06","04","05","09","18","19"};
            for(String i : arr) {
                if( i.equals(in) ) aId=in;
            }   //aId="04";   
    }
    if(h=="BTH" || h=="ALT") {
            aId="13";
            System.out.print("AId Enter :/13/14/15/06/04/05/09/18/19 - ");
            String in= scanner.next();
            String[] arr = {"16","03","17","22","23","20","21","13","14","15","06","04","05","09","18","19"};
            for(String i : arr) {
                if( i.equals(in) ) aId=in;
            }               
    
    }
    switch(aId) {
    case "01":System.out.println("Normal 2"); 
        break;
    case "02":System.out.println("l History");
    // remaining cases

    // remaing cases //

    case "23":System.out.println("IMPACT"); 
    
    default: System.out.println(" Wrong Entry -is"+aId ); 
    }
    
    return aId;
    
}

Here when I checked , It s never Entering the If Statements at all... But instead entering default part of the Switch case and return aId = "NA" ;

Aucun commentaire:

Enregistrer un commentaire