Question: Hi, I'm working on a simple Java program that is looking for the following, Create a project that asks the users name, then reverses them.

Hi, I'm working on a simple Java program that is looking for the following,

Create a project that asks the users name, then reverses them. The first name needs to be reversed and used as the first name, and the last name needs to be reversed and used as the last name. That is, if the users name is Hal Morgan, the reversed version is Lah Nagrom, first name is still first, last name is still last. Notice that the first letter of each name must be capitalized.

This is what I have so far: public static void main(String[] args)

{ Scanner stdIn = new Scanner(System.in);

String name, backwards = ""; int i, length;

System.out.print("What is your name? ");

name = stdIn.nextLine(); length = name.length();

for (i = length - 1; i >= 0; i--)

{ backwards += name.charAt(i); name = Character.toUpperCase(name.charAt(0)) + name.substring(1); }

System.out.println("It's a backwards day! " + backwards );

The problem is that it is not putting the first name first, and the last name last. How do I make sure the first name is still first(but backwards), and the last name last(still backwards)?

Rigth now it prints out " It's a backwards day! nagrom laH"

And I need it to print out "It's a backwards day! Lah Nagrom"

Thanks

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!