Last active
August 29, 2015 13:57
-
-
Save mquezada/9601295 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.util.StringTokenizer; | |
class IO_2 { | |
static public void main(String[] args) throws IOException { | |
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); | |
String line; | |
StringBuilder sb = new StringBuilder(); | |
StringTokenizer st; | |
// cada linea es del tipo N_i A_1 A_2 A_3 ... A_(N_i) | |
// a continuacion, dos formas equivalentes de "parsear" estos datos | |
while((line = bf.readLine()) != null) { | |
// forma # 1 | |
st = new StringTokenizer(line); | |
while(st.hasMoreTokens()) { | |
String token = st.nextToken(); // token es un numero, pero de tipo string | |
int num = Integer.parseInt(token); // ahora es un numero, pero si no, arrojara una excepcion | |
/* hago cosas con el numero */ | |
sb.append(num + "\n"); | |
} | |
// forma # 2 | |
String[] tokens = line.split(" "); // divide el string basado en espacios y guarda cada parte en un arreglo | |
for(String token : tokens) { // equivalente a "for token in tokens" de python | |
int num = Integer.parseInt(token); // ahora es un numero, pero si no, arrojara una excepcion | |
/* hago cosas con el numero */ | |
sb.append(num + "\n"); | |
} | |
} | |
System.out.println(sb.toString()); // imprimimos todo junto al final | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment