Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: not sure why the 'else is not working

  1. #1
    Junior Member reddevilggg's Avatar
    Join Date
    Jan 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default not sure why the 'else is not working

    When i run this code the 'else' output always displays. Why is that ??

    import java.util.Scanner;
    public class Post {
        public static void main(String[] args) {
            Scanner myKeyboard = new Scanner (System.in);
     
        String post1 = "bill",post2 = "circular", post3 = "postcard", post4 = "letter";
     
            System.out.println("Enter letter type");
            String post = myKeyboard.nextLine();
     
            if (post1.equalsIgnoreCase(post)) {
                System.out.println("bills must be paid");
            }
     
            if (post2.equalsIgnoreCase(post)) {
                System.out.println("circulars are thrown away");
            } 
     
            if (post3.equalsIgnoreCase(post)) {
                System.out.println("postcards are put on the wall");
            }
     
            else if (post4.equalsIgnoreCase(post)) {
                System.out.println("personal letters are read and have replies written for them");
            }
     
            else {
                System.out.println("Letter type not recognised");
            }
            System.exit(0);


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: not sure why the 'else is not working

    While I'm not sure what behavior you seek with the program, do you want the 'if' when comparing post2 and post3 to be 'else if'?

    P.S. No need to call System.exit(0)...the main method exits by itself.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: not sure why the 'else is not working

    Please try and post in the correct forum. I have moved your thread to - Loops and Control Statements

    I suggest you have another quick read of - The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Under the first 'if' change the other two to 'else if'
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  4. #4
    Junior Member reddevilggg's Avatar
    Join Date
    Jan 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: not sure why the 'else is not working

    Sorry for posting in the wrong section, i knew the code was not a loop, but i did not know it was a control statement. I'm not sure how you supposed to know these things when you're a total noob, tbh, but hey ho.

    Thank you for your replies, it worked a treat, but how come this doesn't work the same way. If you enter a number you get a specific answer, but i want it so if you enter anything else, like a character, you get the last 'else' output.

    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.

Similar Threads

  1. [SOLVED] Working on Win 7 and not on XP
    By shaumux in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 4th, 2011, 05:36 PM
  2. Why isn't this working?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 14
    Last Post: January 21st, 2011, 03:08 PM
  3. [SOLVED] CoreJavaExample Not Working
    By prasana in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 12th, 2010, 11:23 AM
  4. Cannot seem to get this working
    By OttawaGuy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 28th, 2010, 03:41 PM
  5. Replies: 4
    Last Post: January 26th, 2009, 11:03 PM