ChangeCase.java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RobinsionOrange
    New Member
    • Apr 2007
    • 2

    #1

    ChangeCase.java

    Im trying to create a script that reverses the case of all characters of an argument from the command line:

    This is what i got:
    Code:
    public class ChangeCase {
    
      public static void main (String args[])  {
    
            String tempString;
            char tempCharacter;
            char tempCharUp;
            char tempCharDown;
    
            for (int i=0 ; i < args.length ; i++){
            tempString = args[i];
            tempCharacter = tempString.charAt(i);
            tempCharUp = tempCharacter.toUpperCase();
            tempCharDown = tempCharacter.toLowerCase();
                    if (tempCharacter.equals(tempCharUp)){
                            tempCharacter = tempCharDown;
                            System.out.println (tempCharacter);
    }
                    else {
                            tempCharacter = tempCharUp;
                            System.out.println (tempCharacter);
                         }
                    }
             }
    }
    But im getting this:
    ChangeCase.java :15: char cannot be dereferenced
    tempCharUp = tempCharacter.t oUpperCase();
    ^
    ChangeCase.java :16: char cannot be dereferenced
    tempCharDown = tempCharacter.t oLowerCase();
    ^
    ChangeCase.java :17: char cannot be dereferenced
    if (tempCharacter. equals(tempChar Up)){
    ^
    3 errors

    Any sugestions please, im quite new to java.

    Regards in advance.
  • sandyw
    New Member
    • Mar 2007
    • 122

    #2
    Question what does char mean to you.

    sandyw

    Comment

    • RobinsionOrange
      New Member
      • Apr 2007
      • 2

      #3
      Character?

      Comment

      • sandyw
        New Member
        • Mar 2007
        • 122

        #4
        You might be using char for the wrong reason.
        Review what char mean...

        or I might be wrong...
        sandyw

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          A char is a primitive and it hasn't any methods; that's what your compiler is
          whining about. Have a look at the Character class and its many utility
          methods. Hint: pay special attention to the methods isLowerCase, isUpperCase,
          toLowerCase and toUppercase.

          kind regards,

          Jos

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by RobinsionOrange
            Im trying to create a script that reverses the case of all characters of an argument from the command line:

            This is what i got:
            Code:
            public class ChangeCase {
             
            public static void main (String args[]) {
             
            String tempString;
            char tempCharacter;
            char tempCharUp;
            char tempCharDown;
             
            for (int i=0 ; i < args.length ; i++){
            tempString = args[i];
            tempCharacter = tempString.charAt(i);
            tempCharUp = tempCharacter.toUpperCase();
            tempCharDown = tempCharacter.toLowerCase();
            if (tempCharacter.equals(tempCharUp)){
            tempCharacter = tempCharDown;
            System.out.println (tempCharacter);
            }
            else {
            tempCharacter = tempCharUp;
            System.out.println (tempCharacter);
            }
            }
            }
            }
            But im getting this:
            ChangeCase.java :15: char cannot be dereferenced
            tempCharUp = tempCharacter.t oUpperCase();
            ^
            ChangeCase.java :16: char cannot be dereferenced
            tempCharDown = tempCharacter.t oLowerCase();
            ^
            ChangeCase.java :17: char cannot be dereferenced
            if (tempCharacter. equals(tempChar Up)){
            ^
            3 errors

            Any sugestions please, im quite new to java.

            Regards in advance.
            tempCharacter.t oUpperCase(); is not allowed. Look at API specs for the Character class to see how to convert characters.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by JosAH
              A char is a primitive and it hasn't any methods; that's what your compiler is
              whining about. Have a look at the Character class and its many utility
              methods. Hint: pay special attention to the methods isLowerCase, isUpperCase,
              toLowerCase and toUppercase.

              kind regards,

              Jos
              Hey I thought my connection was faster than Jos' connection ...

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by r035198x
                Hey I thought my connection was faster than Jos' connection ...
                I already had my espresso so I'm amazingly fast this morning ;-)

                kind regards,

                Jos

                Comment

                Working...