ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1033. Лабиринт

How to read input in Java?
Послано Maigo Akisame (maigoakisame@yahoo.com.cn) 8 авг 2007 13:38
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?
Послано BiTOk[PermSU] 30 май 2009 23:56
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?
Послано u_saydazimov 2 окт 2010 09:32
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?
Послано Moonstone [Samara SAU] 2 окт 2010 12:03
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?
Послано SButterfly [Samara SAU] 15 янв 2015 15:52
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;
}