Question: 4 (Code only) Write a program that asks the user to enter their name (using Scanner). Store this value in a String variable. Then output
4 (Code only) Write a program that asks the user to enter their name (using Scanner). Store this value in a String variable. Then output the middle character of the string. If the string has an even number of letters, it has 2 middle characters. In this case, you can output the right-middle character. Example console output: Please enter your name: Zain The middle character is i Name your class Hw1pr4 and your file Hw1pr4.java
This is what I have so far but I cant figure out the errors and make it work.
Import java. utIL,Scanner; public class Hw1pr4 { public static void main(String [1 args) Scanner innew Scanner(System.in); System.out.print ("Please enter your name: "); String str -in.nextLine); L2 13 int position; int length; if (str.length() % 2 == 0) 0 15 position -str.length)/2; length - 1; 18 L9 else positionstr.length) / 2;5 length 1; System.out.print("The middle character in the string: " name.charAr(position)+"\I
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
