Link to home
Start Free TrialLog in
Avatar of hugonieto
hugonietoFlag for United States of America

asked on

Counting Families with an ArrayList

Hi guys!

It's me AGAIN!! I'm writing this program to keep practicing Java! but I need some help!! the code is supposed to use an arrayList (don't want to use an array.... I want to do it just with an arrayList) to ask for a number of families and then ask for the income of each family. After this I need to ask them if they want to continue adding for families. I have to compute the maximum income of all incomes entered. Then I need to count the families who make less than 10 percent of the maximum income. MAN! This book is picky!!!! This is what I have so far( BELOW ) The problems are:

1. When I ask them if they want to continue...... it doesn't even let me enter yes or no!!
2. The result from calculating the percentage is not right..... it outputs the number of families entered and it kind of duplicates the calculated incomes..

I hope you guys can give a hand with this!

Thanks!



CODE


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




public class countfamilies
 {
      public static void main(String[] args)
       {
      
            Scanner k = new Scanner(System.in);
            Integer numFamilies =0, maximum = 0, less = 0, input=0;
         boolean done = false;
            
         ArrayList<Integer> income = new ArrayList<Integer>(numFamilies);
      
            System.out.println("Enter number of families:");
            numFamilies = k.nextInt();
            income.add(numFamilies);

                   
  while(!done)
      {

            for(int countF = 0; countF < numFamilies; countF++)
      {
      
              
                      System.out.println("Enter income");
                  input = k.nextInt();
                  income.add(input);
                         


                      
                  income.add(input);    
                        
      }
        
    System.out.println();
        System.out.println("Do you want to continue adding more families?");
       String answer = k.nextLine();
       System.out.println();

      

      if(!answer.equalsIgnoreCase("yes"))
            
            done = true;
            
      }
      
      
      
      for(int position = 0; position < income.size(); position++)
      {
             int i = income.get(position);
            if(i > maximum)
            {
                  maximum = i;
            }
            
      }
      System.out.println("TOTAL Maximum:" + maximum +'\n');
      
      double porcentage = maximum * .1;
      
      
      System.out.println("The number of families making less than 10% of the maximum are:" );
      
      for(int count=0; count < income.size(); count++)
      {
            if(income.get(count) < porcentage)
            {
                  System.out.println(income.get(count));
                  //less++;
            }
            
      }
      
            
      }
}



OUTPUT


 ----jGRASP exec: java countfamilies

Enter number of families:
4
Enter income
6666
Enter income
22200
Enter income
400000
Enter income
655

Do you want to continue adding more families?

TOTAL Maximum:400000

The number of families making less than 10% of the maximum are:
4
6666
6666
22200
22200
655
655

 ----jGRASP: operation complete.
ASKER CERTIFIED SOLUTION
Avatar of mrcoffee365
mrcoffee365
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hugonieto

ASKER

I GOT it!!! Thanks guys!!! here is my updated code!!! The only thing now is that instead of entering integers I would like to have double!! that way when I enter for example 205030.30.... it will calculate the double and not just the integer. I'm not sure if with arrayLists it is possible!!







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




public class countfamilies
 {
      public static void main(String[] args)
       {
      
            Scanner k = new Scanner(System.in);
            Integer numFamilies=0, maximum = 0, incomeF=0;
         String answer;
            
      
        ArrayList<Integer> income = new ArrayList<Integer>(incomeF);
      
            
                   
  do
  {
      
         System.out.println("Enter number of families:");
            numFamilies = k.nextInt();


      
            for(int count = 0; count < numFamilies; count++)
           {
              
                      System.out.println("Enter income");
                  incomeF = k.nextInt();
                  income.add(incomeF);
                  
           }    
        
               System.out.println();
             System.out.println("Do you want to continue adding more families?");
            answer = k.next();
            System.out.println();
                  
            
      }while(answer.equalsIgnoreCase("yes"));
      
      
      
      for(int position = 0; position < income.size(); position++)
      {
             int i = income.get(position);
            if(i > maximum)
            {
                  maximum = i;
            }
            
      }
      System.out.println("TOTAL Maximum:" + maximum +'\n');
      
      int porcentage = (int)(maximum * .1);
      
      
      System.out.println("The incomes being less than 10% of the maximum are:" );
      
      for(int count=0; count < income.size(); count++)
      {
      
            if(income.get(count) < porcentage)
            {
                  System.out.println(income.get(count));
                  
            }
            
            
      }
      
}            
      
}
You're welcome.

Did you try using Double instead of Integer?  An arraylist can be of any object type.
I did but it gives me this error!!





countfamilies.java:19: error: no suitable constructor found for ArrayList(Double)
        ArrayList<Double> income = new ArrayList<Double>(incomeF);
                                   ^
    constructor ArrayList.ArrayList(Collection<? extends Double>) is not applicable
      (actual argument Double cannot be converted to Collection<? extends Double> by method invocation conversion)
    constructor ArrayList.ArrayList() is not applicable
      (actual and formal argument lists differ in length)
    constructor ArrayList.ArrayList(int) is not applicable
      (actual argument Double cannot be converted to int by method invocation conversion)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial