Variable Might Not Have Been Initialized in Java; Where Did I Mess Up?
by zen
Question by xXBrudu BXx: Variable might not have been initialized in Java; Where did I mess up?
The code is as follows:
/**
* @(#)IRSTaxCalculator.java
*
* IRSTaxCalculator application
* This program will calculate taxable pay based on marital status and income.
* @author
* @version 1.00 2010/10/11
*/
import java.util.Scanner;
public class IRSTaxCalculator {
public static void main(String[] args) {
// These are the values that will be needed to be pre-defined
// The following takes the input for the value of marital status
System.out.println(“Please press 1 if you are Single, 2 if you are Married, or 0 if you would like to quit.”);
Scanner alpha = new Scanner(System.in);
int status = alpha.nextInt();
//The following is the escape sequence
if (status == 0)
System.out.println(“Thank you, and please use this program again.”);
// This calculates taxes for Single people
else if (status == 1)
{
double taxes;
System.out.println(“Please input your salary. No special characters, please.”);
Scanner beta = new Scanner(System.in);
double salary = beta.nextDouble();
if (salary >= 0 && salary <= 27050)
taxes = salary * 0.15;
if (salary > 27050 && salary <= 65500)
taxes = (salary-27050) * 0.275 + 4057.50;
if (salary > 65500 && salary <= 136750)
taxes = (salary-65500) * 0.355 + 36361;
if (salary > 136750)
taxes = (salary-136750) * 0.391 + 88306;
if (salary < 0)
System.out.println("I think you put in a negative value. Either you are making a mistake, or you need to stop gambling.");
//This is the output statement for these lines
System.out.println("You filed your taxes as single");
System.out.println("Your taxable salary is " + salary);
System.out.println("Your taxes are " + taxes);
}
//This calculates the taxes for married people
else if (status == 2)
{
double taxes;
System.out.println("Please input your salary. No special characters, please.");
Scanner beta = new Scanner(System.in);
double salary = beta.nextDouble();
if (salary >= 0 && salary <= 45200)
taxes = (salary) * 0.15;
if (salary > 45200 && salary <= 109250)
taxes = (salary - 45200) * 0.275 + 6780;
if (salary > 109250 && salary <= 166500)
taxes = (salary - 109250) * 0.305;
if (salary > 166500 && salary <= 297350)
taxes = (salary - 166500) * 0.355 + 41855;
if (salary > 297350)
taxes = (salary – 297350) * 0.391 + 88306;
if (salary < 0);
System.out.println("I think you put in a negative value. EIther you are making a mistake, or you need to see what your spouse is doing with your money.");
System.out.println("You filed your taxes as married");
System.out.println("Your taxable salary is " + salary);
System.out.println("Your taxes are " + taxes);
}
}
}
I can't for the life of me figure out what I did wrong. I'm relatively new at this, so I am sure it is some stupid little mistake. The program takes marital status, applies it to a tax group, takes the salary, plugs it in given the marital status, and decides how much your taxes for the year are. I can't get the double taxes to work. Any ideas?
I tried the first comments idea, but to no avail.
Best answer:
Answer by Darnental
You declared “double taxes” inside the if statements. They cease to exist as soon as the if statements close. Try putting it before the first System.out.println.
Answer by mark r
without compiling it I would guess that the problem is that you haven’t set a value to the taxes variable.
when you define taxes (or shortly afterwards) set it to a value e.g. 0.
double taxes = 0;
As I say I haven’t got a compiler to hand to test it with but that is probably the problem.
Java/your editor is warning you that it is possible for the software to run and for the value to have no value.
Do you have any more information about where the error is e.g. a line number or line of code that is highlighted?
What do you think? Answer below!
Gambling Ruined My Life. Quit Gambling. Stop Gambling. Gambling Hypnotherapy, Laser, Bioresonance. – www.lifeprinciples.com Here is another person who quit gambling with our help Hi my name is Andrew. My sister saw Shokat on the TV and then read more about it on the internet. My brother in law then bought me to see Shokat and also helped me to finance the programme. Over the last 10 years I must have lost over £100000 on gambling. The stress of gambling was making me ill. I neglected every aspect of my life because of this. Gambling on the roulette tables was running my life. I would get paid and then be drawn to the roulette table where I would lose all my money. I would feel physically sick after losing all my money and become depressed and promise myself I would never gamble again. The following week I would end up gambling again. After the first session three weeks ago, I have lost all compulsion to gamble. I have been surprised it has been that easy. A week after my treatment I deliberately went into the bookies to see what would happen. I could see other people gamble but not once was I tempted to place a bet. In the past if I ever went to the bookies I would play and nothing could tear me away from plying roulette until I had lost all my money. I now have a life thanks to you. Andrew from Birmingham. www.youtube.com Here is another person who quit gambling with our help: Roberts twenty year gambling addiction vanished in just one powerful hypnotherapy session. Dear Ali. I can?t thank you enough for your time and help yesterday. I feel a calm I haven?t felt for …
Tribe, Canterbury horse track reach deal to stop racino
Filed under: stop gambling
The tribe will benefit, too, because the agreement solidifies the southwest corner of the Twin Cities metro areas as the premier entertainment and gambling destination for the region, said Edward Stevenson, president and CEO of the SMSC Gaming …
Read more on Minnesota Public Radio
Gambling site releases betting lines on 2012 season with A&M favored in just …
Filed under: stop gambling
There are still 80 days until the beginning of the college football season but that wouldn't stop a bettor from picking who will win each SEC game would it? The gambling site, Beyond the Bets, released their "Way-too-early college football point spread …
Read more on Dallas Morning News (subscription) (blog)