Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: 'else' does not execute  (Read 6040 times)

0 Members and 1 Guest are viewing this topic.

reddevilggg

    Topic Starter


    Expert

    Thanked: 69
  • Experience: Beginner
  • OS: Windows 7
'else' does not execute
« on: October 02, 2011, 06:11:21 AM »

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 advance
11 cheers for binary !

Salmon Trout

  • Guest
Re: 'else' does not execute
« Reply #1 on: October 02, 2011, 06:18:23 AM »
You seem to be missing a curly bracket.

kpac

  • Web moderator


  • Hacker

  • kpac®
  • Thanked: 184
    • Yes
    • Yes
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 7
Re: 'else' does not execute
« Reply #2 on: October 02, 2011, 08:02:39 AM »
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.
« Last Edit: October 02, 2011, 08:15:10 AM by kpac »

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: 'else' does not execute
« Reply #3 on: October 02, 2011, 11:50:34 AM »


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");
       
       
    }
       
}


I was trying to dereference Null Pointers before it was cool.

reddevilggg

    Topic Starter


    Expert

    Thanked: 69
  • Experience: Beginner
  • OS: Windows 7
Re: 'else' does not execute
« Reply #4 on: October 02, 2011, 02:18:07 PM »

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.
11 cheers for binary !

reddevilggg

    Topic Starter


    Expert

    Thanked: 69
  • Experience: Beginner
  • OS: Windows 7
Re: 'else' does not execute
« Reply #5 on: October 03, 2011, 09:19:38 AM »

@ 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.
11 cheers for binary !

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: 'else' does not execute
« Reply #6 on: October 03, 2011, 10:16:59 AM »
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).
I was trying to dereference Null Pointers before it was cool.

reddevilggg

    Topic Starter


    Expert

    Thanked: 69
  • Experience: Beginner
  • OS: Windows 7
Re: 'else' does not execute
« Reply #7 on: October 04, 2011, 05:58:37 AM »

change the name of the class from positive to Positive.


Of course, i missed that. Works perfect, thanks again.
11 cheers for binary !