Transferring / saving information from a class?

I start with Java, and I make a simple text RPG, and I want to store character information in a class like Name, Job, Age, etc. For instance:

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

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

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

So, as you can see, I understand this a little, but I'm not quite sure how to do this so that the player can enter this information and save it. I understand this use:

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

When it comes to identifying information, but not to allow the player to choose it for themselves. Sorry if I speak too vaguely, I update my post as necessary!

Edit: I have a main public class that contains different "rooms", etc. It is quite long and I know how to get user input. For example, I have a command window that allows the user to “watch,” “use,” “receive,” etc. Therefore, I understand user input well. I just don’t understand how the user enters information and stores this information in an external class that will be called later.

+4
source share
4 answers

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

You can 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 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 your character age?"); int age = Integer.parseInt(br.readLine()); myChar character = new myChar(name, type, age); 
+3
source

If I understand correctly, you are having problems entering user information at the beginning of your program. You can use the Console class to do this:

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

Or, for example, 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 inputting input in different forms, you can read the documents here: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

+1
source

To write to the user, you write to standard output. You can do this simply by using: System.out.println(yourString);

You may have the following:

 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(); } } 
+1
source

This is the classic problem of reading user input. A good place to start is to learn how to use the console and the Scanner class to get information as a string. Do not forget that you need a basic method to run your program. Here is one starting point.

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

0
source

Source: https://habr.com/ru/post/1480387/


All Articles