Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8723327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:36:12+00:00 2026-06-13T07:36:12+00:00

I am learning Java. I am supposed to write a program that converts all

  • 0

I am learning Java.

I am supposed to write a program that converts all uppercase letters to lowercase and all lowercase to uppercase. It said in the book I just need to subtract 32 from uppercase and add 32 to lowercase.

Here is my code…

class Caseconv {
    public static void main(String args[])
        throws java.io.IOException {
            char ch;

            do {
            ch = (char) System.in.read();

            if (ch >= 97 & ch <= 122) ch = ch - 32;
            if (ch >= 65 & ch <= 90) ch = ch + 32;
            System.out.print(ch);
            } while (ch != '\n');
        }
}

But the compiler doesn’t want to do this, I get this error.

Caseconv.java:13: error: possible loss of precision
            if (ch >= 97 & ch <= 122) ch = ch - 32;
                                              ^
  required: char
  found:    int
Caseconv.java:14: error: possible loss of precision
            if (ch >= 65 & ch <= 90) ch = ch + 32;
                                             ^
  required: char
  found:    int
2 errors

What am I supposed to be doing to subtract from the char?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:36:14+00:00Added an answer on June 13, 2026 at 7:36 am

    You need to add a type cast to convert the result of the expression to char. For example.

    ch = (char)(ch + 32)
    

    Notes:

    1. The reason this is necessary is because 32 is an int literal, and the addition of a char and an int is performed using int arithmetic, and gives an int result.

    2. Assigning an int to a char potentially results in truncation. Adding the type cast effectively says to the compiler: “Yes. I know. It is OK. Just do it.”

    3. The parentheses around the + subexpression are necessary because type-cast has higher precedence than +. If you leave them out, the type-cast makes no difference because it “casts” a char to a char.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just learning java out of a what seems to be an excellent book,
I'm learning java and I have made simple program that simply reads value from
I just started learning java and I'm working on a program. I'm getting an
I am learning java, using the book Java how to program. I am solving
I'm learning Java at work, and the exercise we're supposed to do states the
We are just learning Java/Selenium as our QA is moving towards automation. I am
Learning java server technologies, trying to clarify some things. There are few technologies that
I just started learning Java and I encountered some problems with the tag in
I am learning Java using Java how to program (Deitel and Deitel). Right now
I have a Java book I'm learning from and in one of the examples,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.