0

I am trying to take in someones first name and last name, and then take those two Strings and put them into one ArrayList together.

I am able to take in the two Strings, but the problem is I can't output them.

I have two classes as follows:

import java.util.ArrayList;
import java.util.Scanner;


public class TakeInName {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        ArrayList<Names> studentNames = new ArrayList<Names>();
        Names newName = new Names();

        newName.firstName = scan.nextLine();
        newName.lastName = scan.nextLine();

        studentNames.add(newName);

        Names item = studentNames.get(0);

        System.out.print(item);

    }
}

and second:

public class Names {
    String lastName;
    String firstName;
}

I am not even sure if I made this second class right to be able to do anything with it? Maybe that is my problem?

When I run this code the output I get is: Names@5c647e05

Maybe that is the memory position?

Any help is appreciated, thank you.

CCovey
  • 799
  • 1
  • 10
  • 17
André Fecteau
  • 140
  • 2
  • 11

2 Answers2

1

To get the names you have to do

System.out.print(item.firstName+" "+item.lastName);

because you are fetching the Names object .The string(Names@5c647e05) you get is expected.Its the string representation of object where Name is the class ,@ character which joins the string, and 5c647e05 some hash code

Answer to the comment.

The code would be

Names newName1=new Names();
newName1.firstName = scan.nextLine();
newName1.lastName = scan.nextLine();
studentNames.add(newName1);

Names newName2=new Names();                //create new object for new name
newName2.firstName = scan.nextLine();
newName2.lastName = scan.nextLine();
studentNames.add(newName2);
Names item = studentNames.get(0);
System.out.print(item.firstName + " " + item.lastName);
Names item1 = studentNames.get(1);
System.out.print(item1.firstName + " " + item1.lastName);
singhakash
  • 7,891
  • 6
  • 31
  • 65
  • What does being in a list have to do with anything? – azurefrog Apr 10 '15 at 17:05
  • @azurefrog because OP fetching the object from list I was referring to that – singhakash Apr 10 '15 at 17:10
  • Now I may be doing this wrong but I just quickly made it so I could add a second full name to the array list like so: ' newName.firstName = scan.nextLine(); newName.lastName = scan.nextLine(); studentNames.add(newName); newName.firstName = scan.nextLine(); newName.lastName = scan.nextLine(); studentNames.add(newName); ' output like this: ' Names item = studentNames.get(0); System.out.print(item.firstName + " " + item.lastName); Names item1 = studentNames.get(1); System.out.print(item1.firstName + " " + item1.lastName); ' it only outputs the second name. – André Fecteau Apr 10 '15 at 17:17
  • @AndréFecteau create a new object for the second set of names because when you do `newName.firstName = scan.nextLine(); ` fot the second set of name it changes the previous object properties to that.Check my edits – singhakash Apr 10 '15 at 17:27
  • @singhakash oh, that is simple, thanks! now If I was to make this something that the user could perpetually keep entering names into until they are done would I need to create a new object every time or is there an easier way than that? – André Fecteau Apr 10 '15 at 17:30
  • @AndréFecteau just place you code in a loop and every time ask user if the user want to continue(say inputs are yes or no).If user enters `yes` then repeat the set of code create object, set the names, add to list and if user enters `no` breaks the loop – singhakash Apr 10 '15 at 17:35
0

You can also override toString function in Names class like:

public String toString(){
    return lastName + "  " + firstName;
}

which is the best option for printing object values as a String

roeygol
  • 4,908
  • 9
  • 51
  • 88