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 that will take a line of input and go through and print out...
Write a program that will take a line of input and go through and print out that line again with all the uppercase letters swapped with the lowercase letters. JAVA Using printf
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.
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of miles, and the class of journey (1,2, or 3, for first, second, and third class respectively), for a train journey. The program should then calculate and display the fare of journey based on the following criteria: Note: Use Switch...case and if...else construct First (1) Class Second (1) Class Third (3) Class First 100 mile $ 3 per mile $ 2 per mile $ 1.50...
Write a java program that read a line of input as a sentence and display: ...
Write a java program that read a line of input as a sentence and display:  Only the uppercase letters in the sentence.  The sentence, with all lowercase vowels (i.e. “a”, “e”, “i”, “o”, and “u”) replaced by a strike symbol “*”.
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 java program. The program requirements are: the program will let the user input a...
Write a java program. The program requirements are: the program will let the user input a starting number, such as 2.  It will generate ten multiplication problems ranging from 2x1 to 2x10.  For each problem, the user will be prompted to enter the correct answer. The program should check the answer and should not let the user advance to the next question until the correct answer is given to the question being asked currently. After testing a total of 10 multiplication problems,...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code. Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code.   Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
Write a complete Java program to print out the name bob
Write a complete Java program to print out the name bob
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT