-5
public class LowerAndUpper {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("plz enter a string");
        String str=input.nextLine();
        for (int i=0; i<str.length();i++){
            char c=str.charAt(i);
            if (Character.isLowerCase(c)){
                str.toUpperCase();
            }
            else {
                str.toLowerCase();
            }
        }
        System.out.print(str);

    }
}

        

output:

plz enter a string
AbcD
AbcD
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 5
    Strings are immutable. Each operation that modify a String returns a new one. So it should be `str= str.toUpperCase();` (however i'm not sure this is what you want) – Alexis C. Mar 28 '14 at 16:10
  • (So calling `str.toUpperCase` and ignoring the result is not helpful...) – Jon Skeet Mar 28 '14 at 16:10
  • 2
    Do you want the result of AbcD to be aBCd? – blueygh2 Mar 28 '14 at 16:11
  • yes, `String` is immutable. You will need to create a new `String` to change it's case, like: `String newString = str.toUpperCase(); System.out.println(newString);` – SnakeDoc Mar 28 '14 at 16:11
  • What do the functions toUpperCase() and toLoverCase() do? – crychair Mar 28 '14 at 16:16
  • @crychair Check the Javadocs of the `String` class. They are built in methods. – Duncan Jones Mar 28 '14 at 16:16
  • You are checking on a single character and then trying to convert the entire string tomupper or lower. You should do the converting one character at a time. – NomadMaker Aug 08 '20 at 19:56

3 Answers3

3
        if (Character.isLowerCase(c)){
            str.toUpperCase();
        }
        else {
            str.toLowerCase();
        }

should be

        if (Character.isLowerCase(c)){
            str = str.toUpperCase();
        }
        else {
            str = str.toLowerCase();
        }

Strings are immutable objects.

See duncan's answer for the flipping of the chars. Alternatively you can also have some fun and flip the casing using addition and subtraction. This only works if you are dealing with alphabet characters, though.

public static void main(String[] args) throws Exception {

  String str = "AbcD";

  char[] chars = str.toCharArray();

  for (int i = 0; i < chars.length; i++) {
    char c = chars[i];
    if (Character.isLowerCase(c)) {
      chars[i] -= 32;
    } else {
      chars[i] += 32;
    }
  }
  System.out.print(new String(chars));

}

The reason this works is because the char values for upper case and lower case ascii characters are 32 away from each other.

TTT
  • 1,952
  • 18
  • 33
  • I think the OP is intending to flip the case of each character. So the problem is not only the immutability of strings, but the entire approach taken (e.g. use of `str.ToUpperCase()`). – Duncan Jones Mar 28 '14 at 16:15
2

To flip character case in a String, convert to a character array and work your way through that. Since Java strings are immutable, your current code is essentially performing work and throwing away the answer. Plus you are also calling str.ToUpperCase() which would change the entire string to uppercase.

Below is an example:

public static void main(String[] args) throws Exception {

  String str = "AbcD";

  char[] chars = str.toCharArray();

  for (int i = 0; i < chars.length; i++) {
    char c = chars[i];
    if (Character.isLowerCase(c)) {
      chars[i] = Character.toUpperCase(c);
    } else {
      chars[i] = Character.toLowerCase(c);
    }
  }
  System.out.print(new String(chars));

}

Output:

aBCd
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • WOW 10x for the quick answer but if i dont want to use array i just need to put the new char after low\up at new string? –  Mar 28 '14 at 16:23
  • @user3473379 I don't think that's possible. Anyway... why don't you want to use an array? – Duncan Jones Mar 28 '14 at 16:36
0

Try This

  import java.io.Serializable;
  import java.util.ArrayList;
  import java.util.List;

  import java.util.Scanner;
  public class main {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);
    System.out.println("plz enter a string");
    String str=input.nextLine();
    char[] chars = str.toCharArray();
    for (int i=0; i<str.length();i++){
        char c=chars[i];
        if (Character.isLowerCase(c)){

            chars[i] = Character.toUpperCase(c);
        }
        else {
            chars[i] = Character.toLowerCase(c);
        }
    }
    System.out.print(new String(chars));
}
}
prem30488
  • 2,828
  • 2
  • 25
  • 57
  • i see there is now way to solve this without a array . thanks a lot –  Mar 28 '14 at 16:26