0

this is my very first post ever here. I am VERY new at programming, started learning java like a week ago, but its very exciting so far.

I would like to hear your feedback on this code. Basically i want it to output the abbreviation of the state name, after inputting the name and visa versa...

Is there a more efficient way to write this? (Efficient as in least time for the machine to process the code)

import java.util.Scanner;

class apples2{


    public static void main(String[] args){

        Scanner inputStateName = new Scanner(System.in);

        System.out.println("Input the state name or abbreviation: ");

        String stateName = inputStateName.nextLine();

        for(int counter = 0; counter <= statedata.states.length; counter++){

            if(stateName.equalsIgnoreCase(statedata.states[counter])){
                System.out.println("The abbreviation of your state is: " + statedata.statesAb[counter]);

                break;
            }

            if(stateName.equalsIgnoreCase(statedata.statesAb[counter])){
                System.out.println("You've entered an abbreviation, the state for that is: " + statedata.states[counter]);
                break;
            }
        }
    }
}

"statedata.states" is an array of all state names in another class and "statedata.statesAb" is an array of the abbreviations.

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
retler
  • 3
  • 3

2 Answers2

2

Whether you use an enum or a standard class, the important point is that you shouldn't have two arrays of the same length containins the abbreviations and the names. You should model the concept you're dealing with as a class.

Your program deals with states. A state has an abbreviation and a name, so you should have a class State with two properties: name and abbreviation. And a single array of States.

This becomes critical if, for example, you need to sort the states by name. How would you do to keep the array of abbreviations in the same order than the array of names. That's quite hard. But if the name and abbreviation are kept together in an object, sorting the single array of objects by name, will keep the abbreviation with the name.

So, i short, Java is an object-oriented language, and you should thus define your own classes to model the functional domain you're dealing with.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

There are various ways to model the solution. Java Map is a good fit since it allows you to have key-value pairs (i.e. You can have state abbreviation as the key and state name as the value). It also allows you to quickly check if a given key exists, method containsKey(), or if a given value, method conatinsValue(), exists in the Map. Here is a simple example you can use to advance your learning.

public class StateTest {
    public static void main(String... args)
    {
        Map<String, String> statesMap = new HashMap<>();
        statesMap.put("Ala", "Alabama");
        statesMap.put("Alaska", "Alaska");
        statesMap.put("Calif", "California");
        statesMap.put("Tex", "Texas");
        statesMap.put("Va", "Virginia");
        //... add more states here

        Scanner inputState = new Scanner(System.in);
        System.out.println("Input the state name or abbreviation: ");
        String state = inputState.nextLine();

        if(statesMap.containsKey(state)) 
        {
            System.out.println("You've entered an abbreviation, the state for that is: "+statesMap.get(state));
        } 
        else if(statesMap.containsValue(state))
        {
            for(Map.Entry<String, String> stateEntry : statesMap.entrySet())
            {
                if(state.equals(stateEntry.getValue()))
                    System.out.println("The abbreviation of your state is: " +stateEntry.getKey());
            }
        } else {
            System.out.println("No such state.");
        }
    }
}

Is there a more efficient way to write this? (Efficient as in least time for the machine to process the code) Try modeling the solution in other ways then doing a search for a given state and see if the search is faster or slower. Check this link on how to time a method execution time. Happy learning.

Community
  • 1
  • 1
Tom Njigi
  • 138
  • 3