0

I am new to Java, and trying out simple examples to get familiar with the basics. This is a program to see if three integers specified by the user are all equal.

import java.lang.*;
import java.util.*;
public class CheckEqual{

    public static void main(String [] args){
        if (args.length != 5){
            System.out.println("Please check the number of your integers!");
            return;
        }
        try{
            int firstInteger = Integer.parseInt(args[2]);
            int secondInteger = Integer.parseInt(args[3]);
            int thirdInteger = Integer.parseInt(args[4]);
        }
        catch(NumberFormatException e){
            System.out.println("Make sure that all inputs are integers!");
            return;
        }
        if (firstInteger == secondInteger && secondInteger == thirdInteger){
            System.out.println("True");
            return;
        }
        else{
            System.out.println("False");
            return;
        }
    }

 }

During compilation, it gave me the error "cannot find symbols" on firstInteger,secondInteger, and thirdInteger. Why is this, and how can I solve it?

antande
  • 169
  • 1
  • 13

4 Answers4

2

Declare those integers globally or outside try block because it is only restricted to try block and cannot be used elsewhere change it to:

    import java.lang.*;
    import java.util.*;
    public class CheckEqual{

    public static void main(String [] args){
    int firstInteger,secondInteger,thirdInteger;
    if (args.length != 5){
        System.out.println("Please check the number of your integers!");
        return;
    }
    try{
        firstInteger = Integer.parseInt(args[2]);
        secondInteger = Integer.parseInt(args[3]);
        thirdInteger = Integer.parseInt(args[4]);
    }
    catch(NumberFormatException e){
        System.out.println("Make sure that all inputs are integers!");
        return;
    }
    if (firstInteger == secondInteger && secondInteger == thirdInteger){
        System.out.println("True");
        return;
    }
    else{
        System.out.println("False");
        return;
    }
  }

 }
Rahul Sonwanshi
  • 105
  • 1
  • 12
2

Java uses a scope on every variable. Basically, if you create a variable in a if block, like the following :

if(statement){
    int x = 1;
}

x++;

The x variable only exists in the scope of the if block. It doesn't exist afterwards. Meaning that x++ will create an error because x doesn't exist in this scope. The same applies in the try block in your code. firstInteger, secondInteger and thirdInteger don't exist outside of the try block. You should change your code for the following:

import java.lang.*;
import java.util.*;
public class CheckEqual{

    public static void main(String [] args){
        if (args.length != 5){
            System.out.println("Please check the number of your integers!");
            return;
        }
        try{
            int firstInteger = Integer.parseInt(args[2]);
            int secondInteger = Integer.parseInt(args[3]);
            int thirdInteger = Integer.parseInt(args[4]);

            if (firstInteger == secondInteger && secondInteger == thirdInteger){
                System.out.println("True");
                return;
            }
            else{
                System.out.println("False");
                return;
            }
        }
        catch(NumberFormatException e){
            System.out.println("Make sure that all inputs are integers!");
            return;
        }
    }
}
Shak
  • 166
  • 7
0

Variables declared within the try/catch block are not in scope in the containing block, for the same reason that all other variable declarations are local to the scope in which they occur.That's how the documentation specifies. Due to this reason you should have declared those variables outside the try/catch block for usability of them in main() method.

anurag0510
  • 763
  • 1
  • 8
  • 17
0

As correctly pointed by others that your int variables are out of scope I will make it simple to understand.

Option 1 : Use if...else within try...catch block

    try{
        int firstInteger = Integer.parseInt(args[2]);
        // 
        if (firstInteger == secondInteger && secondInteger == thirdInteger){
            ///
        } else {
           //
        }
    }
    catch(NumberFormatException e){
        System.out.println("Make sure that all inputs are integers!");
        //return; //not needed, you may skip this
    }

Option 2 : declare int variables outside of or before try...catch block

    int firstInteger, ... ;
    try{
        firstInteger = Integer.parseInt(args[2]); //no `int` keyword used !
        //
    }
    catch(NumberFormatException e){
        System.out.println("Make sure that all inputs are integers!");
        return;
    }