1.

Solve : 'else' does not execute?

Answer»
I've written this little program that lets you input a number and the output declares whether it is positive or negative, but want it so that if you enter characters it displays an error. I'm trying to do that with the LAST 'else' statement, but it doesn't work. Can anyone point me in the right direction?

Code: [Select]IMPORT java.util.Scanner;
public class Positive {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
       
        int num;
       
        System.out.println("Enter number");
        num = input.nextInt();
       
        if (num < 0) {
            System.out.println("This is a negative number");
        }
           
        else if (num == 0) {
            System.out.println("Zero");
        }
           
        else if (num > 0) {
            System.out.println("This is a postive number");
        }
       
        else {
            System.out.println("Please enter a valid number");
Thanks in advanceYou seem to be missing a curly bracket.
Or else leave out the curly brackets completely, which only works if you've got a one-line statement. I'm assuming this works in Java - like you I've only started learning it myself. It does work in PHP though.

EDIT: after searching it appears you need to add a try/catch statement to catch the error.

catching the exception is one WAY:

Code: [Select]import java.util.InputMismatchException;
import java.util.Scanner;
public class positive {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
       
        int num;
       
        System.out.println("Enter number");
        try {
        num = input.nextInt();
       
        if (num < 0) {
            System.out.println("This is a negative number");
        }
           
        else if (num == 0) {
            System.out.println("Zero");
        }
           
        else if (num > 0) {
            System.out.println("This is a positive number");
        }
       
        }
        catch(InputMismatchException ime)
        {
        System.out.println("Error: Invalid number");
       
        }
       
    }
       
}

But it's not really the "best" way... best of course being subjective. Generally, if you can do something without raising and catching exceptions, you should. in this case, it's the hasNextInt() function.


Code: [Select]import java.util.InputMismatchException;
import java.util.Scanner;
public class positive {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
       
        int num;
       
        System.out.println("Enter number");
        if(input.hasNextInt())
       {
        num = input.nextInt();
       
        if (num < 0) {
            System.out.println("This is a negative number");
        }
           
        else if (num == 0) {
            System.out.println("Zero");
        }
           
        else if (num > 0) {
            System.out.println("This is a positive number");
        }
       
        }
        else{
        System.out.println("Invalid number");
        }
    }
       
}




and here's my pointlessly reduced version.

Code: [Select]import java.util.Scanner;
public class positive {
public static int Sign(int argument)
{

return argument<0?-1:argument==0?0:1;

}
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
       
        int num;
        String[] resultmessages = new String[]
{"This is a negative number", "Zero","This is a positive number"};
        System.out.println("Enter number");
        if(input.hasNextInt())
        {
        num = input.nextInt();
       
        System.out.println(resultmessages[Sign(num)+1]);
       
       
        }
        else
           System.out.println("Error: Invalid number");
       
       
    }
       
}



Quote from: Salmon Trout on October 02, 2011, 06:18:23 AM
You seem to be missing a curly bracket.

Yes, i didn't copy the end main and end class brackets

BC, i like your second option because it pretty close to what mine is, except it does work.
Quote from: reddevilggg on October 02, 2011, 02:18:07 PM
BC, i like your second option because it pretty close to what mine is, except it does work.

eejit i am, it supposed to read, does NOT work. The second option does NOT work. It works fine for me. If you are copy-pasting into yours to try it, rename the file to positive.java or change the name of the class from positive to Positive. (the latter is a lot easier on windows).
Quote from: BC_Programmer on October 03, 2011, 10:16:59 AM
change the name of the class from positive to Positive.


Of course, i missed that. Works perfect, thanks again.


Discussion

No Comment Found