Need easy method | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Need easy method

Is there any way method to solve this code with java https://code.sololearn.com/cbjYMpzIX6oI/?ref=app

8th Oct 2022, 2:15 PM
Shivam Shiva
Shivam Shiva - avatar
2 Answers
+ 4
Your code is working fine, it does what the task requires. A couple of remarks: - generally, adding a lot of strings is very inefficient, you should use a StringBuilder https://www.baeldung.com/java-strings-concatenation - in the top branches of 'if' where you check the type of character, you always have a special case, like letter 'A'. You could simplify this inner part by using ternary expressions: (condition ? when_true : when_false) https://www.baeldung.com/java-ternary-operator - in my opinion the cleanest way would be to define a HashMap from each source character to target mapping, which already covers the special cases. If the mapped character is not found in the hashmap, then the default ~ char would be used. https://www.baeldung.com/java-hashmap I just can't resist adding, that if you are looking for an EASY method, then you should do this in Kotlin instead. there are lots of convenient collection methods there which make this look very complicated to do in Java.
8th Oct 2022, 2:56 PM
Tibor Santa
Tibor Santa - avatar
+ 2
Just for comparison, I did this in Kotlin with the hashmap idea. https://code.sololearn.com/cx82NL5QHTBU/?ref=app
8th Oct 2022, 3:29 PM
Tibor Santa
Tibor Santa - avatar