Another example of input import java io import

  • Slides: 8
Download presentation
Another example of input import java. io. *; //import java. lang. Integer. *; class

Another example of input import java. io. *; //import java. lang. Integer. *; class Exampleof. Input 2 { private static Buffered. Reader stdin = new Buffered. Reader( new Input. Stream. Reader( System. in ) ); public static void main ( String [] args ) throws IOException { System. out. println("Pls give integer input"); int n = Integer. value. Of(stdin. read. Line()). int. Value(); System. out. println("Your input was "+n+'n'); System. out. println("Pls give float input"); double x = Double. value. Of(stdin. read. Line()). double. Value(); System. out. println("Your input was "+x+'n'); System. out. println("Pls give character input"); char y = (char)stdin. read(); // Use control-D to terminate the input stream System. out. println("Your input was "+y+'n'); System. out. println("Pls give string input"); String z = stdin. read. Line(); // Use control-D to terminate the input stream System. out. println("Your input was "+z+'n'); } }

Using System. in class Count { public static void main(String args[]) throws java. io.

Using System. in class Count { public static void main(String args[]) throws java. io. IOException { int count = 0; while (System. in. read() != -1) { count++; } System. out. println(“Input has ” + count + “ chars. ”); } } // Use control-D to terminate the input stream

Reading a line class Line. Input { public static void main(String args[]) throws java.

Reading a line class Line. Input { public static void main(String args[]) throws java. io. IOException { char line[] = new char[100]; int count = 0, i; while ((line[count++]=(char)System. in. read()) != ‘n’); System. out. println(“Input line was: ”); for (i=0; i<count; i++) { System. out. print(line[i]); } } }

Revisit Java operator shortcircuit class shortcircuit { public static void main(String args[]) { int

Revisit Java operator shortcircuit class shortcircuit { public static void main(String args[]) { int n, d, q; n = 10; d = 2; if(d != 0 && (n % d) == 0) System. out. println(d + " is a factor of " + n); d = 0; // now, set d to zero // Since d is zero, the second operand is not evaluated. if(d != 0 && (n % d) == 0) System. out. println(d + " is a factor of " + n); /* Now, try same thing without short-circuit operator. This will cause a divide-by-zero error. */ if(d != 0 & (n % d) == 0) System. out. println(d + " is a factor of " + n); } }

class sideeffect { public static void main(String args[]) { int i; i = 0;

class sideeffect { public static void main(String args[]) { int i; i = 0; /* Here, i is still incremented even though the if statement fails. */ if(false & (++i < 100)) System. out. println("this won't be displayed"); System. out. println("if statements executed: " + i); // displays 1 /* In this case, i is not incremented because the short-circuit operator skips the increment. */ if(false && (++i < 100)) System. out. println("this won't be displayed"); System. out. println("if statements executed: " + i); // still 1 !! } }

Escape Sequence ’ Single quote ” Double quote \ Backslash r Carriage return n

Escape Sequence ’ Single quote ” Double quote \ Backslash r Carriage return n New Line f Form feed t Horizontal tab b backspace ddd Octal constant (where ddd is an octal constant) uxxxx Hexadecimalconstant (where xxxx is a hexadecimal constant)

literals /* Write comments here */ class literal { public static void main (String

literals /* Write comments here */ class literal { public static void main (String args[]) { System. out. println("tantb bbc"); int x = '130'; int y = 'u 0059'; System. out. println(x+" int xy = 0130; int yx = 0 x 59; System. out. println(xy+" char xx = '130'; char yy = 'u 0059'; System. out. println(xx+" } } "+y); "+yx); "+yy);

Escape sequence example class escseq { public static void main(String args[]) { System. out.

Escape sequence example class escseq { public static void main(String args[]) { System. out. println("First linen. Second line"); System. out. println("At. Bt. C"); System. out. println("Dt. Et. F"); } }