Search code examples
javastringintegerpasswords

how to input username by getting the half of the first name and half of the last name and the day of the given birthday


example of output should be

please help thank you in advance!!

the output of the code in username should be the 2 letter in firt name and 3 in last name and date number

public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        Scanner sc = new Scanner(System.in);
    
    System.out.println("Enter Fullname:");
    String fullname = sc.nextLine();
    System.out.println("Enter Birthday : ");
    String bday = sc.nextLine();
    
    System.out.println("Your Login Details");
    System.out.println("Enter Fullname:" + fullname);
    
    System.out.println("Enter Birthday : " + bday);
    System.out.println("Enter Username: " + );
    
    

    
    
    }
    }

Solution

  • Assuming the input will always be in the format you provided, you can use String.split() and String.substring() to extract the required information from the input as shown below.

    String[] splitName = fullName.split(" ");
    String firstName = splitName[0];
    String lastName = splitName[1];
    String day = bday.split("-")[1];
    
    String username = firstName.substring(0, 2) + lastName.substring(0, 3) + day;