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

3. Write a method called reverse that accepts the Scanner object as its paramete

ID: 3800729 • Letter: 3

Question

3. Write a method called reverse that accepts the Scanner object as its parameter. This method asks the user for the entire name and prints the name in the reverse order. You must only use nextLine() method. If the user enters Mary Lumbardi then the method should output: Lumbardi Mary

main method: in this method, you need to ask the user how many times he/she wants to run each method and create a for loop for each of the method class. In the main you need to have three for loops for each of the methods except the method reverse. You must create a Scanner object so that you can pass it to different methods. Only one scanner object is needed for the whole program.

Enter your first and last name separated by a space > Mary Lambardi Here is your name in the reverse order: Lambardi Mary

Explanation / Answer

Source code:-

import java.io.*;
import java.util.Scanner;

class GFG {
   public static void main (String[] args) {
   String name;
   int n;
   int length ;
  
Scanner in = new Scanner(System.in);
  
System.out.print("Type a name: ");
name = in.nextLine();
length = name.length();
reverse(length,name);
}
  

   public static int reverse(int n,String name)
   {
   String rev = "",ch1="",kk="";
   System.out.println(" Reverse of entered string is: ");
   for ( int i =n-1 ; i>=0 ; i-- )
   {
rev = rev + name.charAt(i);
if(name.charAt(i) == ' ')
{
ch1=rev;
for(int j=ch1.length()-1;j>=0;j--)
{
kk=kk+ch1.charAt(j);
}
System.out.println(kk);
kk="";
rev="";
}
   }
   for(int j=rev.length()-1;j>=0;j--)
{
kk=kk+rev.charAt(j);
}
System.out.println(kk);
return 0;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote