Hydlide
Hydlide

Reputation: 363

Passing/storing info from a class?

I'm a beginner with Java and I'm making a simple text RPG and I want to store character information in a class such as Name, Job, Age, etc. For example:

class myChar
{
    String name = "";
    String job = "";
    int age = 0;
    //set, get, etc...
}

And I want the user to be able to be asked what their name, job, etc. is and pass it back into the class to that it can be pulled up later on. I understand how to do this with ints quite well, for example I have a Stats class where the HP, attack power, etc. is stored:

class Stats // Demonstrates Class
{
    int hp = 10;
    //set, get, etc...
    public int hit() // Player is hit
    {
        hp --;
        return hp;
    }
}

So as you see I have a small grasp on it but I'm not quite sure how to get it so the player can enter this information and have it be stored. I understand this usage:

Stats myStats = new Stats("John", "Warrior", 30);

When it comes to identifying the info, but not how to let the player choose it for themselves. I'm sorry if I sound too vague, I'll update my post as needed!

Edit: I have a main public class which contains the different "rooms" and such. It's pretty lengthy, and I know how to get user input. For instance, I have a command window which allows the user to "look", "use", "get", etc. So I understand user input quite well. I just don't get how to have the user enter info and store this info in an outside class to be called upon later.

Upvotes: 4

Views: 103

Answers (4)

Manuel Reis
Manuel Reis

Reputation: 574

As I understand, you want to have some kind of user input, and then pass this information to the class.

You could do something like this:

import java.io.Reader;

//Receive input from System.in - console
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

System.out.print("Enter the character's name!");
String name = br.readLine(); //Read line from input

System.out.print("Enter the job you want!");
String job = br.readLine();

System.out.print("What's your character's age?");
int age = Integer.parseInt(br.readLine());

myChar character = new myChar(name, type, age);

Upvotes: 3

Steve P.
Steve P.

Reputation: 14709

To write to the user, you're writing to Standard Output. You can do this simply by using: System.out.println(yourString);

You can have this:

public class Stats
{
  private static Scanner in = new Scanner(System.in);//need to import java.util.Scanner;
  private String name;
  private String occupation;
  private int age;

  public Stats()
  {   //NB: you should error check, this is just a simple example
      System.out.println("Enter name");
      name=in.nextLine();
      System.out.println("Enter occupation");
      occupation=in.nextLine();
      System.out.println("Enter age");
      age=in.nextInt();
  }
}

Upvotes: 1

Benjamin
Benjamin

Reputation: 1

This is a classic problem of reading user input. A good place to start is learning how to use the console and the Scanner class to get information in the form of a String. Don't forget you will need a main method to run your program. Here is one starting point.

http://www.java-made-easy.com/java-scanner.html

Upvotes: 0

pcalcao
pcalcao

Reputation: 15988

If I understand correctly, you're having problems getting input from the user at the start of your program. You can use the Console class for that:

Console console = System.console();
String input = console.readLine("Enter input:");

Or, for instance, the Scanner class:

Scanner reader = new Scanner(System.in);
System.out.println("Enter the first number");
//get user input for a
String input = reader.nextLine();

The Scanner class is quite flexible when it comes to getting input in different forms, you can read the docs here: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Upvotes: 1

Related Questions