Ok, well Iam just about done with one of my assignments, but Iam having a problem at the end of it. Iam suppose to find a maximum income of the families income that the user inputs and I have to find the families that make less than 10% of the maximum income. Can anyone help?

Heres what Iam suppose to do,

"Find the maximum income of all the values entered, and print this value to the screen.

Then count the families that make less than 10% of the maximum income. Display the income of each of these families, then display the count."

Heres an example,

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

The maximum income is: 109000

The incomes of families making less than 10% of the maximum are:
8500
9000
for a total of 2 families


And Heres my code so far,

import java.util.*;

public class CountFamilies {

	public static void main(String[] args) {
	
		Scanner kbd = new Scanner(System.in);
		int numOfFamilies = 0, maximum = 0;
		
		System.out.println("Enter number of families:");
		numOfFamilies = kbd.nextInt();
		
		long []income = new long [numOfFamilies];
		
		for (int count = 0; count < numOfFamilies; count++)
		{
			System.out.println("Enter an income:");
			income[count]=kbd.nextLong();
		}
			
		if ()
		{
		System.out.println("The maximum income is:" + );
		}
		

	}

}

I know I have to use a If statment to find the maximum, thats why I have a blank If statement in there.

Get a maximum income in java? Probably get really good at it and go work for Oracle?
Sorry, I guess I should read the actual question.


Okay, you've got an array of integers, we'll go from there. Suppose you had a set of index cards with numbers on them, and you had to pick the biggest one. In addition to the index cards, you have a piece of paper and a pencil with an eraser.
What steps would take to make sure that after you'd gone through the cards, you had the largest value written on the paper?
Assume you have long-term memory problems, and the only information you can hang on to is what you write on the paper.
(this is really as easy a question as it sounds)

commented: Good analogy +4

Get a maximum income in java? Probably get really good at it and go work for Oracle?
Sorry, I guess I should read the actual question.


Okay, you've got an array of integers, we'll go from there. Suppose you had a set of index cards with numbers on them, and you had to pick the biggest one. In addition to the index cards, you have a piece of paper and a pencil with an eraser.
What steps would take to make sure that after you'd gone through the cards, you had the largest value written on the paper?
Assume you have long-term memory problems, and the only information you can hang on to is what you write on the paper.
(this is really as easy a question as it sounds)

How would I put the array in the maximum?

How would I put the array in the maximum?

Not sure what that means?
Step away from the code for a minute. You've got a bunch of index cards and a pencil. How do you find the largest value on the cards?

I didn't plcae the comments in the program because it become too leangthy

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class MaxIncome {
	public static void main(String[] args)throws Exception {
        System.out.println("Enter the number of families");
        InputStreamReader is=new InputStreamReader(System.in);
        BufferedReader buffReader=new BufferedReader(is);
		int nooffam=Integer.parseInt(buffReader.readLine());
		long[] fam=new long[nooffam];
        for(int i=0;i<nooffam;i++)
        {
        	System.out.println("Enter the incomes for "+(i+1)+" family");
        	fam[i]=Long.parseLong(buffReader.readLine());
        }
        long max=fam[0];
        for(int i=1;i<nooffam;i++)
        {
        	if(max>fam[i])
        	{
        		break;
        	}
        	else
        	{
        		max=fam[i];
        	}
        }
        System.out.println("The maximum income is "+max);
        int less=0;
        System.out.println("the income is less than 10%of max are");
        for(int i=0;i<nooffam;i++)
        {
        	if(fam[i]<=10*max/100)
        	{
        		less=less+1;
                System.out.println(fam[i]);
        	}
        }
        if(less==0)
        {
            System.out.println("NILL");
        }
        else
        {
            System.out.println("for "+less+" families");
        }
        }
	}
commented: You are not helping people to learn, you are helping them to cheat with their homeworks and fail -3
commented: Give help, not code, "dude" +0

Hey, Here is my simple solution ..

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner S = new Scanner(System.in);
        System.out.println("Enter The Number Of Families : ");
        // get the size of array .
        int NumberOfFamilies = S.nextInt();
        // build it
        int[] Incomes = new int[NumberOfFamilies];
        // fill it .
        for(int i=0; i<NumberOfFamilies; i++){
            System.out.println("Income : ");
            Incomes[i] = S.nextInt();
        }
        // 1.0 calculate the max.
        int max = 0;
        for (int i = 0; i < Incomes.length; i++) {
            int j = Incomes[i];
            if(j > max){
                max = j;
            }
        }
        // 2.0 Get Incomes With 10 percent less than the max.
        // 2.1 How Much is 10% of max ?
        int theTenPercent = (int)(max*0.10);
        
        // search for families with an income less than the above.
        int counter = 0;
        for (int i = 0; i < Incomes.length; i++) {
            if(Incomes[i]< theTenPercent){
                System.out.println("* Result : "+Incomes[i]);
                counter++;
            }
        }
        // print 
        System.out.println("Total of : "+counter+" Families.");
    }
}
commented: Give help, not code +0

Ok this is what I have now, now I need to get the lower 10% of the families. Help?

Heres my code,

import java.util.*;

public class CountFamilies {

	public static void main(String[] args) {
	
		Scanner kbd = new Scanner(System.in);
		int numOfFamilies = 0, maximum = 0;
		
		System.out.println("Enter number of families:");
		numOfFamilies = kbd.nextInt();
		
		long []income = new long [numOfFamilies];
		
		for (int count = 0; count < numOfFamilies; count++)
		{
			System.out.println("Enter an income:");
			income[count]=kbd.nextLong();
		
		if (income[count] > maximum)
		{
		income[count] = maximum;
		}
		}
		System.out.print("Your maximum income is" + " " + maximum);
	
		
	}

}

Getting the families that make less than 10% of the maximum: calculate the threshold (maxiumum income/10, that'll work for now - there's a bit of loss of precision, but you don't want to get distracted with number representation here) and, for each income that you've taken as input, check to see if it's below that.

Okay, play's to you. Let's have some code.

Ok I got the maximum part done, but now i cant get the 10% part. Help?

Heres my code,

import java.util.*;

public class CountFamilies {

	public static void main(String[] args) {
	
		Scanner kbd = new Scanner(System.in);
		int numOfFamilies = 0, maximum = 0, less = 0;
		double tenPer = maximum * .1;
		
		System.out.println("Enter number of families:");
		numOfFamilies = kbd.nextInt();
		
		long []income = new long [numOfFamilies];
		
		for (int count = 0; count < numOfFamilies; 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 > maximum)
			{
				maximum = i;
			}
		}


                  for (int count = 0; count < income.length; count++) 
		{            
			if(income[count]< tenPer)
			{		

                            }   
                  
                  System.out.print("Your maximum income is" + " " + maximum);

What goes inside the 10% if statement?

Unless you want to save it to work with later (don't see why you'd need that now), write it to the screen. Maybe with a little header (before the loop) to tell you that this is the list of low-income families.

Ok I made it so you can tell which if statement is for the low income families or the under 10% families.

Here,

import java.util.*;

public class CountFamilies {

	public static void main(String[] args) {
	
		Scanner kbd = new Scanner(System.in);
		int numOfFamilies = 0, maximum = 0, less = 0;
		double tenPer = maximum * .1;
		
		System.out.println("Enter number of families:");
		numOfFamilies = kbd.nextInt();
		
		long []income = new long [numOfFamilies];
		
		for (int count = 0; count < numOfFamilies; 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 > maximum)
			{
				maximum = i;
			}
		}
		
                  //this is for the low income families
		for (int count = 0; count < income.length; count++) 
		{            
			if(income[count]< tenPer)
			{
          
                            }

So, what do I put in the low income families if statement?

Like I say, you want to write the values that fall under that 10% mark to the screen. "This looks like a job for... System.out.println()!"

Ok, heres my code now where it shows where the low income families are at or the 10% low families. Help?

import java.util.*;

public class CountFamilies {

	public static void main(String[] args) {
	
		Scanner kbd = new Scanner(System.in);
		int numOfFamilies = 0, maximum = 0, less = 0;
		double tenPer = maximum * .1;
		
		System.out.println("Enter number of families:");
		numOfFamilies = kbd.nextInt();
		
		long []income = new long [numOfFamilies];
		
		for (int count = 0; count < numOfFamilies; 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 > maximum)
			{
				maximum = i;
			}
		}
		//this is for the low income families
		for (int count = 0; count < income.length; count++) 
		{            
			if(income[count]< tenPer)
			{

What goes in the low income families if statement?

while (question == previous question)
  answer = previous answer;

As jon.kiparsky said - if you want to show the user output you need to use the System.out.println() method as you have used in your code in other places. You need to show the families with low income - use the System.out.println() method in the if clause. Inside the if clause you display the income of those families, you also need to keep a count of how many families there are - you can use a variable and increment it each time you enter the if clause. Try to implement it and we will go from there.

Ok I have figured it out, but now the anwser comes out like this,

"Your maximum income is 61000

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

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

Total of: 2 Families."


And I want it to be like this,

"Your maximum income is 61000

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

Total of: 2 Families."

Heres my code,

import java.util.*;

public class CountFamilies {

	public static void main(String[] args) {
	
		Scanner kbd = new Scanner(System.in);
		int numOfFamilies = 0, maximum = 0, less = 0;
	
		
		System.out.println("Enter number of families:");
		numOfFamilies = kbd.nextInt();
		
		long []income = new long [numOfFamilies];
		
		for (int count = 0; count < numOfFamilies; 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 > maximum)
			{
				maximum = i;
			}
		}
		
		System.out.println("Your maximum income is" + " " + maximum + '\n');
		
		int tenPer = (int) (maximum * .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: " + income[count] + '\n');
				less++;
			}
		}
		
		
		System.out.println("Total of: " + less + " Families.");

		
	}

}

Can anyone help?

Have you ever considered doing something other than programming? It would probably be a good career move.

You're printing the header ("The incomes of families...") IN THE SAME INSTRUCTION that prints the income itself. So, why do you think it's printing a header each time it prints an income?

Move the header outside of the for loop.

System.out.println("The incomes of families making less than 10% of the maximum are: ");

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

There, done.

Have you ever considered doing something other than programming? It would probably be a good career move.

You're printing the header ("The incomes of families...") IN THE SAME INSTRUCTION that prints the income itself. So, why do you think it's printing a header each time it prints an income?

Move the header outside of the for loop.

System.out.println("The incomes of families making less than 10% of the maximum are: ");

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

There, done.

Wow thanks man, and I am actually thinking about changing majors. Iam in college now, so Iam looking for another majors now, but I still have to try to finish the Java class though.

Could u maybe help me out on my other program?

Heres the URL,

http://www.daniweb.com/forums/thread326444.html

Thanks again man

Yes, change majors. From what I'm seeing here, you're not cut out for CS. Looking at the other program, I'm pretty sure that's right. No offense meant, but if I have to give you that code, you're probably wasting your time.

On the other program, I will, as always, offer advice on code under way, but you've got to do your part. You just can't expect people to write your homework for you.

jon.kiparsky - Ever tried. Ever failed. No matter. Try again. Fail again. Fail better :)

DallasFan3 - if you want to be a programmer, then stay in the current major. I don't believe in that that some people are cut or not cut to be programmers. If you want it, if you like it, you will get better - if you are not a natural, it will just be harder but possible. I have friends that were horrible in their freshman year, but now having fun (and money) in development positions.

apines - Touché. Time will tell.

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.