+ 3

So, I wrote a code which prints all possible combinations of a three lettered word. However, it displays the error message "erro

import java.io.*; public class Program { public static void main() { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); char a []=new char[3]; int i,j,c=0; char ch; String str; System.out.println("Enter a 3-lettered string"); str=in.readLine(); for(i=0;i<3;i++) { ch=str.charAt(i); a[c++]=ch; } for(i=0;i<3;i++) { for(j=0;j<3;j++) { System.out.print(a[i]); if(i==j) continue; else { System.out.print(a[j]); } } System.out.println(); } } }

28th Jan 2018, 7:11 PM
Arpan Sircar
Arpan Sircar - avatar
3 Answers
+ 5
readLine throws an exception so you need to handle Use a try {...existing code...} catch (Exception e) {} Your main needs (String[] args) Your print of a[i] needs to be moved to outer loop
28th Jan 2018, 7:28 PM
John Wells
John Wells - avatar
+ 20
looks like some good amount of logical as well as syntax error 1)see main method 2)again look at the way u are making combination for 3 letter words ... combinations possible will be 9 (if letters can repeat) & 6 (if letters can't repeat)
28th Jan 2018, 7:25 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 1
Don't know if this will help too much, but try adding curly brackets to the if statement closer to the end of the code.
28th Jan 2018, 7:13 PM
Faisal
Faisal - avatar