0

Just in regards to the below code I am having an issue where everything is running fine but I am not getting the desired output.

The code should take user input and print it but with all the casing of letters reversed. However even though toggleCase works with no issue once the input is returned to toggleStringCase it reverts back to how it was before it was sent to toggleCase.

I am having trouble understanding why this is occuring.

Could someone please point me in the right direction.

Ideally I don't want you to tell me the answer but rather just help me go the right way to solve this.

package loopy;
import java.io.*;

public class loopy {
    public static void main (String[] args) throws IOException {
        // TODO: Use a loop to print every upper case letter
        for (int i = 65; i < 91; i++) {
            System.out.println((char)i);
        }
        // TODO: Get input from user. Print the same input back but with cases swapped. Use the helper functions below.
         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        String input = in.readLine();
        in.close();     

        toggleStringCase(input);

        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
        out.write(input);
        out.close();
    }

    //Takes a single Character and reverse the case if it is a letter
    private static char toggleCase(char c) {
        int asciiValue = (int) c;
        if (asciiValue > 96 && asciiValue < 123){
            asciiValue = asciiValue - 32;
        }
        else if (asciiValue > 64 && asciiValue < 91){
            asciiValue = asciiValue + 32;
        }
        else {

        }
        c = (char) asciiValue;
        return c;
    }

    // Splits a string into individual characters that are sent to toggleCase to have their case changed
    private static String toggleStringCase(String str) {
        String reversedCase = new String();
        for (int i = 0; i < str.length(); i++) {
            char letter = str.charAt(i);
            toggleCase(letter);
            reversedCase = reversedCase + letter;
        }
        str = reversedCase;
        return str;
    }
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
Julien Powell
  • 186
  • 1
  • 14
  • 2
    Since you don't want the answer, read this https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Slimu Mar 06 '19 at 07:29
  • 2
    You do not use the return value of `toggleCase(letter)` in the loop of `toggleStringCase` – vahdet Mar 06 '19 at 07:30
  • You don't have to cast `char` to `int` and vice-versa, you can do `asciiValue => 'a' && asciiValue <= 'z'` instead of `asciiValue > 96 && asciiValue < 123` – Thum Choon Tat Mar 06 '19 at 07:39

2 Answers2

4
toggleStringCase(input);

I think you may want to get the output of that function. You seem to be assuming that input will be changed - that is not the case. See Is Java "pass-by-reference" or "pass-by-value"?

kutschkem
  • 7,826
  • 3
  • 21
  • 56
  • Thanks that was what I needed. That and another article plus re-reading everything. This is really something my lecturer should have explained. – Julien Powell Mar 06 '19 at 07:57
3

Parameters in Java are passed by value. You can't change a value passed to a method that way. However, you are returning the value you want - just assign it back to your variable:

input = toggleStringCase(input);
Mureinik
  • 297,002
  • 52
  • 306
  • 350