fork download
  1. import java.util.Scanner;
  2.  
  3. class Ideone {
  4. private static Scanner input;
  5.  
  6. public static void print(String message){
  7. System.out.println(message);
  8. }
  9. public static void main(String[] args) {
  10. input = new Scanner(System.in);
  11. System.out.println("Enter course code to validate i.e. IT2249: ");
  12. //assign value to input
  13. String code = input.nextLine();
  14.  
  15. // split into first letter and second letter from course code
  16. boolean fail = false;
  17. char firstLetter = code.charAt(0);
  18. char secondLetter = code.charAt(1);
  19.  
  20. if(code.length() != 6){
  21. print("Course code is incorrect, it should have 6 characters. Like - iT1234.");
  22. fail = true;
  23. }
  24. else if(firstLetter != 'i' && firstLetter != 'I'){
  25. //print("First letter should be `i` or `I`.");
  26. fail = true;
  27. }
  28. else if(secondLetter != 't' && secondLetter != 'T'){
  29. //print("Second letter should be `t` or `T`.");
  30. fail = true;
  31. }
  32. else{
  33. String lastFour = code.substring(2);
  34. try{
  35. int courseCde = Integer.parseInt(lastFour);
  36. }
  37. fail = true;
  38. //print("All last four characters of course should be numbers.");
  39. }
  40. }
  41. if(!fail)
  42. print("Right course code!");
  43. else if(code.length() == 6){
  44. for(int i = 0; i < 6; i++){
  45. checkForChar(code.charAt(i), i+1);
  46. }
  47. print("Wrong input format!");
  48. }
  49.  
  50. }
  51.  
  52. public static void checkForChar(char ch, int pos){
  53. if(pos == 1 && ch != 'i' && ch != 'I')print("First letter should `i` or `I` instead of " + ch);
  54. else if(pos == 1){print(pos + " is correct choice.");return;}
  55. if(pos == 2 && ch != 't' && ch != 'T')print("Second letter should `t` or `T` instead of " + ch);
  56. else if(pos == 2){print(pos + " is correct choice.");return;}
  57. if(pos > 2){
  58. try{
  59. int num = Integer.parseInt(""+ch);
  60. print(pos + " is correct choice.");
  61. }
  62. print(pos + " should be digit instead of " + ch);
  63. }
  64. }
  65. }
  66. }
Success #stdin #stdout 0.06s 4575232KB
stdin
itit00
stdout
Enter course code to validate i.e. IT2249: 
1 is correct choice.
2 is correct choice.
3 should be digit instead of i
4 should be digit instead of t
5 is correct choice.
6 is correct choice.
Wrong input format!