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); } }
9 Antworten
+ 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
+ 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.
+ 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);
}
}
+ 2
Does it work?
+ 2
Okay, good to hear, see you later mate.
0
it worked but with errors
0
C:\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 Build failed
0
Thank you
0
just change System.exit (0);
make it zero