Question

Write a Java class named StringProcess that prompts the user to enter two non-empty strings and...

Write a Java class named StringProcess that prompts the user to enter two non-empty strings and report the followings: 1) The length of the two strings 2) The ASCII value of the first letter in the first string 3) The ASCII value of the last letter in the second string 4) Whether the second string is a substring of the first string.

Outputs: Your output should look similar as follows.

Please enter the first string: Carrot

Please enter the second string: Car

The ASCII value of the first letter in string carrot is: 67

The ASCII value of the last letter in string Car is: 114

The second string Car is a substring of the first string Carrot.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class StringProcess {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Please enter the first string: ");
        String s1 = in.nextLine();
        System.out.print("Please enter the second string: ");
        String s2 = in.nextLine();
        System.out.println("The ASCII value of the first letter in string " + s1 + " is: " + (int) s1.charAt(0));
        System.out.println("The ASCII value of the last letter in string " + s2 + " is: " + (int) s2.charAt(s2.length() - 1));
        if (s1.contains(s2)) {
            System.out.println("The second string " + s2 + " is a substring of the first string " + s1 + ".");
        } else {
            System.out.println("The second string " + s2 + " is not a substring of the first string " + s1 + ".");
        }
    }
}

Add a comment
Know the answer?
Add Answer to:
Write a Java class named StringProcess that prompts the user to enter two non-empty strings and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Program must be in Python 3. Write a program that prompts the user to enter two...

    Program must be in Python 3. Write a program that prompts the user to enter two strings and then displays a message indicating whether or not the first string starts with the second string. Your program must satisfy the following requirements. 1. Your program must include a function called myStartsWith that takes two string parameters. This function returns True if the rst string starts with the second string and returns False otherwise. 2. Your program must include a main function...

  • Problem 1: Dynamic Grocery Array Using Java to write a program that prompts the user to...

    Problem 1: Dynamic Grocery Array Using Java to write a program that prompts the user to enter an item’s name for each position in the array. The program then prints uses a loop to output the array. Example 1: Banana 2: Orange 3: Milk 4: Carrot 5: Chips Banana, Orange, Milk, Carrot, Chips Problem 2: Print Characters in String Array    Declare a string array of size 5. Prompt the user enters five strings that are stored in the array....

  • Write Java code that prompts the user to enter his or her full name, then prints...

    Write Java code that prompts the user to enter his or her full name, then prints the name in reverse order (i.e., last name, first name). Read an entire line as a single string. Here is an example dialogue with the user: Please enter your full name: Sammy Jankis Your name in reverse order is Jankis, Sammy

  • write a java program that prompts a user to enter two different numbers, divide the first...

    write a java program that prompts a user to enter two different numbers, divide the first number by second and prints the result

  • Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class...

    Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...

  • Write a program that uses String method regionMatches to compare two strings input by the user....

    Write a program that uses String method regionMatches to compare two strings input by the user. The program should prompt the user to enter two strings, the starting index in the first string, the starting index in the second string, and the number of characters to be compared. The program should print whether or not the strings are equal, (Ignore the case of the characters during comparison.) 四SAMPLE RUN #1: java StringCompare Highlight: None D Show Highlighted Only Interactive Session...

  • Write a program named "VegetablePricer" which prompts the user for a vegetable from the pricing table...

    Write a program named "VegetablePricer" which prompts the user for a vegetable from the pricing table shown below. The program will then display the cost per pound for that vegetable, again using the table provided. Your program must use a switch statement to process the input value and display the desired output. Be sure to provide a default case in your switch statement which handles unrecognized input. Use named constants for all constant strings (e.g. prompts and vegetable names) and...

  • In Java, write a program that prompts the user to input a sequence of characters and...

    In Java, write a program that prompts the user to input a sequence of characters and outputs the number of vowels. You will need to write a method to return a value called isAVowel which will return true if a given character is a vowel and returns false if the character is not a vowel. A second method, called isAConstant, should return true if a given character is a constant and return false if the character is not a constant....

  • Write java program that prompts the user to enter integers from the keyboard one at a...

    Write java program that prompts the user to enter integers from the keyboard one at a time. Program stops reading integers once the user enters the same value three times consecutively. Program then outputs "Same entered 3 in a row. "

  • In Java - Write a program that prompts the user to enter two integers and displays...

    In Java - Write a program that prompts the user to enter two integers and displays their sum. If the input is incorrect, prompt the user again. This means that you will have a try-catch inside a loop.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT