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

Thread: Char type Comparisson

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Char type Comparisson

    Hello everybody I知 a beginner in java, and I知 having problems trying to compare character in my program.
    I知 trying to have the user type a character and in the character it is not a correct one then tell the user to input the character again .

    thank in advance here is the code:

    import javax.swing.JOptionPane;
    public class test
    {
          public static void main(String[] args)
          {	
                String strnGrade = JOptionPane.showInputDialog("Enter the Grade for the course ");
                char grad = strnGrade.charAt(0);
                grad = Character.toUpperCase(grad);
                while  (grad != 'A' || grad != 'B' || grad != 'C' || grad != 'D' || grad != 'F')
                {
                       strnGrade = JOptionPane.showInputDialog
                       ("The Garde enter is invalid please enter the grade again");
                 }
           }
    }
    Last edited by solo_2101; April 25th, 2010 at 11:37 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Char type Comparisson

    Your logic is flawwed... You're asking grad to be 'A', 'B', 'C', 'D', and 'F' at the same time, which it can't.

    Switch the exclusive or logic to exclusive and (&&)

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    solo_2101 (April 25th, 2010)

  4. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Char type Comparisson

    thank you now its working fine...wonderful
    here the new code

    import javax.swing.JOptionPane;
    public class test
    {
      public static void main(String[] args)
      {	
        String strnGrade = JOptionPane.showInputDialog("Enter the Grade for the course ");
        char grad = strnGrade.charAt(0);
        grad = Character.toUpperCase(grad);
        while  (grad != 'A' && grad != 'B' && grad != 'C' && grad != 'D' && grad != 'F')
        {
          strnGrade = JOptionPane.showInputDialog
          ("The grades for the course are invalid please enter the grade againg");
          grad = strnGrade.charAt(0);
          grad = Character.toUpperCase(grad);
        }
        System.out.println(grad);
      }
    }
    Last edited by solo_2101; April 25th, 2010 at 12:21 PM.

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

    Default Re: Char type Comparisson

    I have marked this thread as solved

    You can do this yourself in the 'Thread Tools' menu at the top.
    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.

  6. #5
    Junior Member
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Char type Comparisson

    thank you... i didn't know how to do it but now i know ; and thanks again
    Last edited by solo_2101; April 26th, 2010 at 09:45 PM.

Similar Threads

  1. coverting to Char?
    By JavaNoob82 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 16th, 2010, 07:52 AM
  2. [SOLVED] Char cannot be dereferenced
    By zukipuu in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 17th, 2010, 11:13 PM
  3. Char cannot be dereferenced!! Please help
    By humdinger in forum Object Oriented Programming
    Replies: 5
    Last Post: February 14th, 2010, 03:18 PM
  4. Selection Sorting of Data type (Char)
    By chronoz13 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 08:28 PM
  5. char (toUppercase?)
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: December 12th, 2009, 10:01 AM