insert IF statements inside For loop - java

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:

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);
}
}

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);
}
}
}

Related

Getting error while creating bmi calculator

Im creating a bmi caculator and im using the height and weight formula. I keep getting an erorr that says nan obese at the very end and i dont know what i'm missing or what i need to edit. Any help would be appreciated.
Output error:
Enter Weight in pounds: 150
Enter height (ft.):
6
Enter height (in.):
7
Your BMI is: NaN
Obese
BMI formula:
BMI = (703 * weightInPounds) / heightInInches^2
Code:
import java.util.Scanner;
public class Bmi_Calc {
public static void main(String[] args) {
// TODO Auto-generated method st
Scanner input = new Scanner(System.in);
//double weight;
double weightInPounds;
int feet;
int inches;
//int weightInPounds;
int heightInInches;
double height;
System.out.print("Enter Weight in pounds: ");
double weight = input.nextDouble();
System.out.println("Enter height (ft.): ");
feet = input.nextInt();
System.out.println("Enter height (in.): ");
inches = input.nextInt();
weightInPounds = 0;
heightInInches = 0;
double bmi = (703 * weightInPounds) / Math.pow(heightInInches, 2.0);
double heightMeters = ((feet * 12) + inches) * .0254;
System.out.println("Your BMI is: " + bmi);
if (bmi < 18.5) {
System.out.println("Underweight.");
}
if ((bmi >= 18.5) && (bmi < 24.9)) {
System.out.println("Normal weight");
}
if ((bmi >= 25) && (bmi < 29.9)) {
System.out.println("Overwight");
}
else {
System.out.println("Obese");
}
input.close();
}
}
Check your bmi calculation numbers. That will tell you why you are getting NAN.
weightInPounds = 0;
heightInInches = 0;
double bmi = (703 * weightInPounds) / Math.pow(heightInInches, 2.0);
In above weightInPounds and heightInInches are zero so bmi = 0/0 which is causing NaN
Thanks

One input doesn't work, while the rest does

The user must input the total purchase amount and how old they are, and then calculate the final payment.
If the total amount is $100 or over, there is a 20% discount off the total price. If the age is 65 or older, there is a 10% discount off the total price.
double discount1 = 0.10;
double discount2 = 0.20;
double totalPrice = 0.0;
double finalPrice = 0.0;
System.out.print("Enter total amount: ");
double purchase = input.nextDouble();
System.out.print("Enter age: ");
int age = input.nextInt();
if (purchase >= 100) {
totalPrice = purchase * discount2;
finalPrice = purchase - totalPrice;
System.out.print("The final amount is $" + finalPrice);
}
else if (purchase < 100 && age < 65) {
System.out.println("The final amount is $" + purchase);
}
else if (age >= 65) {
totalPrice = purchase * discount1;
finalPrice = purchase - totalPrice;
System.out.print("The final amount is $" + finalPrice);
}
The user would input 200 as the total amount and 75 as the age. The output is supposed to be $140.00. However, I receive the output as $160.00.
The first if statement will be executed first. Because the price is above 100. So the other statements will not be executed. Try to change the if expressions because of thats the problem why it's not giving the result you may expect
My approach would be to add all of the discounts together and then multiply once at the end.
Than you can add other discounts if needed
double totalDiscount = 0.0;
if (purchase >= 100) {
totalDiscount += discount2;
}
if (age >= 65) {
totalDiscount += discount1;
}
totalPrice = purchase * (1.0 - totalDiscount);
System.out.print("The final amount is $" + totalPrice);
You nee to change below code,
because when ever price is more then 100 it will run first if block and wont enter into last block.
so change it in below manner :-
if (purchase >= 100 && age < 65) {
totalPrice = purchase * discount2;
finalPrice = purchase - totalPrice;
System.out.print("The final amount is $" + finalPrice);
}
else if (purchase < 100 && age < 65) {
System.out.println("The final amount is $" + purchase);
}
else if (purchase < 100 &&age >= 65) {
totalPrice = purchase * discount1;
finalPrice = purchase - totalPrice;
System.out.print("The final amount is $" + finalPrice);
}
else if (age >= 65) {
totalPrice1 = purchase * discount2;
totalPrice = purchase * discount1;
finalPrice = purchase - totalPrice - totalPrice1 ;
System.out.print("The final amount is $" + finalPrice);
}

The program doesn't reassign the value in my if else statment

I am very new to Java and my assignment was to make a tax calculator. Everything seems to be working fine but when I print at the end it always prints 0 for federaltaxes. I make the double at the start and then try to reassign it depending on the situation through the else if statement.
//2353161 Tomas Pospisil
import java.util.Scanner;
public class PaymentCalculator {
public static void main(String[] args){
//Create Scanner for user input
Scanner input = new Scanner(System.in);
//Ask the user for Gross Pay
System.out.print("Please Enter Gross Pay: ");
double GrossPay = input.nextInt();
//Establish the integer for Number of Exemptions
int NoE = 0;
//Establish the integer for the value of exemption
double exemptionvalue = 0;
double federaltaxes = 0;
//Ask the user if they are married
System.out.print("Are you married: ");
String marriedstatus = input.next();
//If yes make the number of Exemptions equal the user input
if (marriedstatus.equals("yes")) {
System.out.print("Please enter Number of Exemptions: ");
NoE = input.nextInt();
}
//If not make the number of exemptions equal 1
else if(marriedstatus.equals("no")) {
NoE = 1;
}
//If NoE is greater than 4 make it 5 for later use
if (NoE >4) {
NoE = 5;
}
// Depending on the number of exemptions the program assigns it a value for later use
switch (NoE) {
case 1:
exemptionvalue = 1000;
break;
case 2:
exemptionvalue = 1800;
break;
case 3:
exemptionvalue = 2400;
break;
case 4:
exemptionvalue = 3600;
break;
case (5):
exemptionvalue = 4000;
break;
}
//Calculate adjusted wages
double adjustedwages = GrossPay - exemptionvalue;
// Figure out the tax percentage
if (GrossPay >= 10000) {
federaltaxes = GrossPay * 0;
}
else if (GrossPay <= 30000 && GrossPay >= 10000) {
federaltaxes = GrossPay * 0.15 ;
}
else if (GrossPay > 30000) {
federaltaxes = GrossPay * 0.20 ;
}
//Program calculates the FICA
double FICA = (GrossPay * .065);
//Program calculates Medicare
double Medicare = (GrossPay * .014);
//Program calculates the federal taxes
//Program Calculates Net Pay
double NetPay = GrossPay - (FICA + Medicare + federaltaxes);
//Program prints the results
System.out.println(" Payroll Taxes");
System.out.println("Gross.....$ " + GrossPay);
System.out.println("Exemptions: " + NoE);
System.out.println("Federal Taxes.....$ " + federaltaxes);
System.out.println(" FICA.....$ " + FICA);
System.out.println(" Medicare.....$ " + Medicare);
System.out.println("-----------------------------");
System.out.println("Net Pay.....$ " + NetPay);
}
}
This is the block that is causing you trouble.
if (GrossPay >= 10000) {
federaltaxes = GrossPay * 0;
}
Any GrossPay amount above 10000 is going to pass the condition and be multiplied by 0.
You have a wrong condition in the if where you evaluate federal taxes, it should be:
if (GrossPay < 10000) {
federaltaxes = GrossPay * 0;
}
else if (GrossPay <= 30000 && GrossPay >= 10000) {
federaltaxes = GrossPay * 0.15 ;
}
else if (GrossPay > 30000) {
federaltaxes = GrossPay * 0.20 ;
}
your code will never evaluate the else condition
It is because your condition logic is wrong
double federaltaxes = 0;
// Figure out the tax percentage
if (GrossPay >= 10000) {
federaltaxes = GrossPay * 0;
} else if (GrossPay <= 30000 && GrossPay >= 10000) {
federaltaxes = GrossPay * 0.15;
} else if (GrossPay > 30000) {
federaltaxes = GrossPay * 0.20;
}
Your first condition will always be true if the Grosspay is >= 10000 so federaltaxes here is 0, then the rest of your if else will not be evaluated because your first condition will always evaluate first. Also you dont have a separate else statement for the rest of the scenarios

Calculating change from 2 inputs by a user

i am trying to output the minimum amount of notes needed from two inputs. one being the cost and the other being the amount handed over by the customer.
so if i enter 400 and then 500, it should say 2 $50 dollars notes.
or if i enter 60 and then 80, it should say 1 $20 dollar note.
this is a small bit of my code:
import java.util.Scanner;
public class bank {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter amount due: ");
int amount = input.nextInt();
System.out.println("Amount tendered: ");
int tmp = input.nextInt();
int change;
if(amount >= 50)
{
change =(tmp - amount)/50;
System.out.println (change + " $50 bills");
}
if(amount >= 20)
{
change =(tmp - amount)/20;
System.out.println (change + " $20 bills");
}
You can achieve it by using mod operator %
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter amount due: ");
int amount = input.nextInt();
System.out.println("Amount tendered: ");
int tmp = input.nextInt();
int change;
int diff = tmp - amount;
if (diff % 50 == 0) {
change = diff / 50;
System.out.println(change + " $50 bills");
}
else if (diff % 20 == 0) {
change = diff / 20;
System.out.println(change + " $20 bills");
}
}
Once you calculate the amount of change in a value of a bill, you need to lower the amount of change required. This is done in the code changeRequired = changeRequired - change;
System.out.println("Amount tendered: ");
int tmp = input.nextInt();
int changeRequired = tmp - amount;
int change;
if(amount >= 50)
{
change =(changeRequired)/50;
changeRequired = changeRequired - change;
System.out.println (change + " $50 bills");
}
if(amount >= 20)
{
change =(changeRequired )/20;
changeRequired = changeRequired - change;
System.out.println (change + " $20 bills");
}
Once you get your change which is the variable amount, you can use the following code to find out how many notes of each of the $50, $20, $10, $5, $1 there are in the change. The number of notes is minimal.
numOf50 = 0; // number of $50 dollar bills, the rest is similar.
numOf20 = 0; // ...
numOf10 = 0; // ...
numOf5 = 0;
numOf1 = 0;
if (amount / 50 > 1){
numOf50 += (amount / 50);
amount %= 50; // update the amount with the remainder
}
else if (amount / 20 > 1){
numOf20 += (amount /20);
amount %= 20;
}
else if (amount / 10 > 1){
numOf10 += (amount / 10);
amount %= 10;
}
else if (amount / 5 > 1){
numOf5 += (amount / 5);
amount %= 5;
}
else if (amount <=5 && amount >0){
numOf1 = amount;
}
// output is trivial, omitted.
Haven't tested on a machine but it should work or give you an idea.

Java change app

So I have Been working on a Program and it has been awhile since I have last used java. I was wondering how to get my program to accept decimals. I have tried looking it up but I couldn't find anything helpful and anything I really understood.Below is what I have done so far...
package test;
import java.util.Scanner;
/**
*
* #author Thao
*/
public class Test {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );
double cash; // double makes it allow decimals
int hundred;
int twenty;
int ten;
int toonies ;
int lonies;
int quarters;
int dimes;
int nickels;
int pennies;
int money;
int change;
System.out.println();// blank line
System.out.print("Hello ");
System.out.println();
System.out.println("Please enter a number with no decimal places ");
cash = input.nextInt(); // read first integer
System.out.println("you have Entered "+cash);
hundred = 100;
change = (int)hundred - (int)cash;
System.out.println("The change from $100.00 is:" + (double)change);
change = 100 * change; // multiply cash by 100
money = (int)change; //cash is now a int
twenty = money / 2000;
money = money - (twenty * 2000);
toonies = money / 200; // money is multiplied by 100 than / by 200
money = money - (toonies * 200); // ex. money = 1586 - (7.93 * 200 )
lonies = money / 100 ;
money = money - (lonies * 100);
quarters = money / 25;
money = money - (quarters * 25);
dimes = money / 10;
money = money - (dimes * 10);
nickels = money / 5;
money = money - (nickels * 5);
pennies = money / 1;
money = money - (pennies * 1);
if(twenty>0){
System.out.println();
System.out.print("You will have this many Twenties ");
System.out.print(twenty + ".");
}
else{
}
if(toonies>0){
System.out.println();
System.out.print("You will have this many Toonies ");
System.out.print(toonies + ".");
System.out.println();
}
else{
}
if(lonies>0){
System.out.print(" You will have this many Lonies ");
System.out.print(lonies + ".");
System.out.println();
}
else{
}
if(quarters>0){
System.out.print(" You will have this many quarters ");
System.out.print(quarters + ".");
System.out.println();
}
else{
}
if(dimes>0){
System.out.print(" You will have this many dimes ");
System.out.print(dimes + ".");
System.out.println();
}
else{
}
if(dimes>0){
System.out.print(" You will have this many nickels ");
System.out.print(nickels);
System.out.println();
}
else{
}
if(pennies>0){
System.out.print(" You will have this many pennies ");
System.out.print(pennies);
System.out.println();
}
else{
}
System.out.println();
System.out.println("Thank you for using my app ");
}
}
You can use double or float for the variable's data type. To read a double from your input you would do:
double cash = input.nextDouble();
or
float cash = input.nextFloat();

Categories