0

I'm trying to do a String Program using BufferedReader where you take a String from the user and change the case of the letters. this is what I've got so far:

import java.io.*;
public class StringProg {
    public void ff()throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter a sentence");
        String str=br.readLine();
        String s="";
        int l=str.length();
        char c;
        for(int a = 1; a < l; a++) {
            c = str.charAt(a);
            char d = 0;
            if(c >= 97 && c <= 122) {
                d = c - 32;
            } else if(c >= 65 && c <= 90) {
                d = c + 32;
            }
         System.out.print(d);
        }
    }
}

when I run it, it says "possible loss of precision; required char; found int" could someone help me rectify this please?

Update: this is the new code after correction:

import java.io.*;
 public class StringProg
  {
    public void ff()throws IOException
   {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter a sentence");
    String str=br.readLine();
    String s="";
    int l=str.length();
    char c;
    char d;
    for(int a=1;a<l;a++)
    {
        c=str.charAt(a);
        if(c>=97 && c<=122)
        {
             d= (char)(c-32);
        }
        else if(c>=65 &&c<=90);
        {
            d=(char)(c+32);
        }
     System.out.print(d);
    }
    }
}

but the output isn't working.Could someone point out my mistake please? when I enter "a" or "b", there is no output but when I enter "E" it changes to "e", but when I enter "HidE" it changes to 2 squares(I don't know how to print it on the keyboard ) and "e".

4
  • 2
    Use Character.toUpperCase(char) API -- docs.oracle.com/javase/7/docs/api/java/lang/… Keep in mind that your code will not be completely Unicode safe until you address multi-char codepoints. Commented Jun 24, 2013 at 14:51
  • Among other things: for(int a=1;a>l;a++) should be for(int a=0;a<l;a++) Plus, I've no idea how your code gets compiled. Your print statement uses the variable d which is out of the scope! Commented Jun 24, 2013 at 14:53
  • @aqua your updated code (probably) works, but imagine I come upon your code and happen not to know the unicode value for letters. It looks like madness. Using Character.toUpperCase(char) and Character.isUpperCase(char) and such really improves the readability of this sort of thing Commented Jun 24, 2013 at 15:12
  • There is a semicolon after your else if. Java will interpret that as the body for the else if and will always execute your intended body: d=(char)(c+32);. Remove the semicolon from the end of your else if line. Commented Jun 24, 2013 at 16:41

4 Answers 4

3
char d= c-32;

c-32 expression results in int (because 32 will be treated as int) and you are trying to assign int value to type char, which is why you are getting "possible loss of precision error". You need to explicitly cast 32 to char (or) entire expression on right side to char.

Same rule applies for char d=c+32; statement also and other places you have used int value with char type.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 True, but the use of numeric representation of a char is always a hack. Better to use Character.toUpperCase(char) and similar
2
char d = (char) (c-32); // note the parentheses
char d = (char) (c+32); // around (char + int); see below
  • 32 is an int literal. The expression resulting from (char + int) arithmetic is of type int as well. Since, the range of a char is a subset of int, compiler complains about the potential loss of precision. By explicitly casting to (char), you're effectively telling the compiler that you know what you're doing.

  • Note, the parentheses around the arithmetic expression (c+32). Without it the compiler evaluates this as (char)(c) + 32 which again results in an int due to the higher precedence of casting.

Comments

0

Simply cast back to a char once you do your calculations. For instance, char d = (char)(c - 32). It basically just wants you to clarify and let the program know that you want it to convert back and forth.

Comments

0

This occurs because when you add an int to a character it becomes an int, then you try to (implicitly) cast that int back to a char and the int may not "fit into" the char so you get an error

Lets say that c='A', so 65

char d= c+32
char d= 65+32
char d= 97;

Now 97 might correspond to a valid character, but we can't know that at compile time (as in principle it could be any integer), if the int is too big or small to fit in the char then parts of it are thrown away; hence the warning. You can cast it to (char) to say you accept that, but the whole process isn't great.

char d= (char)(c+32);

All this looks like its checking for capitalisation and setting to lower case (and vice versa). If so; don't do that, its really really unclear, use:

c=str.charAt(a);
if(Character.isUpperCase(c)){
     char d=Character.toLowerCase(c);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.