Problem when changing a string word for another word in a phrase

4

Hello, I just need help printing the sentence already changed please. When printing I get the following: [Ljava.lang.String; @ 2a139a55

Here is my code:

public static void main(String[]  args) throws IOException  {
        BufferedReader leer= new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Hola ingresa una frase");
        String frase= leer.readLine();
        System.out.println("Ahora ingresa la palabra a suplir");
        String palabrai=leer.readLine();
        System.out.println("Finalmente ingresa por cual palabra necesitas cambiar");
        String sustituir=leer.readLine();
        String[] arreglofrase=frase.split(" ");
        String vacio="";
        for(int x=0;x<arreglofrase.length;x++){
            if (arreglofrase[x].equals(palabrai))
            {
                arreglofrase[x]=sustituir;
                System.out.println(arreglofrase.toString());
            }
        }


        }
    
asked by Cristian Castillo 07.03.2016 в 06:04
source

4 answers

3

You are printing the entire array value:

System.out.println(arreglofrase.toString());

why you get:

Ljava.lang.String;@2a139a55

To print the result with the word to replace, which I imagine is what you really want to do, you could do it after for by printing each of the elements of the array, containing the element with the word to be replaced by:

  for (String s : arreglofrase) {
            System.out.print(s + " ");
        }

or if you use Java version > = 8:

System.out.println(String.join(" ", arreglofrase));

for example:

        BufferedReader leer= new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Hola ingresa una frase:");
    String frase= leer.readLine();
    System.out.println("Ahora ingresa la palabra a suplir:");
    String palabrai=leer.readLine();
    System.out.println("Finalmente ingresa por cual palabra necesitas cambiar:");
    String sustituir=leer.readLine();
    String[] arreglofrase=frase.split(" ");
    String vacio="";
    for(int x=0;x<arreglofrase.length;x++){
        if (arreglofrase[x].equals(palabrai))
        {
            arreglofrase[x]=sustituir;
           // System.out.println(arreglofrase[x].toString());
        }
    }

   for (String s : arreglofrase) {
        System.out.print(s + " ");
    }

this would be an example of the output:

Hola ingresa una frase:
Hola Cristian Castillo
Ahora ingresa la palabra a suplir:
Castillo
Finalmente ingresa por cual palabra necesitas cambiar:
Programador
Hola Cristian Programador
    
answered by 07.03.2016 в 06:27
2

If you are using Java 8, you can use the join of the class java.lang.String . The first argument is the separator , then a string array . That is:

for (int x = 0; x < arreglofrase.length; x++) {
    if (arreglofrase[x].equals(palabrai)) {
        arreglofrase[x] = sustituir;
    }
}

System.out.println(String.join(" ", arreglofrase));
    
answered by 07.03.2016 в 06:44
1

As mentioned, the behavior you are getting is due to the use of arreglofrase.toString() .

To know what happens when you call toString in this case, you can look at this link

It says something like this:

  

Returns a string representation of the object. In general, the   toString method returns a string that "textually represents" this   object. The result should be a concise representation but   informative that is easy for a person to read. It is recommended that   all subclasses replace this method . The toString method for the   class Object returns a string consisting of the class name   of which the object is an instance, the character at '@', and the   unsigned hexadecimal representation of the object's hash code. In   other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

, case to part:

"you could overwrite this method, if you had an object that you want to use when printing that particular method, I just mention it as a note"

The previous solutions are good, but I could also use something like that, it's more rustic but it works:

System.out.println(Arrays.toString(arreglofrase).replaceAll("\[|\]|\s", "")
                                                .replaceAll("\,", " "));

ideone Test

It is compatible towards after to give some example:

link link , java. lang.String)

    
answered by 07.03.2016 в 15:03
1
arreglofrase 
  

is an object when you do the toString without rewriting the function   usually throws the internal memory value, to avoid that   show it as a String vector

    for(String x:arregloFrase)
   {
System.out.println(x);
   }
  

With that you can see what result is throwing and start to correct

    
answered by 08.03.2016 в 16:09