2

I'm a student just starting out in Java; I get hung up on seemingly easy concepts and have had trouble finding the answer to this despite a lot of googling. The assignment asks to:

  • Prompt the user to enter the number of people
  • Create a String array of the given size
  • Prompt the user for the name of each person
  • Put each name in the String array you created
  • Use a Java for-each to print each name with the length of the name

The output should look something like:

  • "Person 1 is named Andrew, and their name is 6 characters long"

This is what I currently have coded.

    System.out.print("Hello. Please enter the number of people: ");

    int people = scan.nextInt();

    String[] myPeople = new String[people + 1];

    System.out.println("Enter the name of each person:");
    for(int i = 0; i < myPeople.length; i++)    

    {       
        myPeople[i] = scan.nextLine();                  
    }


    for(String peoples : myPeople)
    {   
        System.out.println(peoples);         
    }

As it stands right now, the program can collect input from the user, put it into an array, and then print the array back out.

Am I approaching this the wrong way? I can't think of how I would modify the last For to not just print all the names, and instead print out each one with the "Person X is named ... ", their name, and then the description of how many characters long their name is.

2
  • 1
    print and to concatenate strings using the + operator System.out.println("Name " + peoples + " Length : " + peoples.length()); Commented Sep 15, 2016 at 6:05
  • 1
    Possible duplicate of Java String Concatenation with + operator Commented Sep 15, 2016 at 6:11

3 Answers 3

3

Read about string concatenation.

    int j = 1;
    for(String peoples : myPeople)
    {   
            System.out.println("Person " + j + " is named " + peoples + " and their name is " + peoples.length() + " characters long");
            j++;
    }

+ operator is used for concatenation of the strings.

Sign up to request clarification or add additional context in comments.

1 Comment

I think the key part was peoples.length; I wasn't sure how to access the length of items in the array. Thank you very much.
2

You are doing good. In last for loop you can just do something like:

int personNumber = 0;
for(String people: myPeople)
{
System.out.println(String.format("Person %d is named %s, and their name is %d characters long", personNumber + 1, people, people.length());
personNumber++;
}

And one thing I've noticed - why are you instantiating array using new String[people + 1]? Why this extra one person? Arrays indexes are numered from 0, so new String[5] would give you array of 5 persons.

6 Comments

new String[5] will never gives you a array with 6 items. it will have array with indexes 0,1,2,3,4 so 5 items in total
@T.G Edited and up for you :)
I'm not sure why, but when I just instantiate it with String[] myPeople = new String[people]; , it doesn't work properly. The user enters the number of people, is asked to enter in that many names... And proceeds to stop the loop after they've entered one less than the number they entered. Looks like this: i.gyazo.com/7b31abc94e6a0a5af3411b009619c029.png
Scan.next will not properly record first and last names. So 'Andrew Black' is stored as 'Andrew' and 'Black', introducing a new issue, it seems.
such problems probably means that you are forgetting that collections are numbered for 0 not from 1. code you provided on screen had to had loop started from 1 not form 0
|
0

Firstly, you should create an array of size people, not people + 1:

String[] myPeople = new String[people];

To produce output in the required the format, you need to use the + operator to join the strings together. Also, since the person's position in the array is required, you need to create a variable to store that:

int index = 1;
for (String person: myPeople) {
    String output = "Person " + index + " is named " + person + ", and their name is " + person.length() + " characters long.";
    System.out.println(output);
    index++;
}

Alternatively, you can use print instead of println: (only showing code inside for loop)

System.out.print("Person ");
System.out.print(index);
System.out.print(" is named ");
System.out.print(person);
System.out.print(", and their name is ");
System.out.print(person.length());
System.out.println(" characters long.");
index++;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.