BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

How to take String Input in Java

Last Updated: June 9, 2024 by Chaitanya Singh | Filed Under: java

In this tutorial, we will learn how to take String input in Java. There are two ways you can take string as an input from user, using Scanner class and using BufferedReader. However most common way is using Scanner class. Let’s see programs of each of these approaches:

1. String Input using Scanner Class

In this example, we are using nextLine() method of Scanner class to read the user input. This way we can read the entire line entered by user. Don’t forget to import java.util.Scanner package to use Scanner class.

import java.util.Scanner;

public class StringInputExample {
public static void main(String[] args) {
// Create a Scanner object
Scanner scanner = new Scanner(System.in);

// Prompt user for string input
System.out.println("Enter a string:");

// Read the user input using nextLine() method
String input = scanner.nextLine();

// Print the input string
System.out.println("User entered: " + input);

// Close the Scanner object after use
scanner.close();
}
}

Notes:

  • Scanner: The Scanner class is part of the java.util package and it contains various methods that can be used to read user inputs.
  • nextLine(): This method of Scanner class reads the entire line entered by user including spaces until the end of the line.
  • Closing the Scanner: The close() method of Scanner class is used to close the Scanner after use to prevent resource leaks. Although, the program runs fine without using this method, it is still recommended to use this as part of best practices.

2. String Input using BufferedReader

Another way to read String input in Java is by using BufferedReader. This is less common but still useful in some cases.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class StringInputExample {
public static void main(String[] args) {
// Create a BufferedReader object
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

// Prompt user for string input
System.out.println("Enter a string:");

try {
// Read user input
String input = reader.readLine();

// print user entered string
System.out.println("You entered: " + input);
} catch (IOException e) {
e.printStackTrace();
}
}
}

Top Related Articles:

  1. When to use ArrayList and LinkedList in Java
  2. Java Switch with Strings
  3. How to read file in Java – BufferedInputStream
  4. Remove special characters from a String in Java
  5. Convert Comma Separated String to HashSet in Java

Tags: Java-Strings

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap