binibiningtinamoran

RedditHelp

May 3rd, 2021 (edited)
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. import java.util.stream.IntStream;
  4.  
  5. public class RedditHelp {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scan = new Scanner(System.in);
  9.         System.out.print("Enter the String: ");
  10.         String entered = scan.nextLine();
  11.  
  12.         String[] split = entered.trim().split(" ");
  13.         String[] resultString = new String[split.length];
  14.  
  15.         // end for-loop
  16.         IntStream.range(0, split.length).forEach(i -> {
  17.             if (isNumeric(split[i])) {
  18.                 switch (Integer.parseInt(split[i])) {
  19.                     case 1 -> resultString[i] = "one";
  20.                     case 2 -> resultString[i] = "two";
  21.                     case 3 -> resultString[i] = "three";
  22.                     case 4 -> resultString[i] = "four";
  23.                     case 5 -> resultString[i] = "five";
  24.                     case 6 -> resultString[i] = "six";
  25.                     case 7 -> resultString[i] = "seven";
  26.                     case 8 -> resultString[i] = "eight";
  27.                     case 9 -> resultString[i] = "nine";
  28.                     default -> resultString[i] = "zero";
  29.                 } // end switch
  30.  
  31.             } else {
  32.                 if (split[i].equalsIgnoreCase("+")) {
  33.                     resultString[i] = "plus";
  34.                 } else if (split[i].equalsIgnoreCase("-")) {
  35.                     resultString[i] = "minus";
  36.                 } else if (split[i].equalsIgnoreCase("*")) {
  37.                     resultString[i] = "times";
  38.                 } else if (split[i].equalsIgnoreCase("/")) {
  39.                     resultString[i] = "divided by";
  40.                 }
  41.             }
  42.         });
  43.  
  44.         for (int j = 0; j < resultString.length; j++) {
  45.             if (j == 0) {
  46.                 System.out.print(resultString[j].substring(0, 1).toUpperCase() + resultString[j].substring(1));
  47.             } else {
  48.                 System.out.print(resultString[j]);
  49.             }
  50.             System.out.print(" ");
  51.         }
  52.         System.out.println("equals " + (Integer.parseInt(split[0]) + Integer.parseInt(split[2])) + ".");
  53.  
  54.     } // end main()
  55.  
  56.     private static boolean isNumeric(String str) {
  57.         try {
  58.             Integer.parseInt(str);
  59.             return true;
  60.         } catch(NumberFormatException e){
  61.             return false;
  62.         }
  63.     }
  64. }
  65.  
Add Comment
Please, Sign In to add comment