?
Solved

Counting Families with an ArrayList

Posted on 2012-09-08
5
Medium Priority
?
1,163 Views
Last Modified: 2012-09-28
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.
0
Comment
Question by:hugonieto
  • 3
  • 2
5 Comments
 
LVL 27

Accepted Solution

by:
mrcoffee365 earned 2000 total points
ID: 38379763
This part:

if(!answer.equalsIgnoreCase("yes"))

            done = true;

makes done=true, which bounces you out of your while loop.  You need to check for more conditions if you want it to stay in the loop.
0
 

Author Comment

by:hugonieto
ID: 38379987
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));
                  
            }
            
            
      }
      
}            
      
}
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 38380225
You're welcome.

Did you try using Double instead of Integer?  An arraylist can be of any object type.
0
 

Author Comment

by:hugonieto
ID: 38380854
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)
0
 
LVL 27

Assisted Solution

by:mrcoffee365
mrcoffee365 earned 2000 total points
ID: 38380976
If incomeF is not of type Double you can't add it to an ArrayList<Double> .  Convert incomeF to a Double or create a new variable with the type Double and use that to create your ArrayList.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses
Course of the Month16 days, 12 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question