Hey, this is my first java program. I am trying to print my original list of ages, but I can not figure it out. Please help! I have part b and part c already done. Any help is appreciated

a) print original list of ages
b) number of occurrences of age 10
c) print list of ages removing age 10

import java.io.* ;

class BagofAges 
{

public static void main(String args[]) throws IOException
{

DataInputStream in=new DataInputStream(System.in);
int ages[]=new int[100];
int i=0,k=0,kcount=0;


do
{
System.out.print("enter the age of your next family member or -1 to terminate:" );
k= Integer.parseInt(in.readLine());



if(k==10)
{ 
kcount++; //counts the no of occurrence of age 10 and do not add it to the ages[] array
} 
else if (k!=-1) //saves all values in ages[] except for -1 and 10
{
ages[i++] =k;
}
}
while (k!=-1);


System.out.println("Number of age 10 occurrence:"+kcount);

System.out.println("List of Ages");
for(int z=0;z<i;z++)
{
System.out.println(ages[z]);
}

}
}

Since you are modifying the original list you will need to make a copy of it.

Then to print the original ages use the copied version.

Do i make a copy of the original list using a for loop inside the do-while loop or outside? I tried to do it earlier, but I was unsuccessful.

There is a couple of ways to do it.

First would be iterating though the original list and placing the same values in the new list:

int[] a = new int[100]; //the array to modify
....
int[] orig = new int[a.length];
for(int i = 0; i < a.length; i++)
{
    orig[i] = a[i];
}
....

The other way it to use the System.arrayCopy method

int[] a = new int[100];
....
int[] orig = new int[a.length];

//arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
System.arraycopy(a, 0, orig, 0, a.length);

Hope that Helps

I'm going to try the first technique that you did and copy the original values. Do I print out copy inside the do while loop or outside of it for it to be successful?

There is a couple of ways to do it.

First would be iterating though the original list and placing the same values in the new list:

int[] a = new int[100]; //the array to modify
....
int[] orig = new int[a.length];
for(int i = 0; i < a.length; i++)
{
    orig[i] = a[i];
}
....

The other way it to use the System.arrayCopy method

int[] a = new int[100];
....
int[] orig = new int[a.length];

//arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
System.arraycopy(a, 0, orig, 0, a.length);

Hope that Helps

Where I placed the comments are my attempt, but I was unsuccessful. Do you mind helping me some more? I can't believe I'm having a hard time printing out a copy of the list!

import java.io.* ;

class BagofAges 
{

public static void main(String args[]) throws IOException
{

DataInputStream in=new DataInputStream(System.in);
int ages[]=new int[100];
int i=0,k=0,kcount=0;
int copy[]=new int[100];


do
 {
  System.out.print("enter the age of your next family member or -1 to terminate:" );
  k= Integer.parseInt(in.readLine());
 
// for (i=0;i<copy.length;i++)
 //{
 //ages[k] = copy[k];
 //}
  

 
if(k==10)
{ 
  kcount++; //counts the no of occurrence of age 10 and do not add it to the ages[] array
} 
else if (k!=-1) //saves all values in ages[] except for -1 and 10
{
  ages[i++] =k;
}
}
while (k!=-1);


//System.out.println("List of Ages");
//for(i = 0; i < copy.length; i++)
//{   
 //System.out.println(copy[i]);
//}

System.out.println("Number of age 10 occurrence:"+kcount);

System.out.println("List of Ages");
for(int z=0;z<i;z++)
{
  System.out.println(ages[z]);
}

}
}

Here is what I did

class BagofAges {

    public static void main(String args[]) throws IOException {

        DataInputStream in = new DataInputStream(System.in);
        int ages[] = new int[100];
        int i = 0, k = 0, kcount = 0;
        int copy[] = new int[ages.length]; //Make this ages.length that way you only have to change it once


        do {
            System.out.print("enter the age of your next family member or -1 to terminate:");
            k = Integer.parseInt(in.readLine());

            if (k == 10) {
                kcount++; //counts the no of occurrence of age 10 and do not add it to the ages[] array
            } else if (k != -1) //saves all values in ages[] except for -1 and 10
            {
                ages[i++] = k;
            }
        }
        while (k != -1);

        //-----------------------------
        //       MAKE A COPY
        //-----------------------------
        for (i = 0; i < copy.length; i++) {
            copy[i] = ages[i];
        }


        //-----------------------------
        //       LIST ORIGINAL
        //-----------------------------
        System.out.println("ORIGINAL LIST:");
        for(int z = 0; z < copy.length; z++) {
            System.out.print(copy[i]);
            if(z != copy.length-1) System.out.print(", "); //print a comma unless its the last one
        }
        System.out.println();//blank line;



        System.out.println("Number of age 10 occurrence:" + kcount);

        System.out.println("List of Ages");
        for (int z = 0; z < i; z++) {
            System.out.println(ages[z]);
        }

    }
}

Also note when you are looping through. Unless you have a full list (100 items) you are going to get an ArrayIndexOutOfBoundsException when trying to print out the values.

When I receive the "ArrayIndexOutOfBoundsException", do I need to do a try/catch?

That would be best, otherwise you program will die.:ooh:

The code above will still not print the actual original list if it contains 10s because you are never adding the 10s to the array at all.

You need to add every number to the array. Then loop and print it.
Then count your 10s and print the count.
Then print the list, skipping the entries that are 10 - your instructions don't say that you actually have to remove the values, just don't print them. If you do want to remove the 10s, set the element to 0 or -1 or something.

Do I use each printing method inside of my do-while loop for this to work or should I continue printing outside of the do-while loop?

Thanks for your help guys, I finally figured it out.

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.