Search code examples
javaclassjava.util.scanneruser-inputprogram-entry-point

User Input from Class to Main Class in Java


I'm trying to create a program that (1) prompts for a user's full name and then generates a username; (2) prompts for a number and then determines if the number is odd or even. I wrote out the code for the username and odd/even classes and would like to call them from the main class. However, when called from the main class, the username method prompts the user twice before generating the username and the odd/even method doesn't actually determine if the number the user inputted is odd/even. When I remove the scanner object from the username class, I get a out of bounds compilation error so I'm forced to put it back in just so the program will run. Should I be using return statements?

Username

/**
 * Class to generate the username based on user's first name and randomly generated numbers
 */
public void username()
{
    Scanner inputReader = new Scanner(System.in);
    String fullName = inputReader.nextLine();
    // create random object and variable to store it in
    Random randomizer = new Random();
    int randomNumber = randomizer.nextInt(1000);
    // create variable to store lowercase username
    String lowercase = (fullName.toLowerCase());
    // create string variable to format username to first three characters in lowercase
    String firstThreeLetters = (lowercase.substring(0, 3));
    // concatenate lowercase characters and random number
    String usernameFinal = (firstThreeLetters + randomNumber);
    // print out final username
    System.out.println("Your username is " + usernameFinal);
}

Odd/even

/**
 * Class to determine if a user inputted value is odd or even
 */
public void OddEven1()
{
    Scanner inputReader = new Scanner(System.in);
    int userInteger = 0;
    // if/else to determine if number is odd or even
    if (userInteger % 2 == 0)
    {
        System.out.println(userInteger + " is an even number.");
    }
    else
    {
        System.out.println(userInteger + " is an odd number.");
    }
}

Main method

{
/**
 * This class holds the main method through which all other classes are run.
 */
public static void main(String[] args)
{
    // create objects
    Username usernameGenerator = new Username();
    OddEven oddeven = new OddEven();
    Scanner inputReader = new Scanner(System.in);

    // prompt for real name and print username
    System.out.print("Name: ");
    String fullName = inputReader.nextLine();
    usernameGenerator.username();

    // prompt for number
    System.out.print("Give me a number: ");
    // variable to store value
    int userInteger = inputReader.nextInt();
    oddeven.OddEven1();
}

Output: Here is my output


Solution

  • 1 - You request user's name twice, one in here

    String fullName = inputReader.nextLine();
    

    And one in here

    Scanner inputReader = new Scanner(System.in);
    String fullName = inputReader.nextLine();
    

    I would recommend keeping the first method and pass fullName to the username() function. As an example:

    /**
      * Class to generate the username based on user's first name and 
       randomly generated numbers
        */
       public void username(fullName)
       {
        // create random object and variable to store it in
        Random randomizer = new Random();
        int randomNumber = randomizer.nextInt(1000);
        // create variable to store lowercase username
        String lowercase = (fullName.toLowerCase());
        // create string variable to format username to first three characters in lowercase
        String firstThreeLetters = (lowercase.substring(0, 3));
        // concatenate lowercase characters and random number
        String usernameFinal = (firstThreeLetters + randomNumber);
        // print out final username
        System.out.println("Your username is " + usernameFinal);
    }
    

    2 - You do the same in the second function OddEven1() . I would recommend passing a parameter to it too. As an exmaple:

    public void OddEven1(number)
     {
       int userInteger = number;
       // if/else to determine if number is odd or even
        if (userInteger % 2 == 0)
         {
          System.out.println(userInteger + " is an even number.");
          }
        else
         {
           System.out.println(userInteger + " is an odd number.");
         }
     }
    

    3 - So your main function becomes:

    public static void main(String[] args)
    {
    // create objects
    Username usernameGenerator = new Username();
    OddEven oddeven = new OddEven();
    Scanner inputReader = new Scanner(System.in);
    
    // prompt for real name and print username
    System.out.print("Name: ");
    String fullName = inputReader.nextLine();
    usernameGenerator.username(fullName);
    
    // prompt for number
    System.out.print("Give me a number: ");
    // variable to store value
    int userInteger = inputReader.nextInt();
    oddeven.OddEven1(userInteger);
    }