80 likes | 269 Views
Another example of input. import java.io.*; //import java.lang.Integer.*; class ExampleofInput2 { private static BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );
E N D
Another example of input import java.io.*; //import java.lang.Integer.*; class ExampleofInput2 { private static BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) ); public static void main ( String [] args ) throws IOException { System.out.println("Pls give integer input"); int n = Integer.valueOf(stdin.readLine()).intValue(); System.out.println("Your input was "+n+'\n'); System.out.println("Pls give float input"); double x = Double.valueOf(stdin.readLine()).doubleValue(); 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.readLine(); // 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.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 LineInput { 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 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; /* 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 !! } }
literals /* Write comments here */ class literal { public static void main (String args[]) { System.out.println("\ta\n\tb \b\bc"); int x = '\130'; int y = '\u0059'; System.out.println(x+" "+y); int xy = 0130; int yx = 0x59; System.out.println(xy+" "+yx); char xx = '\130'; char yy = '\u0059'; System.out.println(xx+" "+yy); } }
Escape sequence example class escseq { public static void main(String args[]) { System.out.println("First line\nSecond line"); System.out.println("A\tB\tC"); System.out.println("D\tE\tF"); } }