-4

My questions follow like this, there is an N number of people. considering N=9, I have to find Taxable income of those people. I've done the math for 1 employee but, applying it for other 8 people is too much of repeated code. Can I put IF statements inside a FOR loop? I've tried this but it shows an error at FOR-loop(i.e, variable N is already defined in method main(String[]))

public class IncomeTax {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int i,Tax = 0,N = 1;
        System.out.print("Enter the Taxable Income of the Employee " +N+":");
        i = input.nextInt();
        for( int N=1  ;N<=9 ; N++ ){
        if( i >= 0 & i<18200)
            Tax = 0;
        if( i >= 18201 & i<37000)
            Tax = (int) (( i - 18200) * 0.19);
        if( i >= 37001 & i<87000)
            Tax = (int) ((( i - 37000) * 0.325)+3572);
        if( i >= 87001 & i<180000)
            Tax = (int) ((( i - 18200) * 0.37)+19822);
        if( i >= 180001 )
            Tax = (int) ((( i - 18200) * 0.45)+54097);
        System.out.println("The Income Tax for the employee "+N+" is " + Tax);
        }    
    }
}

The output should be of N=9, number of employees and their taxes respectively in order. Enter the Taxable Income of the Employee 1: The Income Tax for employee 1: Enter the Taxable Income of the Employee 2: The Income Tax for employee 2 : Enter the Taxable Income of the Employee 3: The Income Tax for employee 3:

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • I think you want the "print" and "input.nextInt" just *inside* the for loop, to ask the income of each of those 9 employees – Hans Kesting Jul 30 '19 at 15:12
  • Try using an `else if` from the 2nd if onward. Then you can remove one condition (but add a "< 0" test first) – Hans Kesting Jul 30 '19 at 15:14
  • You have defined N already. Once in this line `int i,Tax = 0,N = 1;` and once in your for loop `for( int N=1 ;N<=9 ; N++ ){` Remove the definition from your for loop `for(N; N <=9; N++) {` – Rooben Jul 30 '19 at 15:15
  • 1
    also it is better to move all the ifs in the for loop to a private method and call it in the loop. – PeterHe Jul 30 '19 at 15:16
  • Also don't capitalize variables unless they are final... also try to use better variable names, n isn't very descriptive – RobOhRob Jul 30 '19 at 15:28
  • Possible duplicate of [JavaScript closure inside loops – simple practical example](https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – BIPIN CHANDRA Aug 18 '19 at 02:52
  • @BIPINCHANDRA If you don't want the question anymore, **delete** it by clicking the `delete` link, don't replace it with garbage. Question text restored!! – Andreas Aug 18 '19 at 03:01
  • How can a JavaScript question be a duplicate of a Java question? – Mark Rotteveel Aug 18 '19 at 07:02

2 Answers2

0

You should to paste your input.nextInt() code inside for loop. And remove N variable from int i,Tax = 0,N = 1; declaration. Because you already declared it in for loop.

Solution:

public class IncomeTax {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int i,Tax = 0;
        for( int N=1; N<=9; N++ ){
        System.out.print("Enter the Taxable Income of the Employee " +N+":");
        i = input.nextInt();
        if( i >= 0 & i<18200)
            Tax = 0;
        if( i >= 18201 & i<37000)
            Tax = (int) (( i - 18200) * 0.19);
        if( i >= 37001 & i<87000)
            Tax = (int) ((( i - 37000) * 0.325)+3572);
        if( i >= 87001 & i<180000)
            Tax = (int) ((( i - 18200) * 0.37)+19822);
        if( i >= 180001 )
            Tax = (int) ((( i - 18200) * 0.45)+54097);
        System.out.println("The Income Tax for the employee "+N+" is " + Tax);
        }    
    }
}
Anton Petrov
  • 316
  • 2
  • 10
0

You can absolutely use if statements in a for loop - you're getting an error because you're re-declaring your variable N inside your for loop. I would recommend either using a different variable letter, or not declaring it at all below your Scanner.

/**
 * @param args the command line arguments
 */

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int i,Tax = 0;
    System.out.print("Enter the Taxable Income of the Employee " +N+":");
    i = input.nextInt();
    for( int N=1  ;N<=9 ; N++ ){
    if( i >= 0 & i<18200)
        Tax = 0;
    if( i >= 18201 & i<37000)
        Tax = (int) (( i - 18200) * 0.19);
    if( i >= 37001 & i<87000)
        Tax = (int) ((( i - 37000) * 0.325)+3572);
    if( i >= 87001 & i<180000)
        Tax = (int) ((( i - 18200) * 0.37)+19822);
    if( i >= 180001 )
        Tax = (int) ((( i - 18200) * 0.45)+54097);
    System.out.println("The Income Tax for the employee "+N+" is " + Tax);
    }    
}
nldoty
  • 471
  • 3
  • 8