How To Take Input From Keyboard In Java Using Bufferedreader
How to take input from keyboard in java using bufferedreader - Java Mendapatkan Keyboard Input atau Java Getting Input From Keyboard Using BufferedReader , Mengatasi Input data yang terlewatkan [SOLVED] dan Untuk JOptionPane Method ke sini.
Class BufferedReader
java.lang.Object java.io.Reader java.io.BufferedReader
Source Code
Memasukan Input di Command Line Java Menggunakan BufferedReader
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Pertemuan2; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; /** * * @author Scodeid */ public class GetInputFromKeyboard { public static void main(String[] args) { String nama = ""; BufferedReader dataIn; dataIn = new BufferedReader (new InputStreamReader(System.in)); System.out.print("Please enter your name = "); try { nama = dataIn.readLine(); } catch(IOException e) { System.out.println("Error!"); } System.out.println("Hello " +nama +" !"); } }
BUG ketika memakai read() dan readLine() Java Input yang terlewatkan
BufferedReader yogiIn; yogiIn = new BufferedReader(new InputStreamReader(System.in)); try { System.out.println("Masukan Nim : "); nim = yogiIn.read(); // nim = Integer.parseInt(input.nextLine()); System.out.println("Masukan Nama : "); nama = yogiIn.readLine(); .................. ...................................... .............................................. ...................................................
bergotong-royong ini bukanlah sebuah bug, ketika perintah atau command read di baca lalu dilanjutkan readLine, Maka masukan (input) sebelumnya akan di lewatkan, alasannya ialah readLine adalah
( \n \r \t ) Enter Baris Line type String sedangkan read sebaliknya dan untuk int, selengkapnya sanggup kalian baca ketika menulis read akan muncul rpc jadwal klarifikasi di netbeans IDE.
Disini saya memecahkan problem tersebut dengan embel-embel java utilitas dan mem parse integer nya.
java.util
Class Scanner
- java.lang.Object
- java.util.Scanner
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package LatihanTextPertemuan2; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; /** * * @author Scodeid */ public class LatihanInput1 { public static Scanner input = new Scanner(System.in); public static void main(String[] args) throws IOException { int nim = 0; // String nim =""; String nama =""; String jurusan = ""; int usia = 0; // String usia =""; int tambah = 0; BufferedReader yogiIn; yogiIn = new BufferedReader(new InputStreamReader(System.in)); try { System.out.println("Masukan Nim : "); // nim = yogiIn.readLine(); nim = Integer.parseInt(input.nextLine()); System.out.println("Masukan Nama : "); nama = yogiIn.readLine(); System.out.println("Masukan Jurusan : "); jurusan = yogiIn.readLine(); System.out.println("Masukan Usia : "); usia = Integer.parseInt(input.nextLine()); // tambah = Integer.parseInt(usia)+3; tambah = usia+=3; } catch(IOException e) { System.out.println("Error!"); } System.out.println("NIM : " +nim); System.out.println("NAMA : " +nama); System.out.println("JURUSAN : " +jurusan); System.out.println("USIA : " +usia); System.out.println("LULUS : " +tambah); } // public static void main(String[] args) { // // } }
catatan
Penjelasan BufferedReader Java
Method Summary - Ringkasan Metode | |
---|---|
void | close() Closes the stream and releases any system resources associated with it. Menutup ajaran dan merilis sumber daya sistem apa pun yang terkait dengannya. |
void | mark(int readAheadLimit) Marks the present position in the stream. Tandai posisi ketika ini di arus. |
boolean | markSupported() Tells whether this stream supports the mark() operation, which it does. Memberi tahu apakah ajaran ini mendukung operasi mark (), yang dilakukannya. |
int | read() Reads a single character. Membaca satu karakter |
int | read(char[] cbuf, int off, int len) Reads characters into a portion of an array. Membaca aksara ke dalam porsi array. |
String | readLine() Reads a line of text. Membaca baris teks. |
boolean | ready() Tells whether this stream is ready to be read. Memberi tahu apakah ajaran ini siap dibaca. |
void | reset() Resets the stream to the most recent mark. Setel ulang ajaran ke tanda terbaru. |
long | skip(long n) Skips characters. Melewatkan karakter. |
java.io
Class BufferedReader
java.lang.Object java.io.Reader java.io.BufferedReader
Methods inherited from class java.io.Reader |
---|
read, read |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Selengkapnya ada di https://docs.oracle.com/javase/6/docs/api/java/io/BufferedReader.html Sumber https://scodeid.blogspot.com/