Jump to content

Leitura de ficheiro txt e matriz


JoaoSkate

Recommended Posts

Ola bom dia, estou a tentar ler um ficheiro .txt e criar uma matriz de boolean que fica a true nas posiçoes com o a string "=" , mas estou com algumas dificuldades, o ficheiro é deste tipo

==========
=  L   L =
#        *
#   L    *
#   =    =
#        *
#  k =   *
=     k  *
#  X     *
==========

o que tentei foi:

 

public boolean[][] readToMatrix() throws FileNotFoundException{
		
		Scanner sc = new Scanner(new File("Pasta/ficheiro.txt"));
		String linha = "";
		
		
		
		boolean[][] matriz = new boolean[10][10];
		
		ArrayList<String> list = new ArrayList<String>();
		
		while(sc.hasNextLine()){
			linha =  sc.nextLine();
			list.add(linha);
		}
		
		sc.close();
		
		int j = -1 ;
		
		for(String k : list){
			j++;
			for(int i = 0 ; i < k.split("").length ; i++){
				if(k.split("")[i] == "="){
					matriz[i][j]== true;
				}
				else{
					matriz[i][j]=false; 
				}
			}
			
		}
		
		
		return matriz;
						
	}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.