Advertisement
yakuzata

Counting with words

Feb 17th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. public class TooLongString
  2. {
  3. public static void main(String[] args)
  4. {
  5. String myString = "Codix be ready for my sunshine!";
  6. String parts[] = myString.split(" ");
  7. int getIforSwitch = 0;
  8.  
  9. for (int i = 0; i < parts.length + 1; i++) { // parts.length + 1 because .length starts from 0;
  10. getIforSwitch = i;
  11. }
  12.  
  13. switch (getIforSwitch) {
  14. case 1:
  15. System.out.println("This string contains one word!");
  16. break;
  17. case 2:
  18. System.out.println("This string contains two words!");
  19. break;
  20. case 3:
  21. System.out.println("This string contains three words!");
  22. break;
  23. case 4:
  24. System.out.println("This string contains four words!");
  25. break;
  26. case 5:
  27. System.out.println("This string contains five words!");
  28. break;
  29. case 6:
  30. System.out.println("This string contains six words!");
  31. break;
  32. case 7:
  33. System.out.println("This string contains seven words!");
  34. break;
  35. case 8:
  36. System.out.println("This string contains eight words!");
  37. break;
  38. case 9:
  39. System.out.println("This string contains nine words!");
  40. break;
  41. case 10:
  42. System.out.println("This string contains ten words!");
  43. break;
  44. default:
  45. System.out.println("This string contains more than ten words!");
  46. }
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement