hugonieto
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>(numFami lies);
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.equalsIgnoreCas e("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.
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>(numFami
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.equalsIgnoreCas
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.
//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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You're welcome.
Did you try using Double instead of Integer? An arraylist can be of any object type.
Did you try using Double instead of Integer? An arraylist can be of any object type.
ASKER
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(Collec tion<? 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)
countfamilies.java:19: error: no suitable constructor found for ArrayList(Double)
ArrayList<Double> income = new ArrayList<Double>(incomeF)
^
constructor ArrayList.ArrayList(Collec
(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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.equalsIgnore
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.
}
}
}
}