Saturday, February 2, 2019

java - method not updating original string variable

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....

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...