-4

am trying to write A program in java that receives a string from the user and print it with converting the lowercase letters to uppercase and the uppercase letters to lowercase letters

System.out.print("Enter A String ~>");
            str = scan1.nextLine();
            ChangeCh(str);

private static void ChangeCh(String str) {
    // TODO Auto-generated method stub
    str.replaceAll("[a-z]", "A-Z");
    System.err.println(str);
}
shayne
  • 87
  • 6
  • 1
    Welcome to Stack Overflow! It looks like you may be asking for homework help. While we have no issues with that per se, please observe these [dos and don'ts](http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions/338845#338845), and edit your question accordingly. (Even if this isn't homework, please consider the advice anyway.) – Joe C Mar 26 '17 at 16:10
  • 1
    So what is your question? Can you show us your code so far? Just for your Information: SO is a question and answer site and not a coding service. Please specify your problem – Japu_D_Cret Mar 26 '17 at 16:11
  • 1
    Sorry, but this question looks like a homework dump, and no one wants to do your homework for you. Please improve it or delete it. – DontKnowMuchBut Getting Better Mar 26 '17 at 16:12
  • You didn't ask any question. This should be a tweet, not a Stackoverflow question. – JB Nizet Mar 26 '17 at 16:12
  • 2
    Possible duplicate of [how to convert Lower case letters to upper case letters & and upper case letters to lower case letters](http://stackoverflow.com/questions/14972032/how-to-convert-lower-case-letters-to-upper-case-letters-and-upper-case-letters) – bit-shashank Mar 26 '17 at 16:12
  • Hint : `.toUpperCase()` and `.toLowerCase()`. – John Joe Mar 26 '17 at 16:13
  • my flag is my answer – bit-shashank Mar 26 '17 at 16:13
  • Be careful what you learn from exercises such as this one. The problem description is incomplete, [To perform case conversion, you need a locale.](http://stackoverflow.com/questions/26515060/why-java-character-touppercase-tolowercase-has-no-locale-parameter-like-string-t) A locale indicates a language dialect and writing system. For example, en_US indicates American English with the Latin writing system. In that locale, I is lowercased to i and i is uppercased to I. That's not true in all locales. – Tom Blodget Mar 26 '17 at 18:42

2 Answers2

1
import java.util.*;
public class Help
{
    public static void main(String[] args)
    {
        Scanner scan1 = new Scanner(System.in);
      String str = "";

      System.out.println("Input an abbreviation: ");
     str = scan1.nextLine();
      ChangeCh(str);



    }
    private static void ChangeCh(String str) 
    {
        StringBuilder sb = new StringBuilder("");
     for(int x=0;x<str.length();++x)
     {
         if(Character.isLowerCase(str.charAt(x)))
             sb.append(Character.toUpperCase(str.charAt(x)));
         else if(Character.isUpperCase(str.charAt(x)))
               sb.append(Character.toLowerCase(str.charAt(x)));

     }
     System.out.println(sb.toString());
    }

}
0xDEADBEEF
  • 590
  • 1
  • 5
  • 16
0

Convert to Upper Case

System.out.print("Enter A String ~>");
str = scan1.nextLine();  // assume enter lower case
ChangeCh(str);

private static void ChangeCh(String str) {
    // TODO Auto-generated method stub
    System.println(str.toUpperCase());
}

For converting to lower case, its quite similar with the above answer, you just need to modify a bit. I think you can solve it yourself.

Source: Java - String toLowerCase() Method.


To convert Upper Case to Lower Case and Lower Case to Upper Case between a String, use the code below

    public static void main(String args [])throws IOException
   {
    System.out.println("Enter A String ~>");
    Scanner scan1 = new Scanner(System.in);
    String   str = scan1.nextLine();
    String sentence = "";
    for(int i=0;i<str.length();i++)
    {
        if(Character.isUpperCase(str.charAt(i))==true)
        {
            char ch2= (char)(str.charAt(i)+32);
            sentence = sentence + ch2;
        }
        else if(Character.isLowerCase(str.charAt(i))==true)
        {
            char ch2= (char)(str.charAt(i)-32);
            sentence = sentence + ch2;
        }
        else
        sentence= sentence + str.charAt(i);
    }
    System.out.println(sentence);
  }

Note : A (upper case) and a (lower case) have a difference of 32 in ASCII code.

John Joe
  • 12,412
  • 16
  • 70
  • 135
  • A difference of 32 is true in ASCII but Java doesn't use ASCII; It uses Unicode (specifically UTF-16 for char, Character, and String). There are tens of thousands of letters. The library functions account for that. (You might ask yourself if you ever use ASCII and if so exactly where.) – Tom Blodget Mar 26 '17 at 18:28