-1

Hi I have a password generator and I have it mostly figured out. The only issue I am having is that when I print out my reversed string instead of printing the 2nd to last like I need. But it prints both of the last 2 letters of the first name. Here is the code:

import java.util.*;
public class Main
{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        System.out.println("enter first name here: ");
        String fname = input.next();

        System.out.println("enter middle name here: ");
        String mname = input.next();

        System.out.println("Enter last name here: ");
        String lname = input.next();
        
        System.out.println("Enter birthday (MMDDYYYY) here: ");
        int age = input.nextInt();
    
        
        lname = lname.substring(lname.length()-3);
        
        char resultfn = fname.charAt(1);

        char resultmn = mname.charAt(2);

        fname = fname.substring(fname.length()-2);

        System.out.print(resultfn);
        System.out.print(resultmn);
        System.out.print(lname);
        System.out.print(fname);

    }
}
Adam
  • 186
  • 1
  • 4
  • 20

1 Answers1

0

Ok so I figured it out what i did was add a new line of code which was

char reversedfn = fname.charAt(0);

after I did that it worked great!

I put it after the

fname = fname.substring(fname.length()-2);

and then had it print out the reversedfn line and it came out to the letter i needed. So it now looks like this.

import java.util.*;
public class Main
{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        System.out.println("enter first name here: ");
        String fname = input.next();

        System.out.println("enter middle name here: ");
        String mname = input.next();

        System.out.println("Enter last name here: ");
        String lname = input.next();
        
        System.out.println("Enter birthday (MMDDYYYY) here: ");
        int age = input.nextInt();
    
        
        lname = lname.substring(lname.length()-3);
        
        char resultfn = fname.charAt(1);

        char resultmn = mname.charAt(2);

        fname = fname.substring(fname.length()-2);

        char reversedfn = fname.charAt(0);

        System.out.print(resultfn);
        System.out.print(resultmn);
        System.out.print(lname);
        System.out.print(reversedfn);

    }
}