Download - Another example of input
Another example of inputimport 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.inclass 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 lineclass 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 shortcircuitclass 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 !! }}
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 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 exampleclass 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");
}
}