Home > Enterprise >  Generate a username using string parameters
Generate a username using string parameters

Time:01-23

I'm new to Java. I am tasked with creating a menu program, one option is to generate a username in the form of first initial and surname. The method is stringOperation(String f, String s) Variables are fName and sName.

Here is the code. I have highlighted the areas I need help with. The rest of the code is OK, I think. This is a section of the pseudocode that explains what is required:

stringOperation(String f, String s)

  • 3.1.1 Assign first character of first initial to variable using f.substring(start position, length of string).
  • 3.1.2 Concatenate first initial with users surname.
  • 3.1.3 print username to console.
import java.util.Scanner; // imports scanner class

public class Assessment {
    public static void main(String[] args) { //main method

        menu();  //call menu method
    }

    public static void menu() { //method to display menu options
        int choice;
        String fName;
        String sName;

        Scanner sc = new Scanner(System.in);

        //displays menu options
        System.out.println("Welcome");
        System.out.println("1. Username");
        System.out.println("2. Factorial");
        System.out.println("3. Area of triangle");
        System.out.println("4. Circumference of circle");
        System.out.println("5. Exit");

        //asks for user input
        do {
            System.out.println("Enter your first name");
            fName = sc.next();
            System.out.println("Enter your surname");
            sName = sc.next();
            System.out.println("Thank you. Now enter a selection (1-5):");
            choice = sc.nextInt();
            //menu loop

            switch (choice) {
                case 1:
                    **stringOperation(String fName, String sName);**
                    break;
                case 2:
                    numberFactorial();
                    break;
                case 3:
                    areaTriangle();
                    break;
                case 4:
                    circumferenceCircle();
                    break;
            }

        }while (choice!=5);
    }
**//stringOperation method
    private static void stringOperation(String f, String s) {
        String initial = f.substring(0,1);
        String username = initial   s;
        System.out.println("Your username is "   initial   s);
    }**

    public static void numberFactorial() { //method to calculate factorial of a number

        //variables
        int number;
        int factorial = 1;
        int i;
        //input
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter a number: ");
        number = sc.nextInt();
        //for loop
        for (i = 1; i <= number; i  ) {
            factorial = factorial * i;
        }
        System.out.println("Factorial of "   number   " is "   factorial);
    }


    public static void areaTriangle ()//method to calculate area of a triangle
    {   //input
        Scanner sc = new Scanner(System.in);

        //variables
        double width;
        double height;
        double area;
        //input
        System.out.println("Enter the width: ");
        width = sc.nextInt();
        System.out.println("Enter height: ");
        height = sc.nextInt();
        area = (height * width) / 2;
        System.out.println("The area is :"   area);
    }

    public static void circumferenceCircle ()//method to calculate circumference of a circle
    {   //variables

        double radius;
        double circumference;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter radius: ");
        radius = sc.nextDouble();
        circumference = Math.PI * 2 * radius;
        System.out.println("The circumference is : "   circumference);

    }
}

CodePudding user response:

If you want to make the method cleaner you could do something like this

private static void stringOperation(String f, String s) {
    System.out.println("Your username is "   f.substring(0,1)   s)
}

If you need to refer back to the new username then have a global variable that your method can set to refer to later like this.

private static void stringOperation(String f, String s) {
    Assessment.username = f.substring(0,1)   s;
    System.out.println("Your username is "   Assessment.username);
}

CodePudding user response:

I hope this is what you are looking for.

public class Answer {

    public static void main(String[] args) { //main method

        menu();  //call menu method

    }

    public static void menu() { //method to display menu options
        int choice;
        String firstName;
        String surName;

        Scanner sc = new Scanner(System.in);

        //displays menu options

        System.out.println("Welcome");
        
        System.out.println("1. Username");
        System.out.println("2. Factorial");
        System.out.println("3. Area of triangle");
        System.out.println("4. Circumference of circle");
        System.out.println("5. Exit");

        //asks for user input
        do {
            System.out.println("Enter your first name");
            firstName = sc.next();
            System.out.println("Enter your surname");
            surName = sc.next();
            System.out.println("Thank you. Now enter a selection (1-5):");
            choice = sc.nextInt();
            //menu loop

            switch (choice) {
                case 1:
                    stringOperation(firstName, surName);
                    break;
                case 2:
                    numberFactorial();
                    break;
                case 3:
                    areaTriangle();
                    break;
                case 4:
                    circumferenceCircle();
                    break;
            }


        }while (choice!=5);

        System.exit(0);
    }
    //stringOperation method
    private static void stringOperation(String firstName, String surName) {
        //3.1.1 Assign first character of first initial to variable using f.substring(start position, length of string).
        String initial = firstName.substring(0, firstName.length() - (firstName.length() - 1)); // used this because of 3.1.1
        String username = initial   surName;
        System.out.println("Your username is "   username);
    }

    public static void numberFactorial() { 
        int number;
        int factorial = 1;
        int i;
        //input
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter a number: ");
        number = sc.nextInt();
        //for loop
        for (i = 1; i <= number; i  ) {
            factorial = factorial * i;
        }
        System.out.println("Factorial of "   number   " is "   factorial);
        
    }


    public static void areaTriangle () {   //input
        Scanner sc = new Scanner(System.in);

        //variables
        double width;
        double height;
        double area;
        //input
        System.out.println("Enter the width: ");
        width = sc.nextInt();
        System.out.println("Enter height: ");
        height = sc.nextInt();
        area = (height * width) / 2;
        System.out.println("The area is :"   area);
    }

    public static void circumferenceCircle () {
        double radius;
        double circumference;

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter radius: ");
        radius = sc.nextDouble();
        circumference = Math.PI * 2 * radius;
        System.out.println("The circumference is : "   circumference);

    }
}
  •  Tags:  
  • Related