P
P
Pavel Kononov2021-12-08 22:43:31
Java
Pavel Kononov, 2021-12-08 22:43:31

How to pass console input to a constructor parameter in Java using Scanner?

There is a class Gamer.java and a String variable private String name.

How can I pass it to the constructor parameter so that when a new object of the Gamer class is created IN ANOTHER class, the name is entered from the console using Scanner?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Roo, 2021-12-08
@sfrtsp

In ANOTHER class, enter the name from the console using Scanner and pass it to the constructor of the Gamer class when creating its new object instance.

B
Bonce, 2021-12-08
@Bonce

class Gamer {
  private String name;

  public Gamer(String name) { this.name = name; }
}

class ДругойКласс {
  void method() {
    Scanner scanner = new Scanner(System.in);
    new Gamer(scanner.next());
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question