0

hi i don't have my book with me and i can't remember what is the code for let's say, the console asks "please enter your name" what would be the code for wanting to take what they type and assign a value like name = "whatever they type"

system.out.println("Please enter your name")

what would the code be to go after that?
sorry i'm very new to all of this

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by cbarton.a
0

eikal,

You will want to instantiate a String to store the name you want to store the name in, after that do you will use your print code, then read it in using a Scanner's nextLine() method.

import java.util.Scanner;

public class DaniProject{
	public static void main(String [] args){
		String name; // String to store the name inputted.
		Scanner input = new Scanner(System.in); // Scanner object to store the InputStream of the Prompt.
		
		System.out.println("Enter your name:");
		name = input.nextLine().trim(); // Store whatever the user entered into the name String without 
					// trailing whitespace.
	}
}

Welcome, any further questions, just PM me,

cbarton.a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.