3 Replies - 877 Views - Last Post: 24 November 2013 - 07:27 PM Rate Topic: -----

#1 Kinja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 15-October 12

Glitch in code in which it is repeating something it should not be

Posted 24 November 2013 - 07:09 PM

So this is the code I have:
/**

import java.util.Scanner;

public class Income
{
    public static void main(String[] args) 
    {
        Scanner k = new Scanner(System.in);//k is the number of families being evaluated
               
        System.out.print("Please enter The number of families being evaluated: ");
        int countFamilies = k.nextInt();
        
        int[] net = new int[countFamilies]; //collect net incomes of families
        
        for(int V=0; V < countFamilies; V++)//count down from total k entered. accept a value into the array
            {
                System.out.print("Please enter a net income for a family: ");
                net[V] = k.nextInt();//create an arrany, put each value entered in it
            }

        int largestNet = 0;//highest income entered
        
        for (int v = 0; v < net.length; v++) //find and set largest net income value entered
            {
                int largestEntered= net[v];

                if(largestEntered > largestNet) 
                    {
                        largestNet = largestEntered;
                    }
            }
        
        int netBar = (int)(largestNet - (largestNet*0.10)); //calculate the highest income less 10 percent
        int families = 0;//number of families below netBar

        for (int below = 0; below < net.length; below++)
            {
                if(net[below] < netBar)
                    {
                        System.out.printf("Those families with an income below the highest"
                                + " 10 percent of the maximum net income are/is: %s", net[below]);
                        families++;
                    }
            }

        System.out.printf("\nThere is a total of %s families with a net income less than 10"
                + " percent of the highest net income.\n", families);        
    
}
}



When i try the program this is what I get:
Please enter The number of families being evaluated: 5
Please enter a net income for a family: 120
Please enter a net income for a family: 26
Please enter a net income for a family: 60
Please enter a net income for a family: 80
Please enter a net income for a family: 91
Those families with an income below the highest 10 percent of the maximum net income are/is: 26Those families with an income below the highest 10 percent of the maximum net income are/is: 60Those families with an income below the highest 10 percent of the maximum net income are/is: 80Those families with an income below the highest 10 percent of the maximum net income are/is: 91
There is a total of 4 families with a net income less than 10 percent of the highest net income.
BUILD SUCCESSFUL (total time: 11 seconds)

I don't want that sentence repeated but rather to print once and state the values below it.
HELP!

Is This A Good Question/Topic? 0
  • +

Replies To: Glitch in code in which it is repeating something it should not be

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10471
  • View blog
  • Posts: 38,809
  • Joined: 27-December 08

Re: Glitch in code in which it is repeating something it should not be

Posted 24 November 2013 - 07:16 PM

Moved to Java.

Please do not post programming questions in the Student Campus.
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7645
  • View blog
  • Posts: 12,898
  • Joined: 19-March 11

Re: Glitch in code in which it is repeating something it should not be

Posted 24 November 2013 - 07:17 PM

You have an instruction in a loop. That means it's going to execute according to the terms of the loop. If you don't want it to execute repeatedly, take it out of the loop.

I would guess that the println instruction that appears in the loop is one you could just get rid of, since you have an instruction below that which will print out the data you accumulate.

This post has been edited by jon.kiparsky: 24 November 2013 - 07:20 PM

Was This Post Helpful? 0
  • +
  • -

#4 Kinja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 15-October 12

Re: Glitch in code in which it is repeating something it should not be

Posted 24 November 2013 - 07:27 PM

sorry. thanks. i forgot where to post.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1