TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java Tutorials

Java Program to read a character from the keyboard

Overview

To understand the keyboard interaction in Java language, let’s write and understand a simple program to read a character from the standard input device i.e keyboard.

The stream objects defined in the System class are outlined here:

https://www.testingdocs.com/keyboard-interaction-in-java/

Program

import java.io.IOException;

/*
* Program to read a single character from the keyboard
* Java Tutorials – www.TestingDocs.com
*/

public class ReadCharacter {

public static void main(String[] args) throws IOException {
System.out.println(“Enter a character from keyboard ::”);
char ch = (char) System.in.read();
System.out.println(“The character entered from the keyboard = ” + ch);
}
}

 

Program Output

Sample program output:

Enter a character from keyboard ::
q
The character entered from the keyboard = q

 

Read Single Character Java

 

The read() method throws IOException. We need to handle the exception type IOException in the program. There are two things to fix the error. We can add the throws declaration clause to the method signature. Handle the code by surrounding it with a try/catch block.

Unhandled IOException Java Fix

 

IOException is a subclass of Exception and is a checked exception. It signals that an I/O exception has occurred. This exception is produced by failed or interrupted I/O operations.

—

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Related Posts

Download Greenfoot Windows

Java Tutorials /

Download & Install Greenfoot on Windows

Java Tutorials /

Java Static Code Analysis

Java Tutorials /

Java Testing Tools

Java Tutorials /

Handle Multiple Exceptions in Java

Exceptions_In_Java

Java Tutorials /

Exceptions in Java Programs

‹ Java Method Overriding› Java Arrays

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version