Results 1 to 2 of 2

Thread: [RESOLVED] simple question of char

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Resolved [RESOLVED] simple question of char

    Code:
    char C='e';
    
    C=JOptionPane.showInputDialog("A/B?").charAt(0);
    switch(C)
    {
    	case'A':case'a':
    	System.out.print("A");
    	break;
    	case'B':case'b':
    	System.out.print("B");
    	break;
    	default:
                 JOptionPane.showMessageDialog(null, "Please answerA/B.");
    	break;
    }
    But people enter AAA,BBB instead of A,B,and that is ok...But i dun want it
    being happen...how to de this bug?
    Last edited by newpat; Apr 18th, 2007 at 07:12 AM.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: [RESOLVED] simple question of char

    Fetch the entire return value as a string and use a if...else if chain with equals() instead of a switch on a single character. Note that you can trigger an exception in your code simply by entering nothing at all or by pressing escape.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width