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".
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.for(int a=1;a>l;a++)should befor(int a=0;a<l;a++)Plus, I've no idea how your code gets compiled. Your print statement uses the variabledwhich is out of the scope!else if. Java will interpret that as the body for theelse ifand will always execute your intended body:d=(char)(c+32);. Remove the semicolon from the end of yourelse ifline.