2

Im very new to coding and cant seem to be able to return anything. I need to convert upper case characters to lower case and vice versa. Here's my code:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String str = sc.nextLine();
    invString(str);
    sc.close();

}

private static String invString(String str) {
    for (int i = 0; i < str.length(); i++) {
        char ch = str.charAt(i);
        if (ch > 97) {
            ch = Character.toUpperCase(ch);
            return str;
        } else {
            ch = Character.toLowerCase(ch);
            return str;
        }
    }
    return null;
}

What am i doing wrong? ( in terms of returning, the code isnt complete yet)

EDIT****************

thanks for the helpful remarks, as i understood i do not have a place where my modifications are stored, so i added String res = ""; and kept adding the values into String res but in the end, when returning res, i still dont get any output...Here is the whole thing:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String str = sc.nextLine();
    String st = invString(str);
    sc.close();

}

private static String invString(String str) {
    String res = "";
    for (int i = 0; i < str.length(); i++) {
        char ch = str.charAt(i);

        if (65 <= ch && ch <= 90) {
            ch += 32;
            res += ch;

        } else if (97 <= ch && ch <= 122) {
            ch -= 32;
            res += ch;
        }


    }

    return res;
}

ps. Im not using the ready methods because my task asks for it.

progyammer
  • 1,498
  • 3
  • 17
  • 29
cosmo
  • 83
  • 1
  • 1
  • 7
  • 1
    What is the problem? There is a logical error, since either the `if`- or the `else`-block is entered, therefore the loop is executed exactly once iff. `str.length() > 0`. You are using the `return` Statement (syntactically) correct. – Turing85 Oct 29 '16 at 09:46
  • 1
    "Can't seem to"? Be *specific*. Most likely, you're making multiple erroneous assumptions about how variables and strings work in Java--strings are immutable, and assigning values to variables doesn't change other objects that have their own variables. – chrylis -cautiouslyoptimistic- Oct 29 '16 at 09:46
  • See http://stackoverflow.com/a/1729815/3885376 – ROMANIA_engineer Oct 29 '16 at 09:49
  • 1
    There is no reason you downvote everyones answer , – Pranoy Sarkar Oct 29 '16 at 10:12
  • @PranoySarkar Because 2/5 is "everyone" (yes, I did downvote and I retracted my downvotes after the posters responded to my complains). – Turing85 Oct 29 '16 at 10:18
  • You return immediately after the first character. Also you return the original String without doing anything. – QBrute Oct 29 '16 at 10:35

3 Answers3

2

There are a number of flaws in your code. Firstly, you are attempting to return the original string str in every if statement. So what happens is the method invString( String ) simply returns the original string that is passed as argument. Instead, you should keep adding the characters to a new String variable (as @Massimo said). Also, you are returning null in the end. Why even do that? You would want to return the new String variable instead.

private static String invString(String str) {

    String s=""; //new String variable
    for (int i = 0; i < str.length(); i++) {
        char ch = str.charAt(i);
        if (ch > 97) {
            ch = Character.toUpperCase(ch);
            s+=ch; //do this instead of returning str
        } else {
            ch = Character.toLowerCase(ch);
            s+=ch; //same here
        }
    }
    return s; //return the new String
}

Secondly, in your main method, simply calling the method is wrong as it returns a value. You should assign the value returned by invString() to a String variable.

public static void main(String[] args){
    ...
    String st = invString(str); //st stores the value of str with 
                                //letters' cases changed
}
Community
  • 1
  • 1
progyammer
  • 1,498
  • 3
  • 17
  • 29
1

You return you str object without updating it at all. You should generate a new string in which put the characters for which you reverse the case. Check the last answers in How can I invert the case of a String in Java?

Community
  • 1
  • 1
Massimo Petrus
  • 1,881
  • 2
  • 13
  • 26
-1

If you want to use the inverted string, you need to actually use the returned value, e.g:

str = invString (str)

Strings are immutable so you can't change the characters within them. You can only replace the string variable with a new string.

Modifying the characters you are accessing doesn't affect the string. You need to build a new string (look up StringBuilder) and return it.

djd0
  • 887
  • 10
  • 14
  • 1
    "You need to actually use the returned value" - this is not true. You can call a method with an return type other than void and not assign its returned value to some variable. – Turing85 Oct 29 '16 at 09:56
  • I didn't mean it that way! I just meant you need to use the returned value if you want to access the new value and not throw away the result. – djd0 Oct 29 '16 at 10:00
  • Then you should write it that way. – Turing85 Oct 29 '16 at 10:01
  • 1
    I have added a qualifier that should satisfy pedants – djd0 Oct 29 '16 at 10:06