0

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

1
  • you have to basically read the input in main or in your custom classes you dont need it twice.. Commented Dec 16, 2017 at 18:01

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! Just to clarify, whenever I want to use user input from the main class in another class, I have to use parameters?
It is a convenient way to make main class handles user inputs and pass them to functions. However, you can do whatever you want. For myself, I prefer passing arguments to the functions. Also, I think you need to revise some of Object Oriented Programming concepts, so that you understand things more. Good Luck!
1

You should change your code like below

inside main method

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

Odd/even

public void OddEven1(int userInteger )
{

    // 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.");
    }
}

Now lets discuss about username. You have already captured the username from your main method. So you dont need to get it from user again.

String fullName = inputReader.nextLine();
    usernameGenerator.username(fullName );

public void username(String fullName )
{
//Your logic
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.