Java Read in a Character String From Keyboard
How many means tin can we read data from the keyboard in Java?
The java.io package provides various classes to read write information from various sources and destinations.
Yous tin read information from user (keyboard) using various classes such as, Scanner, BufferedReader, InputStreamReader, Panel etc.
Using Scanner course
From Java 1.5 Scanner form was introduced. This class accepts a File, InputStream, Path and, String objects, reads all the primitive data types and Strings (from the given source) token by token using regular expressions. By default, whitespace is considered as the delimiter (to break the data into tokens).
To read data from keyboard you lot need to utilize standard input equally source (System.in). For each datatype a nextXXX() is provided namely, nextInt(), nextShort(), nextFloat(), nextLong(), nextBigDecimal(), nextBigInteger(), nextLong(), nextShort(), nextDouble(), nextByte(), nextFloat(), side by side().
Case
Following Coffee program reads data from user using the Scanner class.
import java.util.Scanner; class Pupil{ String name; int age; float percent; boolean isLocal; char grade; Student(String name, int historic period, float per centum, boolean isLocal, char grade){ this.proper name = name; this.historic period = age; this.percentage = per centum; this.isLocal = isLocal; this.course = grade; } public void displayDetails(){ System.out.println("Details.............."); Organization.out.println("Name: "+this.proper name); System.out.println("Age: "+this.historic period); System.out.println("Percent: "+this.percent); if(this.isLocal) { Organization.out.println("Nationality: Indian"); }else { System.out.println("Nationality: Foreigner"); } Organisation.out.println("Grade: "+this.form); } } public class ReadData { public static void master(String args[]) { Scanner sc =new Scanner(System.in); System.out.println("Enter your name: "); String name = sc.next(); System.out.println("Enter your age: "); int historic period = sc.nextInt(); System.out.println("Enter your percentage: "); bladder percentage = sc.nextFloat(); Organization.out.println("Are you local (enter true or false): "); boolean isLocal = sc.nextBoolean(); System.out.println("Enter your grade(enter A, or, B or, C or, D): "); char grade = sc.next().toCharArray()[0]; Student std = new Educatee(proper noun, age, percent, isLocal, grade); std.displayDetails(); } }
Output
Enter your name: Krishna Enter your age: 25 Enter your percent: 86 Are you local (enter true or false): true Enter your form(enter A, or, B or, C or, D): A Details.............. Name: Krishna Age: 25 Percent: 86.0 Nationality: Indian Grade: A
Using BufferedReader
The BufferedReader class of Java is used to read stream of characters from the specified source (character-input stream). The constructor of this class accepts an InputStream object every bit a parameter, you lot can to laissez passer an InputStreamReader.
Example
Following Java program reads data from user using the BufferedReaderclass.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; course Student{ Cord proper noun; int age; float percent; boolean isLocal; char grade; Student(Cord name, int age, float percent, boolean isLocal, char grade){ this.proper name = proper name; this.age = age; this.percent = per centum; this.isLocal = isLocal; this.grade = grade; } public void displayDetails(){ System.out.println("Details.............."); System.out.println("Name: "+this.name); Organization.out.println("Age: "+this.age); System.out.println("Percent: "+this.percent); if(this.isLocal) { System.out.println("Nationality: Indian"); }else { System.out.println("Nationality: Greenhorn"); } System.out.println("Grade: "+this.grade); } } public class ReadData { public static void chief(Cord args[]) throws IOException { BufferedReader reader =new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter your name: "); String name = reader.readLine(); System.out.println("Enter your age: "); int age = Integer.parseInt(reader.readLine()); System.out.println("Enter your percentage: "); float percentage = Float.parseFloat(reader.readLine()); System.out.println("Are yous local (enter true or false): "); boolean isLocal = Boolean.parseBoolean(reader.readLine()); System.out.println("Enter your grade(enter A, or, B or, C or, D): "); char grade = (char) reader.read(); Student std = new Student(proper name, age, percent, isLocal, grade); std.displayDetails(); } }
Output
Enter your name: Krishna Enter your age: 25 Enter your pct: 86 Are you local (enter true or simulated): truthful Enter your grade(enter A, or, B or, C or, D): A Details.............. Name: Krishna Historic period: 25 Percent: 86.0 Nationality: Indian Grade: A
Using Console class
This form is used to write/read information from the console (keyboard/screen) devices. It provides a readLine()method which reads a line from the key-board. You lot can go an object of the Console class using the console() method.
Note − If y'all try to execute this program in a non-interactive environment like IDE it doesn't piece of work.
Example
Following Coffee program reads data from user using the Panelgrade.
import coffee.io.BufferedReader; import java.io.Console; import java.io.IOException; import java.io.InputStreamReader; class Student{ String proper noun; int historic period; float per centum; boolean isLocal; char grade; Student(Cord name, int age, float percent, boolean isLocal, char grade){ this.proper noun = name; this.age = historic period; this.percent = percentage; this.isLocal = isLocal; this.course = form; } public void displayDetails(){ System.out.println("Details.............."); System.out.println("Name: "+this.proper name); Arrangement.out.println("Age: "+this.age); Organisation.out.println("Percent: "+this.percent); if(this.isLocal) { Arrangement.out.println("Nationality: Indian"); }else { System.out.println("Nationality: Foreigner"); } System.out.println("Grade: "+this.grade); } } public class ReadData { public static void main(String args[]) throws IOException { Console console = System.console(); if (console == cipher) { System.out.println("Panel is not supported"); System.exit(1); } Arrangement.out.println("Enter your name: "); String name = panel.readLine(); System.out.println("Enter your historic period: "); int age = Integer.parseInt(console.readLine()); Organisation.out.println("Enter your percent: "); float per centum = Float.parseFloat(console.readLine()); System.out.println("Are yous local (enter true or false): "); boolean isLocal = Boolean.parseBoolean(panel.readLine()); System.out.println("Enter your grade(enter A, or, B or, C or, D): "); char form = console.readLine().toCharArray()[0]; Student std = new Student(name, age, pct, isLocal, grade); std.displayDetails(); } }
Output
Enter your name: Krishna Enter your age: 26 Enter your per centum: 86 Are you lot local (enter true or false): true Enter your grade(enter A, or, B or, C or, D): A Details.............. Proper noun: Krishna Age: 26 Percent: 86.0 Nationality: Indian Grade: A
Published on 01-Aug-2019 xiv:27:57
- Related Questions & Answers
- In how many ways we tin can concatenate Strings in Java?
- How can we read from standard input in Java?
- In how many ways tin nosotros split a string in JavaScript?
- How can we read data from an excel sheet in Selenium Webdriver?
- Ways to read input from console in Coffee
- Program to discover how many ways we can climb stairs in Python
- In how many ways nosotros can convert a String to a character array using Java?
- Tin nosotros read from JOptionPane past requesting input from user in Coffee?
- What are JSP declarations? In how many ways we tin write JSP declarations?
- How many means a Cord object can be created in java?
- How many ways can get the instance of a Class course in Java?
- How to read the data from a file in Java?
- How can we read a JSON file in Java?
- In how many ways can we detect a substring within a string in javascript?
- In how many ways you can retrieve the elements of a collection in Java?
Source: https://www.tutorialspoint.com/how-many-ways-can-we-read-data-from-the-keyboard-in-java
0 Response to "Java Read in a Character String From Keyboard"
Postar um comentário