+ 1
Jungle camping (codecoach) Help Java
I am tired for thinking this problem for few days and finally I couldn't solve it. In spite of tried a lot of patterns... Please help me.(sorry for poor English) import java.util.Scanner ; public class Program { public static void main(String[] args) { Scanner scn = new Scanner(System.in); String s = scn.nextLine(); String[] words = s.split("\\s+"); String[] n = {"Grr", "Rawr", "Ssss", "Chirp"}; String[] a = {"Lion", "Tiger", "Snakes", "Bird"}; for(int i=0; i<=n.length; i++){ for(int j=0; j<=words.length; j++){ if(words[j]==n[i]){ System.out.print(a[i]); } } }
12 Antworten
+ 3
Sally Oh sry you need to change words.length to n.length and vise versa as well..
i<words.length
j<n.length
+ 2
Array index, string indexes starts from 0.
And you are accessing beyond array length.
If array length is n (index values are only 0 to n-1)
Ex: 4 length means 0,1,2,3
So use < only instead of <=.
For string length also...
+ 2
Here you go.
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner words = new Scanner(System.in);
String word2 = words.nextLine();
word2=word2.replace("Grr","Lion");
word2=word2.replace("Ssss","Snake");
word2 =word2.replace("Rawr","Tiger");
word2=word2.replace("Chirp","Bird");
System.out.println(word2);
}
}
+ 2
Joe Hutchens, maybe it's better this way?
import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Program
{
private static final Map<String, String> ANIMALS_BY_SOUND = new HashMap<>() {
{
put("Grr", "Lion");
put("Rawr", "Tiger");
put("Ssss", "Snake");
put("Chirp", "Bird");
}
};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String inputString = scanner.nextLine();
System.out.println(getAnimalsBySounds(inputString));
}
private static String getAnimalsBySounds(String sounds) {
Pattern soundPattern = Pattern.compile("[a-zA-Z]+");
Matcher soundsMatcher = soundPattern.matcher(sounds);
return soundsMatcher.replaceAll(sound -> ANIMALS_BY_SOUND.get(sound.group()));
}
}
+ 1
it's probably because the animal names are glued when printed. try this:
System.out.println(a[i]+" ");
+ 1
//for(int i=0; i<=n.length; i++){
for(int i=0; i< n.length; i++){ //<
//for(int j=0; j<=words.length; j++){
for (int j=0; j< words.length; j++){ //<
// if(words[j]==n[i]){
if (words[j].equals(n[i]) ) { //equals
System.out.print(a[i]);
}
}
}
} //added
} // added
+ 1
May be because of order may changing...
Try with if(words[i].equals(n[j]){ //j, i => i, j
System.out.print(a[j] +" "); //i => j
break;
}
Sally
Copy & Paste exactly same code,
Seems code is fine and last }} braces missing, hoping it may missing here only.
+ 1
Jayakrishna🇮🇳
Sorry. That was copy & paste mistake. In codecoach, two braces are written.
I changed order. #3 was become successful. But #2 and #5 are failed...
+ 1
Can you show update code?
Are you 3 changes above.. Sally
0
Thank you for your comment! John Robotane Jayakrishna🇮🇳 zemiak
Almost success!
But only #3 case still failed.
Please let me know if you know anything.
import java.util.Scanner ;
public class Program
{
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String s = scn.nextLine();
String[] words = s.split("\\s+");
String[] n = {"Grr", "Rawr", "Ssss", "Chirp"};
String[] a = {"Lion", "Tiger", "Snake", "Bird"}; //Snakes→Snake
for(int i=0; i<n.length; i++){ //<= → <
for(int j=0; j<words.length; j++){ //<= → <
if(words[j].equals(n[i])){
System.out.print(a[i]+" "); // a[i] → a[i]+" "
}
}
}
0
Jayakrishna🇮🇳
import java.util.Scanner ;
public class Program
{
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String s = scn.nextLine();
String[] words = s.split("\\s+");
String[] n = {"Grr", "Rawr", "Ssss", "Chirp"};
String[] a = {"Lion", "Tiger", "Snake", "Bird"};
for(int i=0; i<n.length; i++){
for(int j=0; j<words.length; j++){
if(words[i].equals(n[j])){
System.out.print(a[j]+" ");
break;
}
}
}
}
}
0
Thank you!!!! Jayakrishna🇮🇳
All cases were success!😊