0

I'm having problem with this code that i wrote to convert the string....the string is not being updated throughout the code...

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class PigLatin {
private static String str = "pig";
private char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
private Character firstChar, secondChar;

public void inputString(String str) {
    try {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader input = new BufferedReader(isr);
        str = input.readLine();
    } catch (Exception e) {
        // TODO: handle exception
        System.out.println("unable to input String");
        e.printStackTrace();
    }
    firstChar = str.charAt(0);
    secondChar = str.charAt(1);
    System.out.println(firstChar);
    System.out.println(secondChar);
}

public static void main(String[] args) {
    PigLatin pl = new PigLatin();
    System.out.println("Enter an english word to convert to PigLatin");
    pl.inputString(str);
    System.out.println(str);
    pl.convert(str);
}

public void convert(String lowerCase) {
    // TODO Auto-generated method stub
    System.out.println("Original string:" + str);
    if (isVowel())
        str.concat("yay");
    else {
        String suffix = firstChar.toString() + "ay";
        // String suffix = String.valueOf(str.charAt(0))+"ay";
        str = str.substring(1) + suffix;
    }
    System.out.println("PigLatin:" + str);

}

public boolean isVowel() {
    // TODO Auto-generated method stub
    for (char ch : vowels) {
        if (firstChar.equals(ch)) {
            if (firstChar.equals('v') && secondChar.equals('q'))
                return false;
            return true;
        }
        if (firstChar.equals('y') && !secondChar.equals(ch))
            return true;
    }
    return false;
}
    }

The output is as follows: Enter an english word to convert to PigLatin

kite

k "first character"

i "second character"

pig

Original string:pig

PigLatin:igkay

Why is the string not being updated even if I'm giving the input in the command line evn though the first and second characters are correctly read from the string that I input..Please help....

user3243678
  • 11
  • 1
  • 3

4 Answers4

4
if (isVowel())
        str.concat("yay");

Strings are immutable in java, so you have to update str:

if (isVowel())
        str = str.concat("yay");

If you don't want to reassign str everytime, you can declare str as StringBuilder and update it via the append method:

private static StringBuilder str = new StringBuilder("pig");

//other code...

public void convert(String lowerCase) {
    //other code...
    if (isVowel())
        str.append("yay"); // this will modify the current object, as it is 
                           // a StringBuilder and not a string
    //other code...
}
BackSlash
  • 20,445
  • 19
  • 77
  • 124
0
str.concat("yay")

should be

str = str.concat("yay") or shorthand str += "yay"

as Strings are immutable in Java. This means that their value can't be changed.

Ben Dale
  • 2,172
  • 12
  • 14
0
pl.inputString(str);

The above statement doesnt change pl.str, hence pl.str = pig

pl.convert(str);

executes below code (for 'kite' as input)

String suffix = firstChar.toString() + "ay";  // result = kay
str = str.substring(1) + suffix; // str=pig, hence ig + kay = igkay

The above code doesnt reassign pl.str in inputString()


UPDATE: (same as comments)
Either static or instance variable doesnt matter in your code because in the method inputString(String str), you are assigning the local variable passed in argument and not the static variable str.

Since Java is pass by value, pl.inputString(str) will result in a reference copy. You are reassigning the local reference in that method. That means local-reference str and PigLatin.str references are different and referring to different objects. To update PigLatin.str, It is not possible to change the string object value since it is immutable. Use a StringBuilder instead and assign the same in inputString(str)

p1nkrock
  • 1,526
  • 1
  • 9
  • 6
  • but doesnt making the string static shoud fix this problem , cause then str is shared throughout the class and not limited to any instance. – user3243678 Jan 28 '14 at 09:05
  • Either static or instance variable doesnt matter in your code because in the method inputString(String str), you are assigning the local variable passed in argument and not the static variable str. – p1nkrock Jan 28 '14 at 09:11
  • Since Java is pass by value, pl.inputString(str) will result in a reference copy. You are reassigning the local reference in that method. That means local-reference str and PigLatin.str references are different and referering to different objects. It is not possible to change the string object value since it is immutable. Use a StringBuilder instead. – p1nkrock Jan 28 '14 at 09:20
  • well after reading about it some more....I now get it........I just need to remove all the method parameters and it'll be fine.....thanks mate for introducing me with this concept... – user3243678 Jan 28 '14 at 09:43
-1

if (isVowel())

    str.concat("yay");

You can change this to..

if (isVowel())

     str=str+"yay";
Sathesh
  • 39
  • 5