another example of input

8
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'); } }

Upload: theola

Post on 04-Jan-2016

36 views

Category:

Documents


0 download

DESCRIPTION

Another example of input. import java.io.*; //import java.lang.Integer.*; class ExampleofInput2 { private static BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) ); - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 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');

} }

Page 2: Another example of input

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

Page 3: Another example of input

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]); } }}

Page 4: Another example of input

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); }}

Page 5: Another example of input

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 !! }}

Page 6: Another example of input

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)

Page 7: Another example of input

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); }}

Page 8: Another example of input

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");

}

}