java - One of my Inputs is skipped -
i have assignment need use bufferedreader, system.in.read() thrown exception , system.in.read() catch , try. have far.
import java.io.*; public class test { public void bufferedreader() throws ioexception { bufferedreader in = new bufferedreader (new inputstreamreader(system.in)); string expr = new string(); system.out.print("input: "); expr = in.readline(); system.out.println("output: " + expr); system.out.println("------------------------------------------"); } public void throwexception() throws ioexception { int eingabe=20; system.out.print("\ninput"); eingabe=system.in.read(); system.out.println("\n" + (char)eingabe); system.out.println("------------------------------------------"); } public void exception() { int i=32; system.out.print("\neingabe "); try { i=system.in.read(); } catch (java.io.ioexception e) { system.out.println("eingabefehler "+ e); } system.out.println("\n" + (char)i); system.out.println("------------------------------------------"); } public static void main(string[] args) throws exception { test method = new test(); method.bufferedreader(); method.exception(); method.throwexception(); } }
bufferedreader works fine , 1 of system.in.read() inputs third input skipped. both system.in.read work separately cannot both system.in.read() work together.
here's output looks like:
input: 5 output: 5 ------------------------------------------ input 5 5 ------------------------------------------ input -----------
thanks in advance
system.in.read() reads next byte of data input stream.
so, when enter second input in method.exception(), there 2 characters (three if you're using windows) in input stream. second character (possibly newline character) read third (last) read() , program ends.
try overloaded version of read() method: read(byte[] b) reads number of bytes input stream , stores them buffer array b
Comments
Post a Comment