|
|
вернуться в форумHow to read input in Java? Is there a class or sth that can both read integers and single chars? Somebody please give me some example code. Thanks! Re: How to read input in Java? I write this: import java.io.*; public class Main { static BufferedReader in; //........ public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int n = Integer.parseInt(in.readLine()); //..... for(int i=1; i<n+1; i++){ for(int j=1; j<n+1; j++) { lab[i][j] = (char)in.read(); while(lab[i][j]!='#' && lab[i][j]!='.') lab[i][j] = (char)in.read(); //in win used two bytes end of line, in *NIX 1 byte } in.read(); } //..... Re: How to read input in Java? import java.util.*; import java.io.*; public class Main{ public static void main(String[] argv) throws IOException{ new Main().run(); } PrintWriter pw; Scanner sc;
public void run() throws IOException{ boolean oj = System.getProperty("ONLINE_JUDGE") != null; Reader reader = oj ? new InputStreamReader(System.in) : new FileReader("input.txt"); Writer writer = oj ? new OutputStreamWriter(System.out) : new FileWriter("output.txt"); sc = new Scanner(reader); pw = new PrintWriter(writer); int n = sc.nextInt(); int arr[][] = new int[n+2][n+2]; for(int i=1;i<=n;i++){ String str = sc.next(); for(int j=0;j<n;j++) if(str.charAt(j)=='.') arr[i][j+1] = 1; } //... pw.close(); }
} Re: How to read input in Java? The best template for Java: BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); StringTokenizer tok = null; String readToken() throws IOException { // reads the token; to read a full line use in.readLine() while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); // sometimes it's better to use nextToken(String) method here } int readInt() throws IOException { // write readDouble(), readLong(), readBigInteger() methods if necessary return Integer.parseInt(readToken()); } Re: How to read input in Java? In addition to previouse answer char[] arr = readString().toCharArray(); //reads string and creates an array of characters. It works not so fast, but for small input, like this, it would be the quickest way to write a code. If you want to read for 1 char manual try this: char readChar() throws IOException{ int x = 0; while ((x = in.read()) == '\n' || x == '\r' || x == ' '){ } return (char)x; } |
|
|