Home > Enterprise >  How can I append the first and middle character of a line that changes based on the letters that are
How can I append the first and middle character of a line that changes based on the letters that are

Time:10-10

I have a small program that needs me to append the first letter and the middle letter of the line that is printed out by the user but I don't understand how to do that here is my code so you can see what i have got.

import java.util.*;
public class Main
{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("This is a password generator that will take some information and create a password.");

        System.out.println("");

        System.out.println("In the following prompts please enter your information in all lowercase. Thanks!");
        
        System.out.println("");
        
        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: ");
        String age = input.next();
    
        
        char resultfn = fname.charAt(1);/// This is where I picked the 2nd character of the front half of the first name

        char resultage0 = age.charAt(2);///Im using these 2 to get the day the person ented for their birthday
        char resultage1 = age.charAt(3);

        char resultmn = mname.charAt(1);

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

        lname = lname.substring(lname.length()-3);

        char reversedfn = fname.charAt(0);

        System.out.print(resultfn);///This is the front part of the first name
        System.out.print(reversedfn);///This is the reversed part of the fist name
 
        System.out.print(resultmn);///This is where I am printing the second letter of the middle name
 
        System.out.print(resultage0);
        System.out.print(resultage1);

        System.out.print(lname); ///This is where I am printing the reversed 3 letters for the last name


    }
}

after the line prints out for the user i need to add the first letter and middle letter and get a number based on that and add it to the end. does anyone know how I could do this?

CodePudding user response:

I Ran your code and obtained below results

This is a password generator that will take some information and create a password.

In the following prompts please enter your information in all lowercase. Thanks!

enter first name here: 
Vaibhav
enter middle name here: 
GHK
Enter last name here: 
Kumar
Enter birthday (MMDDYYYY) here: 
01011993
aaH01mar

Now the response I am getting are not as per the intended results you mentioned in the comments for e.g char reversedfn = fname.charAt(0); the fname you obtained is simply the last two characters of the string and fname.charAt(0) is returning the zeroth element of the fname's last character value,so No concept of reversing is involved , secondly you want the middle element of a line to get it you need something like below algorithm :

string!=null&&string.isempty()==false;
if(string%2!=0)
return string[string.length/2];

But for even length string your middle element criteria is unspecified ,Hence kindly provide proper info

CodePudding user response:

I cannot understand the question correctly but from what I understood you need some kind of number to be added to the final result. not sure which char you need I am just assuming you need ASCII value :D

I am taking the first char and mid-char of the current result and upending their ASCII sum.

import java.util.*;
public class Main
{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("This is a password generator that will take some information and create a password.");

        System.out.println("");

        System.out.println("In the following prompts please enter your information in all lowercase. Thanks!");
        
        System.out.println("");
        
        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: ");
        String age = input.next();
    
        
        char resultfn = fname.charAt(1);/// This is where I picked the 2nd character of the front half of the first name

        char resultage0 = age.charAt(2);///Im using these 2 to get the day the person ented for their birthday
        char resultage1 = age.charAt(3);

        char resultmn = mname.charAt(1);

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

        lname = lname.substring(lname.length()-3);

        char reversedfn = fname.charAt(0);
        StringBuilder sb = new StringBuilder();
        sb.append(resultfn);
        sb.append(reversedfn);
        sb.append(resultmn);
        sb.append(resultage0);
        sb.append(resultage1);
        sb.append(lname);
        String result = sb.toString();
        System.out.println(result);
        
        int number = (int) result.charAt(0)   (int) result.charAt(result.length()/2);
        sb.append(number);
        result = sb.toString();
        System.out.println(result);
 
    }
}

this is that sample result.

This is a password generator that will take some information and create a password.

In the following prompts please enter your information in all lowercase. Thanks!

enter first name here: 
rohit
enter middle name here: 
pratap
Enter last name here: 
singh
Enter birthday (MMDDYYYY) here: 
02211999
oir21ngh
oir21ngh160

provide some better explanation of the problem.

  • Related