Reputation: 3
im trying to learn Java, meaning that my experience and knowledge is almost none, and i hope you can help me with this:
(The thought i had was like how you can name yourself in videogames, with the option to display the name in future instances without any deeper meaning later on)
import java.util.Scanner;
public class Playername {
public static void main(String[] args) {
public String getName();
Scanner Charname = new Scanner (System.in);
System.out.print ("What is your name?");
Charname.nextLine();
String getName = Charname;
System.out.print (Charname);
}
}
i probably messed this up, reading only the early chapters of a book about java and then trying to use commands and things in way's they were not supposed to be used. (i wanted to save the name of the scanner and then copypaste it to the String variable)
thank you
PS: The error i get is "cannot convert from scanner to string", basically stating that my question would be how the idea i had can, in the most simplistic way possble, be realised.
Upvotes: 0
Views: 14755
Reputation:
Welcome to StackOverflow!
Firstly, I'd like to start you off with some code conventions.
These help others read your code, since even though you may know what is going on; we may not.
Now, for your question.
Scanner scannerName = new Scanner (InputStream inputStream);
This line creates a Scanner object with the InputStream coming from the an input stream, and we name it scannerName. Generally, you want to label your objects what they are, or some abbreviation of that.
Scanner scanner1 = new Scanner(System.in);
This is sufficient for our purposes. Since we want to get information from the System.in inputstream, or console, we use System.in as our InputStream.
Now, this scanner object that we've created can receive input from the console. It does this through the use of methods like
scanner1.nextLine();
The above piece of code returns a String value. Alone, it's pretty useless. So we'll assign a String object to take and store that value.
String characterName = scanner1.nextLine();
What this does is it sets the String's value to be equivalent to the value of the scanner1's inputstream, after hitting enter.
So if a user enters "Johnathan Nathan", and then presses Enter,the String named characterName will be set to "Johnathan Nathan".
A full working example is:
Scanner scanner1 = new Scanner(System.in);
System.out.println("Please enter your name: ");
String characterName = scanner1.nextLine();
System.out.println("Hello, " + characterName());
This would ask for the person's name, then say hello to them directly after.
Finally, if you have any questions regarding how a class is used, you can always look up the Javadoc of the class you're having problems with.
Upvotes: 1
Reputation: 1385
You are trying to assign Charname
which is a Scanner
to a String
What you want to do is to assign Charname.nextLine()
which is a String
to your variable
Scanner Charname = new Scanner (System.in);
System.out.print ("What is your name?");
String getName = Charname.nextLine();
System.out.print(getName);
Upvotes: 1
Reputation: 4411
Try this
Scanner Charname = new Scanner (System.in);
System.out.print ("What is your name?");
String input = Charname.nextLine();
String getName = input;
System.out.print (input);
You cannot assign scanner object directly to string reference.
Upvotes: 1