Question

In: Computer Science

Write a java program that will take a line of input and go through and print...

Write a java program that will take a line of input and go through and print out that line again with all
the word numbers swapped with their corresponding numeric representations (only deal with numbers
from one to nine). Sample runs might look like this:
Please enter a line of input to process:
My four Grandparents had five grandchildren
My 4 grandparents had 5 grandchildren

without array and methods.

Solutions

Expert Solution

Below is your code:

import java.util.Scanner;

public class StringReplace {
        public static void main(String[] args) {
                // initializing Scanner object to get input from user
                Scanner sc = new Scanner(System.in);
                // Prompt user to take input from user
                System.out.println("Please enter a line of input to process:");
                // take complete line as input
                String line = sc.nextLine();

                // closing the scanner after taking input
                sc.close();

                // string has a replaceAll method which can replace
                // any substring to another substring
                // we can use the same for that
                // also if we add (?i) in front of the substring to be replaced
                // it will consider as case insensitive
                line = line.replaceAll("(?i)one", "1");
                line = line.replaceAll("(?i)two", "2");
                line = line.replaceAll("(?i)three", "3");
                line = line.replaceAll("(?i)four", "4");
                line = line.replaceAll("(?i)five", "5");
                line = line.replaceAll("(?i)six", "6");
                line = line.replaceAll("(?i)seven", "7");
                line = line.replaceAll("(?i)eight", "8");
                line = line.replaceAll("(?i)nine", "9");

                // printing the result
                System.out.println(line);

        }
}

Output

Please enter a line of input to process:
My four Grandparents had five grandchildren
My 4 Grandparents had 5 grandchildren


Related Solutions

Write a program in Java that will take as input two sets A and B, and...
Write a program in Java that will take as input two sets A and B, and returns a list of all functions from A to B.
Write java program that will ask for the user for 2 input lines and print out...
Write java program that will ask for the user for 2 input lines and print out all words that occur 1 or more times on both lines (case sensitive). Write this without arrays and method. Here is a sample run: <Output> Enter two lines to process. The quick brown fox jumps over a lazy dog The fox hound outruns the lazy dog The words that occur on both lines are: The fox lazy dog
Write a program that takes in a line of text as input, and outputs that line...
Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH yeH IN C++ PLEASE!
Write a complete Java program to print out the name bob
Write a complete Java program to print out the name bob
JAVA Take in a user input. if user input is "Leap Year" your program should run...
JAVA Take in a user input. if user input is "Leap Year" your program should run exercise 1 if user input is "word count" your program should run exercise 2 Both exercises should run in separate methods Exercise 1: write a java method to check whether a year(integer) entered by the user is a leap year or not. Exercise 2: Write a java method to count all words in a string.
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
Python Write a program that will analyse the string input and print “accept” or “reject” based...
Python Write a program that will analyse the string input and print “accept” or “reject” based on the pattern given Accept if it fulfils the following conditions -String length 9 -3 small alphabet (3 lowercase letter) -3 digits -3 big alphabet (3 uppercase letters) -1st alphabet should be a capital -Last alphabet should be a number -Two consecutive alphabets can't be small Reject if any of the conditions is absent So i want it to accept or reject my input,...
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
Write a Java program that uses nested for loops to print a multiplication table as shown...
Write a Java program that uses nested for loops to print a multiplication table as shown below.   Make sure to include the table headings and separators as shown.   The values in the body of the table should be computed using the values in the heading   e.g. row 1 column 3 is 1 times 3.
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT