Why doesn't character datatype take up more than one character ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Amiya Maitreya
    New Member
    • Sep 2010
    • 15

    Why doesn't character datatype take up more than one character ?

    Hello.
    I use BlueJ 3.0.1
    I am writing a program to dictionarize words(arrange words in dictionary order).

    But the 'char' type isn't working properly.

    Code:
    char ch1 = 'qwerty' ;//shows error
    char ch2 = 'qwedrt';
    if(ch1>ch2)
    {
    System.out.println(ch2);
    System.out.println(ch1);
    }
    else
    {
    System.out.println(ch1);
    System.out.println(ch2);
    Please help with it and program.
    * NOTE for Bytes.com : THIS IS NOT A HOMEWORK QUESTION.
  • Amiya Maitreya
    New Member
    • Sep 2010
    • 15

    #2
    given code is a snippet

    Comment

    • Dheeraj Joshi
      Recognized Expert Top Contributor
      • Jul 2009
      • 1129

      #3
      Code:
      'qwerty'
      This is certainly not a character. You can have a single character within single quote for char data type.

      Code:
      char ch2 = 'p';
      Regards
      Dheeraj Joshi

      Comment

      • Amiya Maitreya
        New Member
        • Sep 2010
        • 15

        #4
        bt then hw will i dictionarize ?

        Comment

        • Dheeraj Joshi
          Recognized Expert Top Contributor
          • Jul 2009
          • 1129

          #5
          What do you mean by dictionarize? I am unable to understand your question.

          Regards
          Dheeraj Joshi

          Comment

          • Amiya Maitreya
            New Member
            • Sep 2010
            • 15

            #6
            Aim of my program is to arrange entered words in dictionary order....hw would i do that !!

            Comment

            • Dheeraj Joshi
              Recognized Expert Top Contributor
              • Jul 2009
              • 1129

              #7
              Take String datatype to handle this.

              Regards
              Dheeraj Joshi

              Comment

              • Amiya Maitreya
                New Member
                • Sep 2010
                • 15

                #8
                But > , < , = operators don't work with string !

                Comment

                • Dheeraj Joshi
                  Recognized Expert Top Contributor
                  • Jul 2009
                  • 1129

                  #9
                  Use
                  Code:
                  java.util.Arrays.sort(MyStringArray);
                  Otherwise put the strings in sortedset. And retrieve them

                  Regards
                  Dheeraj Joshi

                  Comment

                  • Amiya Maitreya
                    New Member
                    • Sep 2010
                    • 15

                    #10
                    kk. you knw by any method i can convert string to char ???
                    like i entered + as string datatype but want to convert it to + of char datatype

                    Comment

                    • Dheeraj Joshi
                      Recognized Expert Top Contributor
                      • Jul 2009
                      • 1129

                      #11
                      You can convert it into character array.

                      Code:
                      char[] characters = str.toCharArray();
                      Take the char at position 0

                      or

                      Code:
                      char myChar = str.charAt(0);
                      Regards
                      Dheeraj Joshi

                      Comment

                      • Amiya Maitreya
                        New Member
                        • Sep 2010
                        • 15

                        #12
                        without arrays?

                        Comment

                        • Dheeraj Joshi
                          Recognized Expert Top Contributor
                          • Jul 2009
                          • 1129

                          #13
                          See second option in my last post(11).

                          Regards
                          Dheeraj Joshi

                          Comment

                          • Amiya Maitreya
                            New Member
                            • Sep 2010
                            • 15

                            #14
                            it workd ...thnx

                            Comment

                            • Dheeraj Joshi
                              Recognized Expert Top Contributor
                              • Jul 2009
                              • 1129

                              #15
                              Good to know that.

                              Comment

                              Working...