Question

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
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Write java code that prompts the user to enter his or her full name, then print the name in reverse order

Answers:

Raw java code:

//Importing the Scanner to get input
import java.util.Scanner;
//creating a class called Name_Reverse
public class Name_Reverse {
   //main method
   public static void main(String[] args) {
       //dialogue to the user
       System.out.print("Please enter your full name: ");
       //Creating an object using Scanner class
       Scanner inp=new Scanner(System.in);
       //taking the String using .nextline method from the object created previously
       String name= inp.nextLine();
       //splitting the string based on spaces entered in between the name, and creating the array
       String[] rev=name.split(" ");
       //output dialogue to the user
       System.out.print("Your name in reverse order is ");
       //looping through the array to print the reverse order of names
       for(int i=rev.length-1;i>=0;i--) {
           // condition to not to print the , at the end as per your requirement show in your output
           if(i==0) {
               System.out.print(rev[i]);
           }
           //condition to print the reverse of the name by inserting , after every string
           else {
               System.out.print(rev[i]+",");
           }
       }
   }
}

Code in Editor:

output:

Please feel free to comment in the comment section if you have any doubts or queris

Thank you :)

Add a comment
Know the answer?
Add Answer to:
Write Java code that prompts the user to enter his or her full name, then prints...
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
  • 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....

  • 1. Write a function that prompts the user to enter his or her first name and...

    1. Write a function that prompts the user to enter his or her first name and last name, as two separate values. This function should return both values to the caller via additional pointer (or reference) parameters that are passed to the function. Try doing this first with pointers and then with references. (Hint: the function signature will look be similar to the swap function from earlier!)

  • java Write a method named printEntireFile that prompts the user for a file name and points...

    java Write a method named printEntireFile that prompts the user for a file name and points the contents of that file to the console as output. You may assume that the file exists. For example. If the file example. txt contains the following input data: Then the following would be an example dialogue of your method:

  • Design a modular program using pseudo-code which prompts a user for their first name, followed by...

    Design a modular program using pseudo-code which prompts a user for their first name, followed by their last name; then displays a "Hello" salutation which concatenates their first name with their last name. Sample output (user input shown in red): Please enter your first name: John Please enter your last name: Smith Hello, John Smith! Your program must include a main module and one function; this function prompts the user for either their first name or last name, using a...

  • Write a Java program called Flying.java that, firstly, prompts (asks) the user to enter an input...

    Write a Java program called Flying.java that, firstly, prompts (asks) the user to enter an input file name. This is the name of a text file that can contain any number of records. A record in this file is a single line of text in the following format: Num of passengers^range^name^manufacturer^model where: Num of passengers is the total number of people in the plane. This represents an integer number, but remember that it is still part of the String so...

  • java programe Write a program that prompts the user to enter in an integer number representing...

    java programe Write a program that prompts the user to enter in an integer number representing the number of elements in an integer array. Create the array and prompt the user to enter in values for each element using a for loop. When the array is full, display the following: The values in the array on a single line. The array with all of the elements reversed. The values from the array that have even numbered values. The values from...

  • Write a program in java that asks a user for a file name and prints the...

    Write a program in java that asks a user for a file name and prints the number of characters, words (separated by whitespace), and lines in that file. Then, it replaces each line of the file with its reverse. For example, if the file contains the following lines (Test1.txt): This is a test Hi there Output on the console: Number of characters: 22 Number of words: 6 Number of lines: 2 Data written to the file (Test1.txt): tset a si...

  • IN JAVA (Maximum consecutive increasingly ordered sub-string) Write a program that prompts the user to enter...

    IN JAVA (Maximum consecutive increasingly ordered sub-string) Write a program that prompts the user to enter a string and displays the maximum consecutive increasingly ordered sub-string. Analyze the time complexity of your program and explain. Here is a sample run: Enter a string: abcabcdgabxy output abcdg Time complexity is O( ). -n must be display between the parenthesis.

  • 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

  • Write a program that asks the user for his or her name, and then asks the user to enter a sentence that describes him or herself.

    23. Personal Weeb Page GeneratorWrite a program that asks the user for his or her name, and then asks the user to enter a sentence that describes him or herself. Here is an example of the program's screen:Enter your name: Julie Taylor [Enter]<html>  <head>  </head>  <body>    <center>      <h1>Julie Taylor</h1>    </center>    <hr />    I am a computer science major, a member of the Jazz club,    and I hope to work as a mobile app developer after I graduate.    <hr />  </body> </html>Describe yourself: I am a computer science major, a member of theOnce the user has entered the requested input, the program should create an HTML file,...

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