I need to print out the titles for family members being stored in an array, but I am having issues with the report. It prints out all the stored names, instead of the ones that belong to each family member and one line for each. I'm new with arrays so, it's a learning process for me, thanks for the help!

import java.util.Scanner;

public class Geneology
{
private String[] family;  
private int[][] memberName;
private int size;
private int sizeNo;
private String[] titles;
private Scanner input = new Scanner(System.in);

public String[] setTitles()
{
  size = 0;
  sizeNo = 0;



  System.out.printf("\nYou will be entering the titles for your family members."
                      +"\nHow many family members will be entered?"
                      +"\nAlso, enter the highest number of titles of one of the family members: ");
 size = input.nextInt();
 sizeNo = input.nextInt();
 input.nextLine();

  memberName = new int[size][sizeNo];
  family = new String[size];
  titles = new String[sizeNo];

for(int a = 0; a < memberName.length; a++)
{
  System.out.printf("\nEnter name of member %d: ", a+1);
  family[a] = input.nextLine();

  for(int b = 0; b < memberName[a].length; b++)
  {
    System.out.printf("\nEnter title %d for %s: ", b+1, family[a]);
    titles[b] = input.nextLine();

  }
}
  return titles;
}

public void printTitles()
{

  for(int a = 0; a < memberName.length; a++)
  {
  System.out.printf("\n%S's TITLES\n%s\n", family[a], titles[a]);
}

This is the output that I am getting:
D's TITLES
sonnephewdaughterniece

A's TITLES
sonnephewdaughterniece

Recommended Answers

All 17 Replies

it's nice that you tell us what output you get, but if we don't know what input there was, it's useless.

you do understand, that since family and titles don't have the same length, this:
System.out.printf("\n%S's TITLES\n%s\n", family[a], titles[a]);
is bound to cause problems?

It looks like the problem is with the arrays you are using, noy how you print them.
You create a 2d memberNames array that looks like it should have one row for each family and member names for that family in the row, but its an array of ints, not Strings. Then you set up a titles array that is String[], but it is only one-dimensional, so it's not organised by family.
I recommend that you sit down with pen and paper and draw out your data structures with some sample data. When that makes sense, go back to the PC and convert it into Java arrays.

Well the teacher wanted a 1D array that stored the names and a 2D array that was the size of the members by the number of titles. I'm sorry I just learned arrays.

That makes sense, but if the 2D array is going to hold the titles for each name, it needs to be an array of String[][], not int[][], that's all

Yeah I just realized that I made that mistake. I must've done it that way because size and sizeNo are ints. Also, realized I'm not storing anything in memberName as well. So, he wants family name stored in a 1D array and then code a 2D array that will store the titles of your family members (i.e. aunt, uncle, cousin, daughter, son). The size of the array will be m by n where m is the number of family members and n is the highest number of titles for the family member. I guess I was confused by the size of the the 2D aray.

How would I store then the names of the titles then in that 2d array?

Something like (pseudo-code):

for i = 0 to no of families -1
   get family name & store it in names array[i]
   for j = 0 to no of titles for this family -1
      get title & store it in titles array[i][j]

ohhh, ok I didn't know you could do it like that. I will try it and see if it works, thank you!

Why the -1? Sorry, just trying to understand as much as I can.

That's because arrays in Java start with element 0
eg if you have 3 elements they are numbered [0]. [1] and [2]
that's 0 to (number of elements)-1
You soon get used to that and it becomes automatic.

I get it now, thank you for the help.

Ok, so that worked, but I am still having issue printing it out. It prints out like this (these aren't real names, but just letters to see if it prints properly): I need them to print where all the titles are under one name(Ex: S's titles)

S's TITLES
e

S's TITLES
t

D's TITLES
e

D's TITLES
w

this is my print code

public void printTitles()
{

  for(int a = 0; a < family.length; a++)
  {
    for(int b =0; b< titles[a].length; b++)
    {
      System.out.printf("\n%S's TITLES\n%s\n", family[a], titles[a][b]);
    }
  }

That's close. You just need to print the family before going into the loop for the titles

for(int a = 0; a < family.length; a++)
    print family[a] 
    for(int b =0; b< titles[a].length; b++) {
        print titles[a][b]) 
    }
}

ok well he wants it printed in a different method, hence my problem.

oh i see what youre saying.

It worked!!

Just in case your still having issues I changed your code a little, but I think your on the right track....just stay with it...

import java.util.Scanner;
public class Geneology
{
private String[] family;  
private String[][] titles;
private int size;
private int sizeNo;

private Scanner input = new Scanner(System.in);
public void setTitles()
{
  size = 0;
  sizeNo = 0;
  System.out.printf("\nYou will be entering the titles for your family members."
                      +"\nHow many family members will be entered?"
                      +"\nAlso, enter the highest number of titles of one of the family members: ");
 size = input.nextInt();
 sizeNo = input.nextInt();
 input.nextLine();
  titles = new String[size][sizeNo];
  family = new String[size];

for(int a = 0; a < family.length; a++)
{
  System.out.printf("\nEnter name of member %d: ", a+1);
  family[a] = input.nextLine();
  for(int b = 0; b < sizeNo; b++)
  {
    System.out.printf("\nEnter title %d for %s: ", b+1, family[a]);
    titles[a][b] = input.nextLine();
  }
}
}
public void printTitles()
{
  for(int a = 0; a < family.length; a++)
  {
    System.out.printf("\n%S's TITLES\n", family[a]);
    for (int b=0; b < sizeNo ; b++) {
       System.out.println(titles[a][b]);
    }
  }
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.