Lowercase to Uppercase?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jordan88
    New Member
    • Feb 2008
    • 4

    Lowercase to Uppercase?

    Hello, I am having a "loss of precision error" every time I try to convert a lower case to an upper case. We are not allowed to use the touppercase and tolowercase method in the program. Any advice on how to go about doing this without the error would be appreciated, thank you.

    if (c >= 'a' && c <= 'z' )
    {
    x = c - 32;
    }



    c is the number read in
    x was declared at the top of the program
  • jx2
    New Member
    • Feb 2007
    • 228

    #2
    we need to see more of your code a specialy how you declared your variables
    i am quite sure tou need to cast the values(tell the compiler that you know what you trying to do
    e.g. if you want to change from float to int
    [code=java]

    int i = 0;
    float f = 4.54;

    i = (int)(f);
    [/code]
    i hope that helps
    jx2

    Comment

    • jordan88
      New Member
      • Feb 2008
      • 4

      #3
      public class P4
      {
      public static void main(String []args)
      {
      char x = ' ';
      Scanner input = new Scanner(System. in);
      System.out.prin tln("Please enter a sequence of characters.");
      String a = input.nextLine( );
      char c = a.charAt(0);

      if(c >= 'a' && c <= 'z')
      {
      x = (char) c - 34;
      }

      I tried adding the (char) next to the variable c(input) and it did not work. I am not used to working with chars and when I did have to convert a lower case to an uppercase I would just use the method toupper, ect.

      Comment

      • jx2
        New Member
        • Feb 2007
        • 228

        #4
        [CODE=java]

        x = (char) c - 34;// remember about bracets what you trying to do in here
        //is trying to convert char c into char.
        // c is a char !!
        // and later you substracting 34
        // wich change it into number

        //try something like this:

        x = (char)(c-34);

        [/CODE]
        should help

        Comment

        • jordan88
          New Member
          • Feb 2008
          • 4

          #5
          Originally posted by jx2
          [CODE=java]

          x = (char) c - 34;// remember about bracets what you trying to do in here
          //is trying to convert char c into char.
          // c is a char !!
          // and later you substracting 34
          // wich change it into number

          //try something like this:

          x = (char)(c-34);

          [/CODE]
          should help
          worked like a charm, I wasnt aware that you could use it like that, appreciate your help.
          thank you :D

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            That 34 (or is it 32?) is mysterious. Why not write:
            [CODE=Java]x = (char) (c - 'a' + 'A');[/CODE]

            Comment

            • jordan88
              New Member
              • Feb 2008
              • 4

              #7
              Originally posted by BigDaddyLH
              That 34 (or is it 32?) is mysterious. Why not write:
              [CODE=Java]x = (char) (c - 'a' + 'A');[/CODE]
              It was actually 32, i changed it in my program to see if i made a mistake in other char tests. Thanks for the check though :) just started to program again so i forget all the checks sometimes.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by jordan88
                It was actually 32, i changed it in my program to see if i made a mistake in other char tests. Thanks for the check though :) just started to program again so i forget all the checks sometimes.
                It has nothing to do with 'checks'; everytime you see a 'magic' number in a piece
                of code other than -1, 0 or 1 (and I feel tempted to spit on -1) throw the code away
                or let any scabby cat pee on it and forget it afterwards.

                Nowadays programming languages allow you to define those magic monstrosities
                as either a (defined) constant or final value; there is no excuse anymore for silly
                literal values other than -1, 0 and 1 (and even -1 is close to the edge).

                kind regards,

                Jos

                Comment

                • BigDaddyLH
                  Recognized Expert Top Contributor
                  • Dec 2007
                  • 1216

                  #9
                  Originally posted by JosAH
                  Nowadays programming languages allow you to define those magic monstrosities
                  as either a (defined) constant or final value; there is no excuse anymore for silly
                  literal values other than -1, 0 and 1 (and even -1 is close to the edge).
                  Once when I was teaching, I told a student that he could improve his code if he replaced 10 (which was just an arbitrary array size) with a constant. When he handed in his code, I saw that he had...
                  [CODE=Java]
                  public static final int TEN = 10;
                  [/CODE]

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by BigDaddyLH
                    Once when I was teaching, I told a student that he could improve his code if he replaced 10 (which was just an arbitrary array size) with a constant. When he handed in his code, I saw that he had...
                    [CODE=Java]
                    public static final int TEN = 10;
                    [/CODE]
                    :-) I once saw this:

                    [code=java]
                    public interface Constants {
                    int contant00= 42; // I don't remember their values
                    ... // and the list continued up to this:
                    int constant71= 54; // but I do remember there were 72 of them
                    }
                    [/code]

                    Seventy two constants with meaningless names; some people deserve to have
                    their head nailed to the floor and covered in smelly parrot droppings.

                    kind regards,

                    Jos

                    Comment

                    Working...