hue manny
hue manny

Reputation: 247

How to Print two columns of loops

I have a file that has a list of names along with their sex and a count of how many people have the name. I want to print the top 10 female and male names side by side . My issue is when i run my program this is what it prints.

What is Printing

"Female"    "Male"

  Jacob     Jacob
  Ethan     Ethan
  Michael   Michael
  Jayden    Jayden
  William   William
  Alexander Alexander
  Noah      Noah
  Daniel    Daniel
  Aiden     Aiden
  Anthony   Anthony

I want this to print

"Female"    "Male"

Isabella     Jacob
Sophia       Ethan
Emma         Michael
Addison      William
Elizabeth    Alexander    
Ella         Joshua
Olivia       Mason
Ava          Evan
Emily        Nicholas
Abigail      Gavin                

for (String s : descending())
{

    for (int i = 0 ;i < 10;i++)
    {
    String z = oneName.get(i).getName();

        if('M' == oneName.get(i).getSex())
        {
         System.out.printf("%13.10s%10.10s%n",z, oneName.get(i).getName());

        }
        if('F' == oneName.get(i).getSex())
        {

          z = oneName.get(i).getName();
        }
    }

        break;
}

Upvotes: 0

Views: 870

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347334

Each time your print something, z has always been set to the current name...

String z = oneName.get(i).getName();
if('M' == oneName.get(i).getSex())
{
    System.out.printf("%13.10s%10.10s%n",z, oneName.get(i).getName());

This would suggest that the female name seems to be coming first.

Assuming that the data comes in female/male order you could do something like...

String maleName = null;
String femaleName = null;
for (int i = 0; i < 10; i++) {

    if ('M' == oneName.get(i).getSex()) {

        maleName = oneName.get(i).getName();

    }
    if ('F' == oneName.get(i).getSex()) {

        femaleName = oneName.get(i).getName();

    }

    if (maleName != null && femaleName != null) {

        System.out.printf("%13.10s%10.10s%n", femaleName, maleName);
        maleName = null;
        femaleName = null;

    }
}

But I'm making assumptions about the order in which the data is stored and without evidence to the country, it's difficult to suggest something else

Updated with a proof of concept

import java.util.ArrayList;
import java.util.List;

public class Test {

    public static void main(String[] args) {
        List<Person> people = new ArrayList<>(25);
        people.add(new Person("Isabella", 'F'));
        people.add(new Person("Jacob", 'M'));
        people.add(new Person("Sophia", 'F'));
        people.add(new Person("Ethan", 'M'));
        people.add(new Person("Emma", 'F'));
        people.add(new Person("Michael", 'M'));
        people.add(new Person("Addison", 'F'));
        people.add(new Person("William", 'M'));
        people.add(new Person("Elizabeth", 'F'));
        people.add(new Person("Alexander", 'M'));
        people.add(new Person("Ella", 'F'));
        people.add(new Person("Joshua", 'M'));
        people.add(new Person("Olivia", 'F'));
        people.add(new Person("Mason", 'M'));
        people.add(new Person("Ava", 'F'));
        people.add(new Person("Evan", 'M'));
        people.add(new Person("Emily", 'F'));
        people.add(new Person("Nicholas", 'M'));
        people.add(new Person("Abigail", 'F'));
        people.add(new Person("Gavin", 'M'));

        String maleName = null;
        String femaleName = null;
        for (Person p : people) {

            if ('M' == p.getSex()) {

                maleName = p.getName();

            }
            if ('F' == p.getSex()) {

                femaleName = p.getName();

            }

            if (maleName != null && femaleName != null) {

                System.out.printf("%13.10s%10.10s%n", femaleName, maleName);
                maleName = null;
                femaleName = null;

            }
        }
    }

    public static class Person {

        private String name;
        private char sex;

        public Person(String name, char sex) {
            this.name = name;
            this.sex = sex;
        }

        public char getSex() {
            return sex;
        }

        public String getName() {
            return name;
        }

    }

}

Which prints out...

 Isabella     Jacob
   Sophia     Ethan
     Emma   Michael
  Addison   William
Elizabeth Alexander
     Ella    Joshua
   Olivia     Mason
      Ava      Evan
    Emily  Nicholas
  Abigail     Gavin

Upvotes: 1

newuser
newuser

Reputation: 8466

String z = oneName.get(i).getName();

declare the variable z outside the forLoop otherwise it creates new String everytime.

String z = null;
for (String s : descending())
{
    for (int i = 0 ;i < 10;i++)
    {
       z = oneName.get(i).getName();

Upvotes: 0

Related Questions