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.
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 complete Java program to print out the name bob
Write a complete Java program to print out the name bob
I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
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 Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT