简体   繁体   中英

How to use the Scanner in a separate method without calling it multiple times

what the program does: The program I'm trying to create takes a name a user inputs, and finds how many characters they type, uses that information to generate a random number that acts as an ID, and then the program stores the name the user inputted earlier and the random number it generated inside of two separate arrays, and then prints out the array.

What i'm trying to do: Instead of using the scanner class in the main, I'm using it in it's own method that method is called personName() .

The problem that I am facing: the problem am i facing is when I run the program I have to type the name twice, if I dont it throws me an exception.

the cause of the problem: I know what's causing the problem, I just dont know how to fix it. The problem arise when I call personName() twice in two different methods, but I need to call it twice so i can get the data from it. The first time i called it is in the id() method. I did this so i can get how many characters the name has. The second time i called it when it's in the yourArrays() method. I did this because I needed to store the name inside of an array. I called it twice by using the variable String yourName = personName();inside the id() method and I called it again using the variable String name = personName(); inside the yourArrays(); method.

what have I tried: I tried to close the scanner but it still just throws the exception since it's still getting called twice, and since it is closed there's no way to get the data anyway.

What answer am I looking for: The answer I want is how do I stop the system from asking the user to input the name twice, or is there a way to get the name the user inputted without calling the method?

the code:

import java.util.*;
public class IdMaker {

    public static void main(String[] args) {
        System.out.println("Hello welcome to our new system of generating your ID!");
        System.out.print("Please enter your name: ");
        yourArrays(); //outputs the arrays
    }
    
    
    public static int randomNumber(int a) { //the method that generates the random number
        
        Random rand = new Random();
        int ID = rand.nextInt(a)+1000;
        
        return ID;
        
        
    }
    
    public static String personName() { //the method that gets and stores the user's name
        Scanner name = new Scanner(System.in);
        String yourName = name.nextLine();
        
        return yourName;
        
        
        
    }
    
    public static int id() { //the method that takes the user name, computes the length of the characters of the name to use for the number generator
        String yourName = personName(); //personName() called once
        int length = yourName.length();
        int id = randomNumber(length);
        
        return id;
        
    }
    public static void yourArrays() {// the method that takes the ID and the name and stores it into the arrays
        String name = personName(); //personName() called twice
        int yourId = id();
        String[] nameArray = {name};
        Integer[] idArray = {yourId};
        
        for(int i = 0; i < nameArray.length; i++) {
            System.out.println("the names are: " + nameArray[i] + " ");
        
        }for(int i = 0; i < idArray.length; i++) {
            System.out.println("the IDs are: " + idArray[i] + " ");
        
        }
        
        
    }

}

the output: this is how it looks when the user have to enter it twice

Hello welcome to our new system of generating your ID!
Please enter your name: chris
chris
the names are: chris 
the IDs are: 1001  

the output if you dont enter the name twice

Hello welcome to our new system of generating your ID!
Please enter your name: chris

Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
    at java.util.Random.nextInt(Random.java:388)
    at IdMaker.randomNumber(IdMaker.java:14)
    at IdMaker.id(IdMaker.java:34)
    at IdMaker.yourArrays(IdMaker.java:41)
    at IdMaker.main(IdMaker.java:7)

Rather than calling personName() again, which will cause the user to be asked for input again, id should accept a name as a parameter, and use that generate an ID:

public static int id(String name) {
    int length = name.length();
    int id = randomNumber(length);
    
    return id;
    
}

You can then pass the name returned by yourName to id in the yourArrays method:

public static void yourArrays() {
    String name = personName();
    int yourId = id(name);
    ...

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