Evan
Evan

Reputation: 374

How can I make this more efficient?

My program gives a String answer based on comparisons of Integer input. Would there be a more efficient way of doing this instead of prompting the user 3 separate times and calling numberVerify three separate times? How would I do this? Very new to Java.

public static void main(String[] args) {
    // TODO code application logic here
    // declare variables
    int number=0;
    System.out.println("Enter the first integer: ");
    int number1 = numberVerify(number);
    System.out.println("Enter the second integer: ");
    int number2 = numberVerify(number);
    System.out.println("Enter the third integer: ");
    int number3 = numberVerify(number);
    String compare = comparison(number1, number2, number3);
    printer(compare);
}
public static int numberVerify(int number){
    Scanner in = new Scanner(System.in);
     if(in.hasNextInt()){
        number = in.nextInt();
    }

    else{

        System.out.println("Error! Input must be an integer!");   

        System.out.println("");

        return numberVerify(number);

    }
    return number;
}

public static String comparison(int number1, int number2, int number3){
    String answer;
    if(number1 == number2 && number1 == number3)
    {       
        answer = "all the same";
    }
    else if(number1 != number2 && number1 != number3)
    {       
        answer = "all different";
    }
    else
    {       
        answer = "neither";
    }
    return answer;
}

public static void printer(String answer){
    System.out.println(answer);
}

Upvotes: 1

Views: 112

Answers (5)

Stephen C
Stephen C

Reputation: 719739

Efficiency is irrelevant here.

The user is going to take SECONDS to enter the number. The OS could take MILLISECONDS to do the keyboard event processing, screen redrawing etc. Your Java application is going to take MICROSECONDS or less to do the decision making. Any effort you expend to reduce the microsecond component is wasted effort.

If there are issues to be addressed here (and I'm not saying there are) they would be:

  • making the user interface easy to use.

  • writing the code so that it is easy to read and easy to maintain.

  • writing the code to be "elegant" ... which is not necessarily the same as the previous.


UPDATE - Apparently you are actually interested in simplifying and/or reducing the size of the code.

Firstly, that's NOT what most people mean by efficiency. (Indeed, I would argue that spending time on improving code that is good enough is inefficient use of your time.)

Secondly, simplicity and code size are not the same things. Most people (programmers) would say that simple code is code that is easy to understand ... for the average programmer. Often, a more concise solution to a problem will actually be difficult to understand. (Take @rby's code for example. If you needed to be sure it was correct without testing it, you would need to read it very carefully.)

Finally, professional software engineers follow the general principle that the code needs to be "good enough for purpose"1 ... but no better. Perfect is the enemy of good. Spending your time making a program better than it needs to be is wasting your time, and liable to cost you / your client / your employer money.

1 - The purpose may include performance criteria, readability / maintainability, generality / resuability, and various specific correctness criteria. But that is not necessarily the case. Often, code is written for a context where many of those criteria are irrelevant or even counter-productive. But I digress ....

Upvotes: 3

rby
rby

Reputation: 786

You can use the following single line to replace your entire if blocks in method comparison and return answer as you do:

answer =  ( (n1 == n2) ? 
            ( (n2 == n3) ? "all equal" : "neither") : 
                ( n1 == n3) ? "neither" : ( ( n2 == n3 ) ? "neither" : "all different" ) ); 
return answer;

Upvotes: 1

Blue
Blue

Reputation: 557

I don't know about more efficient or readable, it seems that way already. But I can make it shorter.

public static void main(String[] args) {
    // TODO code application logic here
    // declare variables
    Scanner in = new Scanner(System.in);
    int[] numbers = new int[3];
    String[] labels = {"first","second","third"};
    for(int i=0; i<3; i++)
    {
        System.out.print("Enter the " + labels[i] + " integer: ");
        numbers[i] = in.nextInt();
    }
    System.out.println(comparison(numbers[0],numbers[1],numbers[2]));
}

public static String comparison(int number1, int number2, int number3){
    String answer;
    if(number1 == number2 && number1 == number3)
    {       
        answer = "all the same";
    }
    else if(number1 != number2 && number1 != number3 && number2 != number3)
    {       
        answer = "all different";
    }
    else
    {       
        answer = "neither";
    }
    return answer;
}

Upvotes: 0

Karuna
Karuna

Reputation: 739

You can use arrays and little other modifications to make it efficient.

Here is the code.

import java.util.Scanner;

public class efficient {

private static String comparison(int[] numberArray) {
    String answer;
    if(numberArray[0] == numberArray[1] && numberArray[1] == numberArray[2]){
        answer = "all the same";
    }else if(numberArray[0] != numberArray[1] && numberArray[1] != numberArray[2]){
        answer = "all different";
    }else{
        answer = "neither";
    } return answer;

}

public static void main(String[] args) {

    int[] numberArray = new int[3];
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the numbers");
    for(int i=0;i<=numberArray.length-1;i++){
        if(scan.hasNextInt()){
            numberArray[i]=scan.nextInt();
        }else{
            System.out.println("Error! Input must be an integer!");   
        }
    }       
    String compare = comparison(numberArray);
    System.out.println(compare);


}

}

Upvotes: 0

shad
shad

Reputation: 124

Use an int array and a for loop instead say :

int [] values = new int[3];
for(int i =0; i<3; i++){
  values[i] = in.nextInt();
}

Then modify your compare method to use the array;

For brownie points look up Sets and figure how you can eliminate most of your comparison method with them.

Upvotes: 1

Related Questions