Vinoth
Vinoth

Reputation: 1359

Code efficiency. Effective way of using loops

I am building a framework for a web application and would like to perform certain simple tasks like input retrieval and displaying them. Below is have the code, I feel the code can be improved and isn't efficient. Note:All three methods have the same implementation.

The Switch needs to be implemented twice one within the IF loop and the other for general case.

    public class selector8
{
      public static void main (String [] args){

    selector8 obj1 = new selector8();
    Scanner inputString = new Scanner (System.in);
    Scanner inputYN = new Scanner (System.in);
    String input, A,B,C;

    System.out.println("Choose. A || B || C || X to exit");
    input = inputString.nextLine();
    char contLoop1 = 'y';
    do{
        if(input.equalsIgnoreCase("y")) {
            input = "";
            System.out.println("Choose. A || B || C || X to exit");
            input = inputString.nextLine();
            switch (input){

            case("a"):
                obj1.startMethod1(input);
                break;

            case("b"):
                obj1.startMethod2(input);
                break;

            case("c"):
                obj1.startMethod3(input);
                break;

            case("x"):
                System.out.println("Goodbye");
                break;

            case ("n"):
                System.out.println("See ya");
                break;

            default:
                    System.out.println("Invalid argument. Try again");
                    break;

                }
        } else
        switch (input){

        case("a"):
            obj1.startMethod1(input);
            break;

        case("b"):
            obj1.startMethod2(input);
            break;

        case("c"):
            obj1.startMethod3(input);
            break;

        case("x"):
            System.out.println("Goodbye");
            break;

        case ("n"):
            System.out.println("See ya");
            break;

        default:
                System.out.println("Invalid argument. Try again");
                break;

            }
            System.out.println("Do want to try again " +"Y/N" );
            input = inputYN.nextLine(); 
            contLoop1 = input.charAt(0);
    }while(contLoop1 != 'n');
}

public void startMethod1(String A){
    String input;

    Scanner inputString = new Scanner (System.in);
    System.out.println("Enter a or b");
    input = inputString.nextLine();

    switch (input){

    case("a"):
        System.out.println("output parsed");
    break;

    case("b"):
        System.out.println("output parsed");
    break;

    default:
        System.out.println("Invalid argument");
    }
}

public void startMethod2(String B){

    String input;
    Scanner inputString = new Scanner (System.in);
    System.out.println("Enter a or b");
    input = inputString.nextLine();
    switch (input){
    case("a"):
        System.out.println("output parsed");
    break;
    case("b"):
        System.out.println("output parsed");
    break;

    default:
        System.out.println("Invalid argument");
        break;
    }
}

public void startMethod3(String C){

    String input;
    Scanner inputString = new Scanner (System.in);
    System.out.println("Enter a or b");
    input = inputString.nextLine();
    switch (input){
    case("a"):
        System.out.println("output parsed");
    break;
    case("b"):
        System.out.println("output parsed");
    break;

    default:
        System.out.println("Invalid argument");
    }
}
}

On a side note could you also tell me why can static classes be called without an object?

public class StaticImplementation{
public static void main (String [] args){
StaticImplementation obj1 = new StaticImplementation();
obj1.stat(); //is invalid
stat(); //is valid
}
static void stat(){
    System.out.println("Static class");
}

Thanks a bunch. Have a good day!

Upvotes: 0

Views: 125

Answers (3)

Guy Gavriely
Guy Gavriely

Reputation: 11396

using Java you'd probably take a more "Object Oriented" approach and leave those switches to C programming :)

do something like:

interface Action {
    void start();
}

class Jumper implements Action {
    public void start() {
        System.out.println("Jump!");
    }
}

class Sitter implements Action {
    public void start() {
        System.out.println("Sit!");
    }
}

class Runner implements Action {
    public void start() {
        System.out.println("Run!");
    }
}

class Test {
    public static void main(String[] args) {
        // init phase
        Map<String, Action> map = new HashMap<String, Action>();
        map.put("a", new Jumper());
        map.put("b", new Runner());

        // usage
        map.get("a").start();
    }
}

you can have two Maps one for Y and the other for the rest, you can also have a key composed from the two strings Pair

Upvotes: 2

Infinite Recursion
Infinite Recursion

Reputation: 6557

Try this:

public class Selector8 {
    public static void main(String[] args) {

        Selector8 obj1 = new Selector8();
        Scanner inputString = new Scanner(System.in);
        Scanner inputYN = new Scanner(System.in);
        String input, A, B, C;

        System.out.println("Choose. A || B || C || X to exit");
        input = inputString.nextLine();
        char contLoop1 = 'y';
        while (contLoop1 != 'n') {
            if (input.equalsIgnoreCase("y")) {
                System.out.println("Choose. A || B || C || X to exit");
                input = inputString.nextLine();
            } 
            switch (input) {

            case ("a"):
            case ("b"):
            case ("c"):
                obj1.startMethod();
                break;
            default:
                System.out.println(validate(input));
                break;

            }
            System.out.println("Do want to try again " + "Y/N");
            input = inputYN.nextLine();
            contLoop1 = input.charAt(0);
        }
    }

    public static String validate(String input) {
        if (input.equalsIgnoreCase("x"))
            return "Goodbye";

        if (input.equalsIgnoreCase("n"))
            return "See ya";

        return "Invalid argument. Try again";
    }

    public void startMethod() {
        Scanner inputString = new Scanner(System.in);
        System.out.println("Enter a or b");
        String input = inputString.nextLine();

        switch (input) {
        case ("a"):
        case ("b"):
            System.out.println("output parsed");
            break;
        default:
            System.out.println("Invalid argument");
        }
    }
}

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201497

Oh yes, it can. You don't need two scanners, and I would extract your switch into a method (let's call it startMethods) like so -

private static void startMethods(selector8 obj1,
    String input) {
  switch (input) {
  case ("a"):
    obj1.startMethod1(input);
    break;
  case ("b"):
    obj1.startMethod2(input);
    break;
  case ("c"):
    obj1.startMethod3(input);
    break;
  case ("x"):
    System.out.println("Goodbye");
    break;
  case ("n"):
    System.out.println("See ya");
    break;
  default:
    System.out.println("Invalid argument. Try again");
    break;
  }
}

public static void main(String[] args) {
  selector8 obj1 = new selector8();
  Scanner inputString = new Scanner(System.in);
  String input, A, B, C;

  System.out.println("Choose. A || B || C || X to exit");
  input = inputString.nextLine();
  char contLoop1 = 'y';
  do {
    if (input.equalsIgnoreCase("y")) {
      input = "";
      System.out.println("Choose. A || B || C || X to exit");
      input = inputString.nextLine();
      startMethods(obj1, input);
    } else
      startMethods(obj1, input);
    System.out.println("Do want to try again "
        + "Y/N");
    input = inputString.nextLine();
    contLoop1 = input.charAt(0);
  } while (contLoop1 != 'n');
}

Upvotes: 1

Related Questions