How do I scanf, readln, etc. in Java?
Java has no exact equivalent to C’s scanf(), fscanf() and sscanf() functions, Pascal’s read() and readln() function, or Fortran’s READ* function. In particular there’s no one method that lets you get input from the user as a numeric value. However, roughly equivalent functionality is scattered across several classes. You first read an input line into a String using DataInputStream.readline() or BufferedReader (in Java 1.1) Next use the StringTokenizer class in java.util to split the String into tokens. By default StringTokenizer splits on white space (spaces, tabs, carriage returns and newlines), but this is user definable. For example, import java.util.StringTokenizer; class STTest { public static void main(String args[]) { String s = “9 23 45.4 56.7”; StringTokenizer st = new StringTokenizer(s); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } } } prints the following output: 9 23 45.4 56.7 Finally you convert these tokens into numbers using the type wrapper classes