Save character by character within an array

Asked

Viewed 145 times

0

inserir a descrição da imagem aqui
(my return)
I would like to convert a string into an array of characters, a character in each index.

I would like 'hi' to be converted to var[0] = 'o', var1 = 'i';

I am making an array calculator and need to read the matrix data of a txt file, the file:

2 2
34 78
89-12
@
2 2
67 76
123 5

I’m reading the file with (excerpt from the code):

 BufferedReader in = new BufferedReader(new 
 FileReader(CaminhoArquivo));

I need to pass each of these characters(numbers) for each example: [2] [2] [34] [78] [89] [@]

  package calculadoramatrizes;
  import java.io.BufferedReader;
  import java.io.FileInputStream;
  import java.io.FileReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.util.ArrayList;
  import java.util.List;

  public class Conteudo {   
     public List<String> Matriz(){

     String CaminhoArquivo=("C:\\Users\\jessica borges"
            + "\\Downloads\\CalculadoraMatrizes-20190315T112959Z-001"
            + "\\CalculadoraMatrizes-20190315T112959Z-001"
            +"\\CalculadoraMatrizes\\src\\ArquivosMatriz\\matrix.txt");

    List<String> conteudo = new ArrayList<String>(); 
        try {
            BufferedReader in = new BufferedReader(new FileReader(CaminhoArquivo));
            String linha;
            while ((linha = in.readLine()) != null) { 
                conteudo.add(linha);
            }   
             in.close();
        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    System.out.println(conteudo);
    return conteudo;        

}         

public void CriaMatriz(){


}

}

    =================== main ====================

   package calculadoramatrizes;
   public class Matrix {
         public static void main(String[] args){
             chamarMatriz();
          }
          public static void chamarMatriz(){
             Conteudo ct = new Conteudo();
             ct.Matriz();
          }
    }

3 answers

1

int var[] = new int[conteudo.size()]; for (String str : conteudo) { String[] split = str.split(" "); var[Integer.parseInt(split[0])] = Integer.parseInt(split[1]); }

0


In the section where you add the line:

conteudo.add(linha);

Replace with:

conteudo.addAll(Arrays.asList(linha.split(" ")));

String.split(String s) divides the String in a String[] where s is the character of separation. Arrays.asList(Array a) converts the String[] on a list. List.addAll(Collection c) adds a collection at the end of the list.

Your code will look approximately like this:

try {
            BufferedReader in = new BufferedReader(new FileReader(CaminhoArquivo));
            String linha;
            while ((linha = in.readLine()) != null) { 
                // Subistitua conteudo.add(linha) por:
                conteudo.addAll(Arrays.asList(linha.split(" ")));
            }   
             in.close();
        } catch (IOException ioe) {
            System.out.println(ioe);
        }
  • I did it in my head. If I don’t make a comeback.

-1

Just do:

String qualquer = "Qualquer";
char[] qualquerArray = qualquer.toCharArray();

Java itself already provides a method for this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.