Vous devez construire quelque chose pour analyser le flux d'entrée. Si l'on suppose qu'il est littéralement non complexe que vous avez indiqué la première chose que vous devez faire est d'obtenir la ligne de la InputStream
, vous pouvez le faire comme ceci:
// InputStream in = ...;
// read and accrue characters until the linebreak
StringBuilder sb = new StringBuilder();
int c;
while((c = in.read()) != -1 && c != '\n'){
sb.append(c);
}
String line = sb.toString();
Ou vous pouvez utiliser un BufferedReader
(comme le suggère commentaires):
BufferedReader rdr = new BufferedReader(new InputStreamReader(in));
String line = rdr.readLine();
une fois que vous avez une ligne que vous devez traiter le diviser en morceaux, puis traiter les morceaux dans le tableau désiré:
// now process the whole input
String[] parts = line.split("\\s");
// only if the direction is to read the input
if("read".equals(parts[0])){
// create an array to hold the ints
// note that we dynamically size the array based on the
// the length of `parts`, which contains an array of the form
// ["read", "1", "2", "3", ...], so it has size 1 more than required
// to hold the integers, thus, we create a new array of
// same size as `parts`, less 1.
int[] inputInts = new int[parts.length-1];
// iterate through the string pieces we have
for(int i = 1; i < parts.length; i++){
// and convert them to integers.
inputInts[i-1] = Integer.parseInt(parts[i]);
}
}
Je suis s Certaines de ces méthodes peuvent lancer des exceptions (au moins read
et parseInt
), je vais laisser les manipuler comme un exercice.
vérifier ce [exemple] (http://www.ensta.fr/~diam/java/online /notes-java/data/arrays/arrays.html). – Emil