Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN JAVA!: Read the input from the scanner Declare st1 and st2 as type String. De

ID: 3746428 • Letter: I

Question

IN JAVA!:

Read the input from the scanner

Declare st1 and st2 as type String. Declare ch1 and ch2 as char. Compare the strings and display, as appropriate

Which string is longer

The 5th character of st1, or a message if the string has less than 5 characters o Which word comes before in an alphanumeric comparison

o Display if ch1 appears in st1 or not

o The last position of ch2 in st2 (or -1 if not in the string)

Use printf for all of your output. Use appropriate format specifiers like %s, %d, %c, etc.

Sample output:

Example 1:

Enter two words and two characters:     hello world a w

Your words are: hello and world.    And your characters are a and w.

world is the same length as hello.

The fifth character in hello is o.

hello comes before world.

a is not found in hello.

The last position of w in world is index 0

Example 2:

Enter two words and two characters:     hello Hello w y

Your words are: hello and Hello.    And your characters are w and y.

Hello is the same length as hello.

The fifth character in hello is o.

Hello comes before hello.

w is not found in hello.

The last position of y in Hello is index -1

Example 3:

Enter two words and two characters:     java 823 w 2

Your words are: java and 823.     And your characters are w and 2.

java is longer than 823.

java is less than five characters long.

823 comes before java.

w is not found in java.

The last position of 2 in 823 is index 1

Explanation / Answer

import java.util.Scanner; public class StringOperationsInput { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter two words and two characters: "); String s1 = in.next(); String s2 = in.next(); char ch1 = in.next().charAt(0); char ch2 = in.next().charAt(0); if(s1.length() == s2.length()) { System.out.printf("%s is the same length as %s. ", s2, s1); } else if(s1.length() > s2.length()) { System.out.printf("%s is longer than %s. ", s1, s2); } else { System.out.printf("%s is longer than %s. ", s2, s1); } if(s1.length() < 5) { System.out.printf("%s is less than five characters long. ", s1); } else { System.out.printf("The fifth character in %s is %c. ", s1, s1.charAt(4)); } if(s1.compareTo(s2) < 0) { System.out.printf("%s comes before %s. ", s1, s2); } else if(s1.compareTo(s2) > 0) { System.out.printf("%s comes before %s. ", s2, s1); } if(s1.indexOf(ch1) == -1) { System.out.printf("%c is not found in %s. ", ch1, s1); } else { System.out.printf("%c is found in %s. ", ch1, s1); } System.out.printf("The last position of %c in %s is index %d ", ch2, s2, s2.indexOf(ch2)); } }