Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- import java.util.stream.IntStream;
- public class RedditHelp {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- System.out.print("Enter the String: ");
- String entered = scan.nextLine();
- String[] split = entered.trim().split(" ");
- String[] resultString = new String[split.length];
- // end for-loop
- IntStream.range(0, split.length).forEach(i -> {
- if (isNumeric(split[i])) {
- switch (Integer.parseInt(split[i])) {
- case 1 -> resultString[i] = "one";
- case 2 -> resultString[i] = "two";
- case 3 -> resultString[i] = "three";
- case 4 -> resultString[i] = "four";
- case 5 -> resultString[i] = "five";
- case 6 -> resultString[i] = "six";
- case 7 -> resultString[i] = "seven";
- case 8 -> resultString[i] = "eight";
- case 9 -> resultString[i] = "nine";
- default -> resultString[i] = "zero";
- } // end switch
- } else {
- if (split[i].equalsIgnoreCase("+")) {
- resultString[i] = "plus";
- } else if (split[i].equalsIgnoreCase("-")) {
- resultString[i] = "minus";
- } else if (split[i].equalsIgnoreCase("*")) {
- resultString[i] = "times";
- } else if (split[i].equalsIgnoreCase("/")) {
- resultString[i] = "divided by";
- }
- }
- });
- for (int j = 0; j < resultString.length; j++) {
- if (j == 0) {
- System.out.print(resultString[j].substring(0, 1).toUpperCase() + resultString[j].substring(1));
- } else {
- System.out.print(resultString[j]);
- }
- System.out.print(" ");
- }
- System.out.println("equals " + (Integer.parseInt(split[0]) + Integer.parseInt(split[2])) + ".");
- } // end main()
- private static boolean isNumeric(String str) {
- try {
- Integer.parseInt(str);
- return true;
- } catch(NumberFormatException e){
- return false;
- }
- }
- }
Add Comment
Please, Sign In to add comment