0
I have a question about a college job. I can read with numbers, remove ";" and pass the values to the array while reading the file. However, when I try to access a position of the array outside of reading the file, it does not return the value I have in the file but another value. If anyone can help, thank you.
ARCHIVE I READ
10
1;1;1
2;2;3;2;150;20
3;3;3;1;100;10
4;4;3;4;350;30
5;5;3;1;100;10
6;6;3;2;150;20
7;7;3;3;100;10
8;8;3;5;500;10
9;9;2
10;10;3;1;100;10
EXIT FROM THE PROGRAMME
10
1
1
1
2
2
3
2
150
20
and so on ...
If I want to read the number 2 that is in the 5th position of the output, I do:
System.out.println(array[4]);
and instead of returning the number 2 it returns the 100.
MY CODE
package jogo;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Tabuleiro {
private static BufferedReader br;
public static void main(String[] args) {
String[] array = null;
try {
br = new BufferedReader(new FileReader("/home/overwatch/tabuleiro1.txt"));
String linha = br.readLine();
while(linha!=null){
array = linha.split(";");
linha = br.readLine();
for(int i=0; i < array.length;i++){
System.out.println(array[i]);
}
}
br.close();
//imprimindo o 3 valor que está no array
System.out.println(array[4]);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
I’m glad you had to change so little in my code... thanks for the explanation and solution :)
– user79427