I’m desperately trying to remove white spaces from a String (that late i want to be able to convert into an int) but i can’t seem to get it all right.
String input;
if(GamePlay.GameStart == true){
input = Terminal.askString("Welcome to MASTERMIND! \n Please Give in Your Game Attributes \n");
input.replaceAll("\\s","");
}else
input = Terminal.askString("");
if (input.equals("Quit") || input.equals("quit")){
Quit();
}
else if(GamePlay.GameStart == true){
System.out.println(input); .......(code follows)
Can you please tell me,what it is that i’m doing wrong?
PS:I’ve tried \W” and \S” too
replace
with
It will work, because strings are immutable, so
replaceAll()won’t change your string object, it will return a new one. So you assign your variableinputto the string returned byinput.replaceAll("\\s","");Also, you should try to follow the Java naming conventions, and have your fields and variables start with a lowercase letters.
And, you can also replace
with
because in your version you compare the value of
GamePlay.GameStartwithtrue, and only execute theifblock if that evaluation istrue, whereas in my version, theìfblock is executed ifGamePlay.GameStartistrue(although the compiler probably optimizes it away anyway).On another note, you can also replace
with
because, well I think it’s obvious.