-2

I need to display multiple list arrays in one line through the for loop - so basically I need to count them as well.

Example:

enter image description here

Here is my code:

List<String> name = new ArrayList<String>();
List<String> number = new ArrayList<String>();
  name.add("Tommy");                      // Test name
  name.add("Tom");                        // Test name 2
  number.add(new String("123-456-7890")); // Test phone number
  number.add(new String("098-765-4321")); // Test phone number 2

for (String d:name) {
      System.out.print(d);
    }
for (String b:number) {
      System.out.println(b);
    }

And here is my output with my code:

enter image description here

Sorry if this question was duplicated, if it is, I will delete my question right away but for now I haven't fount anything like this.

Thank you.

John E.
  • 191
  • 3
  • 10

3 Answers3

2

I think in this case you are better off using a Map, which allows you more flexibility and better control over the entries:

    import java.util.HashMap;
    import java.util.Map;

    /**
     * Created by dlcol_000 on 3/19/2016.
     */
    public class MapExample {
        public void execute(){
            Map<String,String> phoneDirectory = new HashMap<>();
            phoneDirectory.put("Tommy","123-456-7890");
            phoneDirectory.put("Tom", "098-765-4321");

            //Now you can do things like, look up the phone number for a person:
            String tomsPhone = phoneDirectory.get("Tom");
            System.out.println("Toms' phone number is: " + tomsPhone);

            //Or you can print the contents of the phone directory
            System.out.println("The phone directory contains: " + phoneDirectory);

            //Or you can iterate over all people in the directory printing their numbers
            for(String person: phoneDirectory.keySet()){
                System.out.println(person + ": " + phoneDirectory.get(person));
            }
        }

        public static void main(String... args){
            new MapExample().execute();
        }
    }

Which gives the following output:

Toms' phone number is: 098-765-4321
The phone directory contains: {Tom=098-765-4321, Tommy=123-456-7890}
Tom: 098-765-4321
Tommy: 123-456-7890
pczeus
  • 7,195
  • 4
  • 34
  • 50
  • Thank you, I will try this – John E. Mar 19 '16 at 18:21
  • I have a question, what if I also have an option to add new contacts? How can I add input value to a HashMap? – John E. Mar 19 '16 at 19:08
  • Can you explain what you mean by adding input value? – pczeus Mar 19 '16 at 19:20
  • for example I want to add a new contact with name and phone number and put it in the HashMap, like you did as a test value with Tommy. Sorry for this question, this is new for me – John E. Mar 19 '16 at 19:25
  • Do you mean that you want to allow user input from the command line to enter a new name and phone number to add to the phone directory? – pczeus Mar 19 '16 at 19:30
  • There are plenty of examples on this website for gathering user input using `java.util.Scanner`: http://stackoverflow.com/questions/11871520/how-can-i-read-input-from-the-console-using-the-scanner-class-in-java . Have the user input a name, then a phone. After that just add it to the map as I did in the example. – pczeus Mar 19 '16 at 19:34
1

You are printing complete the name list and then the number list, you should instead intercalate/alternate and print one from each list for every iteration

final List<String> name = new ArrayList<String>();
final List<String> number = new ArrayList<String>();
name.add("Tommy"); // Test name
name.add("Tom"); // Test name 2
number.add(new String("123-456-7890")); // Test phone number
number.add(new String("098-765-4321")); // Test phone number 2

for (int i = 0; i < name.size(); i++) {
    System.out.println(name.get(i) + "\t\t" + number.get(i));
}

at the end I will suggest you as comment above to define a Class "ContactPerson" and override the toString method.

With that approach you will get it faster and safer

ΦXocę 웃 Пepeúpa ツ
  • 43,054
  • 16
  • 58
  • 83
0

It really depends on what you want to use your objects for.

One important aspect is that one should separate concerns. In your case:

  1. there is a concern about "persons"; where each person probably has one name; and one ... or maybe several (!) phone numbers
  2. there is a concern on how certain information is modeled internally. Of course, you can think of a phone number as a simple string with numbers. But you could also design a whole class that holds phone numbers.
  3. there is a concern on how certain information is displayed.

Based on that, you could the following for starters:

  • create a class "Person" that has two fields; a String for the name; and a String for the phone number
  • override the toString() method that contains both name and phone number

And all of a sudden, when iterate your list of Person objects and call System.out.println(person.toString()) for each person; you have nicely formatted output.

My key message to you: understand that there are plenty of choices; and shouldn't stop on the first "working" version; but play around ...

GhostCat
  • 127,190
  • 21
  • 146
  • 218