Reading Strings
import java.io.*; class JavaApplication { static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { System.out.println("What's your name? "); String s = stdin.readLine(); System.out.println("Hello, " + s + "!"); } }
What's your name? Blaise Pascal Hello, Blaise Pascal!
Reading ints
import java.io.*; class JavaApplication { static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { System.out.println("Enter first number: "); int i = Integer.parseInt(stdin.readLine()); System.out.println("Enter second number: "); int j = Integer.parseInt(stdin.readLine()); System.out.println(i + " + " + j + " = " + (i + j)); } }
Enter first number: 2 Enter second number: 3 2 + 3 = 5
Parsing Strings
readLine() returns a String. This string is parsed into an int with the wrapper class "Integer".
- See Also:
- Conversions Using Wrapper Types
java.io is the package name, BufferedReader is the public package member and java.io.BufferedReader is the long name.
0 comments:
Post a Comment