简体   繁体   中英

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: 这是我的输出

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
}

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM