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 2 of 2

Thread: Switch Statement with multpile char variables not working properly

  1. #1
    Junior Member
    Join Date
    Oct 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Switch Statement with multpile char variables not working properly

    Here are the directions: Write a switch statement that checks origLetter. If '’a'’ or '’A'’, print "Alpha". If '’b'’ or '’B'’, print "Beta". For any other character, print "Unknown". Use fall-through as appropriate. End with newline. Use JOption pane to get the origLetter from user.

    My problem is that "Alpha", "Beta", and "Unknown" are all printing no matter the input.

     import javax.swing.JOptionPane;
    public class week7assignmentq1 {
        public static void main(String [] args){
            char alpha = 'a' & 'A';
            char beta = 'b' & 'B';
            String origLetterString;
           origLetterString=JOptionPane.showInputDialog("Enter a letter:");
     
           switch (alpha){
               case 'a':
               case 'A':
                   System.out.println("Alpha");
                   break;
           }
           switch (beta){
     
               case 'b':
               case 'B':
                   System.out.println("Beta");
               default:
                   System.out.println("unknown");
               break;
           }
    }
     }
    Last edited by ravinniaofcreed; October 22nd, 2017 at 02:47 PM. Reason: I forgot some things

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Switch Statement with multpile char variables not working properly

    My problem is that "Alpha", "Beta", and "Unknown" are all printing no matter the input.
    You should check origLetter instead of "Alpha" and "Beta"
    public class week7assignmentq1 {
        public static void main(String[] args) {
            String origLetterString;
            origLetterString = JOptionPane.showInputDialog("Enter a letter:");
     
            switch (origLetterString) {
                case "a":
                case "A":
                    System.out.println("Alpha");
                    break;
                case "b":
                case "B":
                    System.out.println("Beta");
                default:
                    System.out.println("unknown");
                    break;
            }
        }
    }
    Last edited by John Joe; October 22nd, 2017 at 10:12 PM.
    Whatever you are, be a good one

Similar Threads

  1. Using variables initialized in a switch statement.
    By kunimaro15689 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 21st, 2014, 05:13 PM
  2. Using variables in switch statement case clauses
    By kunimaro15689 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 19th, 2014, 06:21 PM
  3. java awt...if statement not working properly
    By edward005 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 28th, 2013, 02:57 AM
  4. IF statement accumulataion not working properly - random numbers
    By StrugglerWithJava in forum Loops & Control Statements
    Replies: 1
    Last Post: April 4th, 2013, 07:48 AM
  5. if else statement not working properly
    By tina G in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 29th, 2010, 08:04 AM