0

jungle camping: no output, no errors

no errors, no output, idk why it doesnt output anything, everything works until last checks for animal sounds for some reason import java.util.*; public class Program { public static void print(String x) { x += " "; System.out.print(x); } public static void main(String[] args) { Vector <String> v = new Vector <String>(); Scanner sc = new Scanner(System.in); while (sc.hasNext()) { v.add(sc.next()); } for (String word : v) { if (word == "Grr") { print("Lion"); continue; } if (word == "Rawr") { print("Tiger"); continue; } if (word == "Ssss") { print("Snake"); continue; } if (word == "Chirp") { print("Bird"); } } } }

31st Dec 2020, 12:08 PM
ItzNekS
ItzNekS - avatar
3 Answers
+ 4
ItzNekS , first to compare just the content of the string use equals() method, "==" is used for reference comparison. Remove all continue statements. Then everything works. Look at the code. https://code.sololearn.com/covnwxAq6L5d/?ref=app
31st Dec 2020, 12:21 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
TheWh¡teCat 🇧🇬 why remove continue? wont it cause pointless checking?
31st Dec 2020, 12:22 PM
ItzNekS
ItzNekS - avatar
0
ItzNekS, yes it works, but the code doesn't look clear. It could be rewritten. Or for example you can use replaceAll() method to solve it.
31st Dec 2020, 12:31 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar