Error using split function and removing certain characters from a java file

Asked

Viewed 80 times

1

The idea is to separate pieces with the split function that does not contain spaces, comma, point and comma, etc. But the function does not separate correctly, separating most of the time only space.

public String[] getTokens(String s)
{
    String[] tokens;
    tokens = s.split(",");
    tokens = s.split(" ");
    int pos = 0;
    for(int i = 0; i < tokens.length; i++)
    {
        if(tokens[i].equals(";") || tokens[i].equals(","))
        {
            tokens[i] =" ";
            pos = i;
            break;
        }
    }

public void execute()
{
    File[] filesArray = dt.returnFiles();
    for(File f : filesArray)
    {
        try
        {
            fileContent = dt.loadFile(f);

            for(int i=0; i < fileContent.size(); i++)
            {

                if(fileContent.get(i).equals(",") || fileContent.get(i).equals(";") || 
                        fileContent.get(i).isEmpty())
                {
                    fileContent.remove(i);
                }

            }
  • Amendment!

  • Alan, the picture you can leave in the question, the biggest problem was not having the code in text, as it has now.

  • Faith, I thought it was wrong...

  • 1

    The problem with your code is that you are overwriting the tokens variable after the second split. Try to pass all the characters you would like to use as a split function parameter, more or less like this: tokens = s.split("[\\s;,]"); With this, you can divide the string by all characters.

  • Look bro, I have the following excerpt as an example that is not working: ; Programa LOGICA.ASM&#xA;&#xA;; Este programa testa todas as operacoes logicas da ALU&#xA;&#xA;; Cada teste incrementa o registrador r4 se for bem sucedido.&#xA;; no final, se todos os testes forem bem sucedidos, a pontuacao&#xA;; atinge valor 10, que eh armazenada em Mem[1000]&#xA;; Cada teste verifica se a tabela verdade para a operacao&#xA;; esta sendo gerada corretamente.&#xA;&#xA;zeros r4&#xA;&#xA;teste0:&#xA; zeros r0 ; Operacao C = A & B;&#xA; zeros r1&#xA; and r1,r0,r1&#xA; jf.zero teste1&#xA; You know why the split is not working?

  • Alan, you did two assignments for the variable s followed. Imagine that you wrote on a page of a notebook the result of the operation s.split(","), then you ripped the page off and now (with s intact) s.split(" "). Always remember that in Java strings are immutable

Show 1 more comment
No answers

Browser other questions tagged

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