how to print array with strings? [closed]

0

Hi, I'm learning java, I'm doing a program where the user can enter several names with their birthday but I'm stuck because I want to print the list of names that I previously asked and put in an array using a scanner but I get this when I run the program at the end when I want to print the entire array. [Ljava.lang.String;@880ec60[Ljava.lang.String;@880ec60[Ljava.lang.String;@880ec60[Ljava.lang.String;@880ec60[Ljava.lang.String;@880ec60

here is my code:

import java.util.Scanner;

public class birthday {
    public static void main(String args[]){ 
    Scanner input  = new Scanner(System.in);
    String[] words = new String[5];
    for (int i = 0; i < words.length; i++) {

        System.out.println("Birthday name?");
        words[i] = input.next();

    }
    for (int i = 0; i < words.length; i++){
        System.out.print(words.toString());

Here I created an array with 5 to put the 5 names asked and use a cycle to ask 5 times and if it was to print the same thing, try using another cycle to print the array of names but the same thing was still happening can someone help me please, thank you.

    
asked by Microplo 07.12.2017 в 18:50
source

1 answer

1

In the second for you are not indexing the array

for (int i = 0; i < words.length; i++){
        System.out.print(words.toString());
}

Change it for this:

 for (int i = 0; i < words.length; i++){
            System.out.print(words[i]);
    }

It may be necessary to parcel it depending on the use you give it or what comes to you, something like:

for (int i = 0; i < words.length; i++){
        System.out.print(words[i].toString());
}

and to show the name, this should be the for:

for (int i = 0; i < words.length; i++) {

        System.out.println("Birthday name?"+words[i]);

    }
    
answered by 07.12.2017 / 18:58
source