lundi 22 mai 2017

Issue with comparitor in a if/else statement in Java

import java.util.HashMap;

public class Driver {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    String location = "memes more memes Virginia";

    String test = createStateMap(location);
    System.out.println(test);
}

public static String createStateMap(String loc) {

    loc = loc.toUpperCase();
    String secondLast; // Retrieves potential second to last value 
    String lastWord; // Retrieves last value 
    String state = "failed";
    String trimmed = loc.trim(); // Take off spaces at the end of String
    String o = trimmed.substring(0, trimmed.lastIndexOf(" ")); // Remove last Word in String   EX. "SOUTH CAROLINA" = "SOUTH"
    String otrim = o.trim(); // Take off spaces at the end of String

    if (trimmed.contains("/")){
    secondLast = otrim.substring(otrim.lastIndexOf("/")+1); 
    lastWord = trimmed.substring(trimmed.lastIndexOf("/")+1);
    } else { 
        secondLast = otrim.substring(otrim.lastIndexOf(" ")+1); 
        lastWord = trimmed.substring(trimmed.lastIndexOf(" ")+1);
    }




    HashMap<String,String> names = new HashMap<String,String>();
       names.put("ALABAMA", "AL");
       names.put("ALASKA", "AK");
       names.put("ARIZONA", "AZ");
       names.put("ARKANSAS", "AR");
       names.put("CALIFORNIA", "CA");
       names.put("COLORADO", "CO");
       names.put("CONNECTICUT", "CT");
       names.put("DELAWARE", "DE");
       names.put("FLORIDA", "FL");
       names.put("GEORGIA", "GA");
       names.put("HAWAII", "HI");
       names.put("IDAHO", "ID");
       names.put("ILLINOIS", "IL");
       names.put("INDIANA", "IN");
       names.put("IOWA", "IA");
       names.put("KANSAS", "KS");
       names.put("KENTUCKY", "KY");
       names.put("LOUISIANA", "LA");
       names.put("MAINE", "ME");
       names.put("MARYLAND", "MD");
       names.put("MASSACHUSETTS", "MA");
       names.put("MICHIGAN", "MI");
       names.put("MINNESOTA", "MN");
       names.put("MISSISSIPPI", "MS");
       names.put("MISSOURI", "MO");
       names.put("MONTANA", "MT");
       names.put("NEBRASKA", "NE");
       names.put("NEVADA", "NV");
       names.put("NEWHAMPSHIRE", "NH");
       names.put("JERSEY", "NJ");
       names.put("MEXICO", "NM");
       names.put("YORK", "NY");
       names.put("CAROLINA", "NC");
       names.put("DAKOTA", "ND");
       names.put("OHIO", "OH");
       names.put("OKLAHOMA", "OK");
       names.put("OREGON", "OR");
       names.put("PENNSYLVANIA", "PA");
       names.put("RHODEISLAND   ", "RI");
       names.put("CAROLINA", "SC");
       names.put("DAKOTA", "SD");
       names.put("TENNESSEE", "TN");
       names.put("TEXAS", "TX");
       names.put("UTAH", "UT");
       names.put("VERMONT", "VT");
       names.put("VIRGINIA", "VA");
       names.put("WASHINGTON", "WA");
       names.put("VIRGINIA", "WV");
       names.put("WISCONSIN", "WI");
       names.put("WYOMING", "WY");

       System.out.println(secondLast);
    if (names.containsValue(lastWord)){
        state = lastWord;
    } 
    if (names.containsKey(lastWord)){
        if (names.get(lastWord).equals("CAROLINA")){ // Differentiate NC and SC
            if (secondLast.equals("North")){
                state = "NC";
            } else { state = "SC"; }

        } if (names.get(lastWord).equals("DAKOTA")) { // Differentiate ND and SD
            if (secondLast.equals("North")){
                state = "ND";
            } else { state = "SD"; }

        } if (names.get(lastWord).equals("VIRGINIA")) { // Differentiate WV and VA
        if (secondLast.equals("West")){
                state = "WV";
            } else { state = "VA"; }

        } else { state = names.get(lastWord); } 
        }

      return state;
      }
     }

Hey guys, I am currently having issues with my code that assigns a state's abbreviations to a variable depending on the String passed into the parameter of the createStateMap method. When the lastWord String is either Virginia, Dakota, or Carolina they will always be assigned WV, ND, or NC- even if the secondLast is not North or West. Any help would be much appreciated, I have been stuck on this one for a while.

Aucun commentaire:

Enregistrer un commentaire