0

How do i make my program only use one if statement and an else statement?

import java.io.*;

public class TwoNum {
    public static void main(String[] args){
        String first="";
        String second="";

        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

        try{
            System.out.print("Input the first number: ");
            first = in.readLine();

            System.out.print("Input the second number: ");
            second = in.readLine();

        } catch(IOException e){
            System.out.println("Error!");
        }

        int number1=Integer.parseInt(first);
        int number2=Integer.parseInt(second);

        if(number1==number2){
            System.out.println("EQUIVALENT");
        }
        if(number1>number2){
            System.out.println("GREATER THAN");
        }
        if(number1<number2){
            System.out.println("LESSER THAN");
        }
    }
}
Zhianc
  • 1,411
  • 3
  • 20
  • 37
  • There are three different outputs. With one if and an else you can handle two. You will need another else-if. – taskinoor Jul 20 '11 at 12:15
  • 1
    Why the bloated code dump? Anyway, you have three code paths, so you'll need some to branch two times one way or another. You can use the ternary conditional operator `?:` to write it all in one line, though. – Kerrek SB Jul 20 '11 at 12:16
  • @Kerrek SB, what do you mean **Why the bloated code dump?** – Zhianc Jul 20 '11 at 12:18
  • I mean that it would have sufficed to show us the actual `if` statements that you want to improve without the entire ambient program text. Ask yourself, "do they really need to know all this"? – Kerrek SB Jul 20 '11 at 12:21
  • Thanks all for helping, already found the solution to the prob ^_^ I'm so glad to get such fast replies to my problem. – Zhianc Jul 20 '11 at 12:23
  • @jc david, You should be able to do this sort of assignment by yourself. It's kind-of pitiful. – Moonbeam Jul 20 '11 at 12:26
  • I am asking for help as for these i will be able to learn :) – Zhianc Jul 20 '11 at 12:29

7 Answers7

3
if(number1==number2){

  System.out.println("EQUIVALENT");
 }
 else if(number1>number2){

 System.out.println("GREATER THAN");
  }
 else{
 System.out.println("LESSER THAN");
}
AndroGeek
  • 1,280
  • 3
  • 13
  • 25
2

Here use this:

import java.io.*;

public class TwoNum {

public static void main(String[] args){

    String first="";
    String second="";

    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

    try{
        System.out.print("Input the first number: ");
        first = in.readLine();

        System.out.print("Input the second number: ");
        second = in.readLine();

    } catch(IOException e){
        System.out.println("Error!");
    }

    int number1=Integer.parseInt(first);
    int number2=Integer.parseInt(second);

    String result = null;

    if( number1 == number2 )
        result = "EQUIVALENT";
    else
        result = ( number1 > number2 ) ? "GREATER THAN" : "LESS THAN";

    System.out.println( result );

    }
}
quaertym
  • 3,917
  • 2
  • 29
  • 41
  • that seems to be outputting EQUIVALENT
    null when both numbers are the same.
    – Zhianc Jul 20 '11 at 12:33
  • what should it output when two numbers are EQUIVALENT? – quaertym Jul 20 '11 at 12:37
  • only the world EQUIVALENT, but the code above displays **EQUIVALENT** then **null** on another line when the two numbers which were inputted are the same. – Zhianc Jul 20 '11 at 12:39
  • no, it does not output null, you should read code more carefully, it is so simple. – quaertym Jul 20 '11 at 12:43
  • Ah yeah, just missed a bit of the code. thank you! this was the real answer, LoL. – Zhianc Jul 20 '11 at 12:45
  • In my opinion, using the '?' operator is only going around the problem statement. When you goal is to avoid if statement, you want to avoid a branch/goto in the assembly code generated (ok, it is Java, but it can also be applied in C), and '?' sill needs a branch. This allows the assembly code to run faster. The Exception solutions, including mine, still have a lot of branches, so they might even not qualify. I think the real answer is the solution with the bitwise logic. – jfg956 Jul 21 '11 at 08:08
1

A variant of qbert solution, using 'java.lang.ArithmeticException' and without allocating memory:

try {
  1 / (number2-number1);

  if(number1 > number2){
    System.out.println("GREATER THAN");
  } else {
    System.out.println("LESSER THAN");
  }      
}
catch (ArithmeticException e) {
  System.out.println("EQUIVALENT");
}
jfg956
  • 16,077
  • 4
  • 26
  • 34
1

Another solution, in my opinion much better than the 'ArithmeticException' solution I previously submitted:

String res;
int i = number2 - number1;
if (i == 0) {
    res = "EQUIVALENT";
} else {
    String RES[] = { "GREATER THAN", "LESSER THAN" };
    int j = (i & (1 << 31)) >> 31;
    res = RES[j+1];
}
System.out.println(res);

To explain this a little, when 'number1' is gt 'number2', 'i' is negative. The leftmost bit of a number is 1 when it is negative, 0 otherwise. So I get this bit, with 'i & (1 << 31)' shit it right of 31, which gives me -1 for a negative number, 0 otherwise. And then I only have to do the array lookup to get the result.

jfg956
  • 16,077
  • 4
  • 26
  • 34
0

try this

String msg="";
msg = ((number1==number2) ? "number1 and number2 is equal" : ((number1>number2) ? "number1 is greater than number2" : "number2 is greater than number1"));
System.out.println(msg);

without if using ternary operator

Pratik
  • 30,639
  • 18
  • 84
  • 159
0

My idea, avoiding ternary operator. It allocates an array, catching the exception if the array size is negative.

import java.io.*;

public class TwoNum {
    public static void main(String[] args){
        String first="";
        String second="";

        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

        try{
            System.out.print("Input the first number: ");
            first = in.readLine();

            System.out.print("Input the second number: ");
            second = in.readLine();

        } catch(IOException e){
            System.out.println("Error!");
        }

        int number1=Integer.parseInt(first);
        int number2=Integer.parseInt(second);

        try {
            int c[] = new int[number2-number1];
            if(number1==number2){
                System.out.println("EQUIVALENT");
            } else {
                System.out.println("LESSER THAN");
            }
        }
        catch (Exception e) {
            System.out.println("GREATER THAN");
        }
    }
}
qbert220
  • 11,220
  • 4
  • 31
  • 31
  • Not the downvoter, but it's highly unnecessary to create an array for this problem, whether it works or not. – rtheunissen Jul 20 '11 at 14:08
  • I'm not claiming it's elegant or efficient. It's not a solution I'd use in a piece of production software. But then I'm not normally restricted to only using one `if` and one `else`. It is an artificial homework situation and this is a novel (IMHO), working solution, so I really don't understand the down vote. – qbert220 Jul 20 '11 at 15:22
  • Granted, but `ArithmeticException` as shown by *jfgagne* is basically the same idea but would be much more efficient. I'm sure you know this, so I guess the downvote is just because it's beyond the scope of what was asked. I'll upvote. – rtheunissen Jul 20 '11 at 15:30
  • Agreed - jfgagne's is more efficient. And will work with bigger numbers. I have used a similar technique to do compile time checking of constants in C before (the C compiler barfs if you try to allocate an array of negative size). – qbert220 Jul 20 '11 at 15:39
  • I do not think that this solution is that inefficient: as 'c' is never used, it will be taken back by the garbage-collector very quickly. Maybe some little improvements could be made to make it more bulletproof: catch the 'OutOfMemoryError' than can be generated by the new, and doing assignment instead of println in the try/catch and println out of the try/catch to avoid catching an exception thrown by println. In all cases, this solution is much better than the one with the '?' operator, that is basically a hidden if which everybody should know of. – jfg956 Jul 21 '11 at 08:23
0

String result = (number1==number2) ? "EQUIVALENT" : ((number1 > number2) ? "GREATER THAN" : "LESS THAN");

rtheunissen
  • 7,347
  • 5
  • 34
  • 65