Word break

Asked

Viewed 469 times

2

I’m trying to make a line break in a Array, but I am not succeeding in trying to execute the split(), summarizing, I want to make a table of 10 rows with 2 columns that acquire the values of list and list2

public class Ultimos10 {

    public Ultimos10(ObjectOutputStream saida, Socket cliente) throws IOException {
        String resultados = NovoJogo.list.toString(); //Puxa Array da outra classe
        String resultados2 = NovoJogo.list2.toString();

        String quebra[]=resultados.split("\\n"); //Tentando colocar split
        String quebra2[]=resultados2.split("\\n");

        for(String resul:quebra){ //Imprime na tela do cliente
            saida.writeObject(resul+"\r\n");
        }

        for(String resul2:quebra){
            saida.writeObject(resul2);
        }

        Servidor serv = new Servidor();
        serv.Menu(cliente);
    }
}

I also have doubts regarding the exclusion of a line and draw by results placed by first.

  • What is the content of resultados and resultados2? Are you sure to give a toString in an array will do what you want? And also, your question is not very clear, try edit it and add more details.

  • resultados and resultados2 receive the values of two ArrayLists, to list and list2, that Array that I want to create needs to be String to be read by the client, because that’s how I created the client reading part, but that’s not the case, I was trying to do something like String[][] res = new String[10][2];
 res[0][0] = resultados;
 res[0][1] = resultados2; , but gives the error [[Ljava.lang.String; cannot be cast to java.lang.String

  • Okay, let’s go in pieces. Make a System.out.println(resultados); and System.out.println(resultados2); and see on the console which the contents of these two variables.

  • The result is correct, is pulling the variables of the list and list2 correctly

  • But I need to see the contents of them in order to help you. Another thing, in the comment above you say that you are receiving an error, in your question you say nothing about error. Put the error in the question so that other people can see clearly and help you.

  • Okay, I’ll explain the purpose of my program to make it clearer, I have a client who gets the information and the server, this server launches a random number and the client "kicks" values until right, at the end I need to show a table that informs the random number that the server has launched and the number of attempts, here comes the resultado(attempts) and resultado2(random number) the content comes out like this [tentativas, numRan, tentativas, numRan...] I need you to stay [ten, numRan] there on the bottom line the same thing [ten, numRan] in the case of 10 rows per 2 columns

Show 1 more comment

2 answers

3


I don’t have the code anymore, but I remember I did it this way:

String variavel = System.getProperty("line.separator");

When I wanted to break the line I pulled the variable

1

If both arrays have the same size (attempts) (randomic), then you can simplify your method:

String attempts[] = ...//Your array of attempts String numeroRamdomic[] = ...//Your random number array

for (int i = 0; i < tentativas.length; i++) {
   System.out.println("Tentativa: " + tentativas[i] 
                    + " | Numero Randomico: " + numeroRamdomico[i]);
}

This way you are generating your list with two columns and if you want to limit the number of rows just put instead of tentativas.length the amount you want.

  • Whoa, thanks for the answer, but I got it here ;)

  • @If Matheuslopes could do otherwise, he could post the solution as a response?

  • 1

    @utluiz answered, I hope to have helped ;)

Browser other questions tagged

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