where is the wrong i want to write recursive method prints all small letters | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

where is the wrong i want to write recursive method prints all small letters

public class JavaApplication61 { public static void findSmallLetters(String s,int i) { if(i>= s.length()) { System.exit(1); } else if (s.charAt(0)>='a' && s.charAt(0)<='z') { System.out.println(s.charAt(0)); findSmallLetters(s, i+1); } else { findSmallLetters(s, i+1); } } public static void main(String[] args) { String a = "kASfk + adsAfqwF"; findSmallLetters(a,0); } }

26th Aug 2017, 12:18 PM
Ghiath Aletek
Ghiath Aletek - avatar
9 Answers
+ 3
Try to change the else if part like this: else if (s.charAt(i)>='a' && s.charAt(i)<='z') { System.out.println(s.charAt(i)); findSmallLetters(s, i+1); } It seems you forgot to use the i, and use zero instead. Hth, cmiiw
26th Aug 2017, 12:59 PM
Ipang
+ 2
@Ghiath Aletek tell me the error message, I tested it in Code Playground and it worked well. All the small case letters are printed out.
26th Aug 2017, 2:00 PM
Ipang
+ 2
@Ghiath Aletek here's the code I tried on Code Playground: public class JavaApplication61 { public static void findSmallLetters(String s,int i) { if(i>= s.length()) { System.exit(1); } else if (s.charAt(i)>='a' && s.charAt(i)<='z') { System.out.println(s.charAt(i)); findSmallLetters(s, i+1); } else { findSmallLetters(s, i+1); } } public static void main(String[] args) { String a = "kASfk + adsAfqwF"; findSmallLetters(a,0); } }
26th Aug 2017, 10:39 PM
Ipang
+ 2
Does it work?
27th Aug 2017, 5:13 AM
Ipang
+ 2
Okay, good to hear, see you later mate.
27th Aug 2017, 5:15 AM
Ipang
0
it worked but with errors
26th Aug 2017, 1:42 PM
Ghiath Aletek
Ghiath Aletek - avatar
0
C:\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 Build failed
26th Aug 2017, 2:59 PM
Ghiath Aletek
Ghiath Aletek - avatar
0
Thank you
27th Aug 2017, 5:06 AM
Ghiath Aletek
Ghiath Aletek - avatar
0
just change System.exit (0); make it zero
27th Aug 2017, 5:14 AM
Ghiath Aletek
Ghiath Aletek - avatar