Finding the biggest number among 3, I ran into problems for the last part as bolded

Viewed 107
import java.util.Scanner;
class CurrencyExchange_1 {
 
public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    System.out.print("Input any 3 numbers");
    int firstnumber = sc.nextInt();
    int secondnumber = sc.nextInt();
    int thirdnumber = sc.nextInt();

    //Finding the bigger number
    if (firstnumber > secondnumber);
    if (firstnumber > thirdnumber ) {
        System.out.println("Biggest number is " +firstnumber);

    } else if (secondnumber > firstnumber);
    if (secondnumber > thirdnumber); {
        System.out.println("Biggest number is " +secondnumber);

    } **else if** (thirdnumber > firstnumber);
    if (thirdnumber > secondnumber); {
        System.out.println("Biggest number is " +thirdnumber);
    }
  }
}

For the bolded part, I originally wrote < else > only without the conditions but it was an error. Even with the code as written above, an error prompts for <'else' without 'if'>. I am not too sure where went wrong as when I completely delete the last < "else" > part, the code works perfectly. I was reading answers for other questions that <else {...}> can work perfectly when not all conditions are met but it seems to be incorrect for me. Please pardon my complicated code because I am currently learning the basics and I am required to solve it using what I know currently which is just the comparing part of programming.

Also, is it me or there is a lot of auto-correct going on? Some words keep changing back when I have clearly edited them!

Thank you all for your kindness! I finally understand my errors! Would like to thank each of y'all personally but to prevent clutter, i'm going to write it here!

6 Answers

I would suggest you use the && statement in Java's if.

By writing if(condition1 && condition2) both condition1 and condition2 must be true for the execution to enter the if.

Now you can easily rewrite your code to

import java.util.Scanner;
class CurrencyExchange_1 {
 
public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    System.out.print("Input any 3 numbers");
    int firstnumber = sc.nextInt();
    int secondnumber = sc.nextInt();
    int thirdnumber = sc.nextInt();

    //Finding the bigger number
    if (firstnumber > secondnumber && firstnumer > thirdnumber) {
        System.out.println("Biggest number is " +firstnumber);
    } else if (secondnumber > firstnumber && secondnumber > thirdnumber) {
        System.out.println("Biggest number is " +secondnumber);
    } else
        System.out.println("Biggest number is " +thirdnumber);
    }
  }
}

To answer your actual question the reason why you can not use the else statement is because you write if(...); {} and also else if(...); . In java you do not write a line terminator ; in if statements. Just write if(...) {logic}

By writing if(...); or if(...){}there will be noting to execute if the condition is true hence the if statement is useless. There should always be some logic that is to be executed if the condition in the if statement is met.

Your code is having incorrect if statements e.g if (firstnumber > secondnumber); if this if condition is true the there is no logic to execute.

You can do simply following to what you want to achieve. See complete working code here:

int largest = (firstnumber > secondnumber) ?
            (firstnumber > thirdnumber ? firstnumber : thirdnumber) :
            (secondnumber > thirdnumber ? secondnumber : thirdnumber);

UPDATED Following the simplify form of above logic using if-else:

int largest = thirdnumber;
if(firstnumber > secondnumber)
{
    if(firstnumber > thirdnumber)
        largest = firstnumber;
}
else if (secondnumber > thirdnumber)
    largest = secondnumber;

Following is the complete code. See it working here:

public static void main (String[] args)
{
    try( Scanner sc = new Scanner(System.in) )
    {
        System.out.print("Input any 3 numbers:");
        int firstnumber = sc.nextInt();
        int secondnumber = sc.nextInt();
        int thirdnumber = sc.nextInt();

        int largest = thirdnumber;
        if(firstnumber > secondnumber)
        {
            if(firstnumber > thirdnumber)
                largest = firstnumber;
        }
        else if (secondnumber > thirdnumber)
            largest = secondnumber;

        System.out.println("Biggest number is: " +largest);
    }
}

I would phrase your logic as:

int maxnumber;

if (firstnumber > secondnumber && firstnumber > thirdnumber) {
    // 1st larger than both 2nd and 3rd
    maxnumber = firstnumber;
}
else if (secondnumber > thirdnumber) {
    // else 1st is NOT largest -> either 2nd or 3rd is largest
    maxnumber = secondnumber;
}
else {
    // if 2nd not largest, then 3rd must be largest
    maxnumber = thirdnumber;
}

System.out.println("Biggest number is " + maxnumber);

Try to use && in your if and elsif statements. Also use this in the last condition :

else { System.out.println("Biggest number is " +thirdnumber); }

As other replies suggested the best course of action would be to rewrite the if clauses merging some conditions with AND (&& ).

In any case you code has two kind of errors.

  1. You use the semicolon after some if statements. Example:

if (firstnumber > secondnumber); The example above is syntactically correct BUT it does not actually change the flow of the program.

  1. You need to use more curly braces to group code together. A syntactically correct version could be the following:

     class CurrencyExchange_1 {    
     public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
         System.out.print("Input any 3 numbers");
         int firstnumber = sc.nextInt();
         int secondnumber = sc.nextInt();
         int thirdnumber = sc.nextInt();
    
         //Finding the bigger number
         if (firstnumber > secondnumber) {
             if (firstnumber > thirdnumber) {
                 System.out.println("Biggest number is " + firstnumber);
    
             }
         } else {
             if (secondnumber > firstnumber) {
                 if (secondnumber > thirdnumber) {
                     System.out.println("Biggest number is " + secondnumber);
    
                 } else {
                     if (thirdnumber > firstnumber) {
                         if (thirdnumber > secondnumber) {
                             System.out.println("Biggest number is " + thirdnumber);
                         }
                     }
                 }
             }
         }
     }
    

    }

The simplest way is to create a new variable, say max and process the numbers as shown below:

// Assign firstnumber to a new variable, max
int max = firstnumber;

// Assign secondnumber to max if secondnumber > max. Do the same thing for other
// numbers as well.

if (secondnumber > max)
    max = secondnumber;
if (thirdnumber > max)
    max = thirdnumber;

// Display the value of max
System.out.println("Biggest number is: " + max);
Related