Why can't I replace 2nd String? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why can't I replace 2nd String?

import java.util.*; public class Program { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s1=sc.nextLine(); String s2=sc.nextLine(); String lm="January February March Apirl May June July August September October November December"; String nm="123456789101112"; String[]lm1=lm.split(" "); String[]nm1=nm.split(""); for(int i=0;i<12;i++){ s1=s1.replaceAll(lm1[i],nm1[i]); s2=s2.replaceAll(lm1[i],nm1[i]); }

9th Jan 2023, 4:44 AM
Killiam Chances
Killiam Chances - avatar
1 Answer
+ 2
The string array <nm1> appears to be the issue here. It was splitted by blank string, means it doesn't have "10", "11" and "12". Set a delimiter for splitting e,g. using a comma; to get a valid month index string array String nm = "1,2,3,4,5,6,7,8,9,10,11,12"; ... String[] nm1 = nm.split( "," ); P.S. Prefer to attach code bit link inatead of raw text next time ~ especially when the code is rather long. Your raw text code is truncated from character limits ...
9th Jan 2023, 5:51 AM
Ipang