2

I'm trying to write a program that asks the user for input using the scanner class to name something. Then in a completely different class, reference that input.

For example:

class TeamInfo {
    Scanner nScan = new Scanner(System.in);

public String GetName(){
    String GetName = nScan.nextLine();

}

The issue I'm having is that the first time I reference the GetName method in the TeamInfo class, it works--At the right spot, it prompts for the team name.

However, it prompts for the team name every time after that. I can't seem to find anywhere online or in my Java Beginner's Guide how to make this input constant, so that I can reference the input. I'm also not entirely sure what it is I'm looking for, so that doesn't help.

In other words, what I want is to prompt the user one time, and then remember that answer and re-use it again and again.

2
  • 3
    I only see a single, incomplete class above. Commented Jan 29, 2014 at 19:57
  • Yeah, you're correct -- I was referring to the main method, not a class. I got it figured out though. Commented Feb 25, 2014 at 7:17

3 Answers 3

6

You should make two methods: getName() and promptName() (or whatever names you like best)

One method would be for retrieving the name from the user, and the other would be for retrieving the value that you got from the user:

class TeamInfo {
    private Scanner nScan = new Scanner(System.in);
    private String name;

    public void promptName() {
        name = nScan.nextLine();
    }

    public String getName() {
        return name;
    }
}

When you want to get the name from the user, you'd call:

TeamInfo info = new TeamInfo();
info.promptName();

And when you wanted to retrieve the name for your uses:

String teamName = info.getName();
Sign up to request clarification or add additional context in comments.

Comments

4

Save the result of the input to a field, then return that on request:

class TeamInfo {

    private String name;
    Scanner nScan = new Scanner(System.in);

    public void promptForName() {
        System.out.print("Name: ");
        this.name = nScan.nextLine();
    }

    public String getName() {
        return this.name;
    }
}

Comments

3

You need to store the name you have read fro the Scanner in a class member, then return that class member when calling GetName (). Otherwise you will lose the name you have read, and will have to read it again (and thus prompt the user again for input).

For example:

class TeamInfo {
    Scanner nScan = new Scanner(System.in);
    String name = null;

public String GetName(){
    if (name == null) {
        name = nScan.nextLine();
    }

    return name;

}

On a different note, you should read on Java naming conventions. A method name should not start with a capital letter.

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.