It is a simple demonstration to show how to take a string as an input from the user.
The only thing you need to be concerned about is the data type that you declare in the program that is the String data type as shown below in the program.
How to take user Input as String in java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //user String input in java import java.util.Scanner; public class UserInput { public static void main(String []args) { String str; Scanner in = new Scanner(System.in); //Get input String System.out.println("Enter a string: "); str = in.nextLine(); System.out.println("Entered String is: "+str); } } |
Output:
Enter a string:
I am Coding right now.
Entered String is: I am Coding right now.