Ok so my problem should have a fairly simple fix. My program takes the number of families and their income and prints the families that make less than 10% of the maximum income. Below is a sample run.


Please enter the number of families:
5
Enter an income:
8500
Enter an income:
109000
Enter an income:
49000
Enter an income:
9000
Enter an income:
67000
Your maximum income is 109000

The incomes of families making less than 10% of the maximum are: 8500

The incomes of families making less than 10% of the maximum are: 9000

Total of: 2 Families.

What it should look like

Your maximum income is 109000

The incomes of families making less than 10% of the maximum are:
8500
9000
Total of: 2 Families.

Code:

//Dalton Kennedy

import java.util.*;

public class CountFamilies {

	public static void main(String[] args) {

		Scanner kbd = new Scanner(System.in);
		int numberOfFamilies = 0;
		int max = 0, less = 0;


		System.out.println("Please enter the number of families:");
		numberOfFamilies = kbd.nextInt();

		long [] income = new long [numberOfFamilies];

		for (int count = 0; count < numberOfFamilies; count++)
		{
			System.out.println("Enter an income:");
			income[count]=kbd.nextLong();
  		}

		for (int count = 0; count < income.length; count++)
		{
			int i = (int) income[count];
			if (i > max)
			{
				max = i;
			}
		}

		System.out.println("Your maximum income is" + " " + max + '\n');

		int tenPer = (int) (max * .1);

		for (int count = 0; count < income.length; count++) 
		{            
			if(income[count]< tenPer)
			{
				System.out.println("The incomes of families making less than 10% of the maximum are: " + '\n');
				less++;
			}
		}
		

		System.out.println("Total of: " + less + " Families.");


	}

}

Recommended Answers

All 2 Replies

by this line alone:
System.out.println("The incomes of families making less than 10% of the maximum are: " + '\n');

I assume you either didn't test your code, or this is not the code you ran.

print the text
"The incomes of families making less than 10%"
before the for-loop
and the actual income within the loop.

that 'll clear up one of your issues.

There is no real problem with your code, you just need to rethink what to do when.

Yeah sorry left out my array in that print statement, i fixed it, thanks for the help!

//Dalton Kennedy

import java.util.*;

public class CountFamilies {

	public static void main(String[] args) {

		Scanner kbd = new Scanner(System.in);
		int numberOfFamilies = 0;
		int max = 0, less = 0;


		System.out.println("Please enter the number of families:");
		numberOfFamilies = kbd.nextInt();

		long [] income = new long [numberOfFamilies];

		for (int count = 0; count < numberOfFamilies; count++)
		{
			System.out.println("Enter an income:");
			income[count]=kbd.nextLong();
  		}

		for (int count = 0; count < income.length; count++)
		{
			int i = (int) income[count];
			if (i > max)
			{
				max = i;
			}
		}

		System.out.println("Your maximum income is" + " " + max + '\n');

		System.out.println("The incomes of families making less than 10% of the maximum are:");
		int tenPer = (int) (max * .1);

		for (int count = 0; count < income.length; count++) 
		{            
			if(income[count]< tenPer)
			{
				System.out.println("" + income[count]);
				less++;
			}
		}


		System.out.println("Total of: " + less + " Families.");


	}

}
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.