Splitting strings Java code coach | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Splitting strings Java code coach

Only 2 hidden test cases fail and i don’t know why. Here’s my code: import java.util.*; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); String s = input.nextLine(); int n = input.nextInt(); ArrayList<Character> r = new ArrayList<Character>(); String res = ""; for(int i=0;i<s.length();i++){ if(i==n){ n=n+n; r.add('-'); } r.add(s.charAt(i)); } for(char j : r){ res += j; } System.out.println(res); } }

22nd Feb 2023, 10:14 AM
Nikola Markov
2 Answers
+ 4
Nikola Markov , your code could be rewritten this way. Add the symbol "-", only if position in string is multiple of n and differ from 0. Look at the code. Hope it helps you. https://code.sololearn.com/c2R13bRmJ47R/?ref=app
22nd Feb 2023, 11:05 AM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
Thank you for the help!
22nd Feb 2023, 11:15 AM
Nikola Markov