473,386 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Conditional logic

Greetings all. I am having a problem with getting java program to produce the correct output. I am being asked to use conditional logic to determine a tax rate category. I am not getting any syntax errors at all but I am not getting the correct code after putting in the annual salary as directed.
Here is my code. Any help in pointing me in the right direction will be greatly appreciated.


import javax.swing.JOptionPane;

public class TaxCodeTest1 {
public static void main(String[] args) {


float salary = 0;
float code = 0;

JOptionPane.showInputDialog(null,
"Enter annual salary, example 10000",
"Input",
JOptionPane.QUESTION_MESSAGE);



if (salary < 15000) { //Salary less than 15000 code 1
code = '1';
JOptionPane.showMessageDialog(null, " Tax Code =" + 1);
}
else if (salary >= 15001 - 25000) { //salary between 15001 - 25000 code 2
code = '2';
JOptionPane.showMessageDialog(null, " Tax Code =" + 2);
}
else if (salary >= 25001 - 40000) { //salary between 25001 - 40000 code 3
code = '3';
}
else if (salary >= 40001 - 65000) { //salary between 40001 - 65000
code = '4';
}
else if (salary >= 65001 - 80000) { //salary between 65001 - 80000
code = '5';
}
else if (salary > 80000) { //salary greater than 80000
code = '6';


JOptionPane.showMessageDialog(null, "The code based on annual salary") ;

}
}
}
Sep 26 '07 #1
6 1794
JosAH
11,448 Expert 8TB
You can't test whether or not a value lies in an interval that way: the '-' symbol
actually means subtraction so the compiler (that doesn't know what you want to
do) compiles your code but your compiled code does quite something different
from what you want to accomplish.

If you want to check whether or not a value 'v' lies in an interval 'lo' ... 'hi' do this:

Expand|Select|Wrap|Line Numbers
  1. if (lo <= v && v < hi) ...
  2.  
The interval is 'half open' i.e. the interval is [lo, hi)

kind regards,

Jos
Sep 26 '07 #2
r035198x
13,262 8TB
Greetings all. I am having a problem with getting java program to produce the correct output. I am being asked to use conditional logic to determine a tax rate category. I am not getting any syntax errors at all but I am not getting the correct code after putting in the annual salary as directed.
Here is my code. Any help in pointing me in the right direction will be greatly appreciated.


import javax.swing.JOptionPane;

public class TaxCodeTest1 {
public static void main(String[] args) {


float salary = 0;
float code = 0;

JOptionPane.showInputDialog(null,
"Enter annual salary, example 10000",
"Input",
JOptionPane.QUESTION_MESSAGE);



if (salary < 15000) { //Salary less than 15000 code 1
code = '1';
JOptionPane.showMessageDialog(null, " Tax Code =" + 1);
}
else if (salary >= 15001 - 25000) { //salary between 15001 - 25000 code 2
code = '2';
JOptionPane.showMessageDialog(null, " Tax Code =" + 2);
}
else if (salary >= 25001 - 40000) { //salary between 25001 - 40000 code 3
code = '3';
}
else if (salary >= 40001 - 65000) { //salary between 40001 - 65000
code = '4';
}
else if (salary >= 65001 - 80000) { //salary between 65001 - 80000
code = '5';
}
else if (salary > 80000) { //salary greater than 80000
code = '6';


JOptionPane.showMessageDialog(null, "The code based on annual salary") ;

}
}
}
1) Use code tags when posting code.
2.) Your salary is always 0. You did not assign the value entered to the salary variable.
Sep 26 '07 #3
r035198x
13,262 8TB
You can't test whether or not a value lies in an interval that way: the '-' symbol
actually means subtraction so the compiler (that doesn't know what you want to
do) compiles your code but your compiled code does quite something different
from what you want to accomplish.

If you want to check whether or not a value 'v' lies in an interval 'lo' ... 'hi' do this:

Expand|Select|Wrap|Line Numbers
  1. if (lo <= v && v < hi) ...
  2.  
The interval is 'half open' i.e. the interval is [lo, hi)

kind regards,

Jos
Hey, haven't we seen this code before?
Sep 26 '07 #4
JosAH
11,448 Expert 8TB
Hey, haven't we seen this code before?
Yup, l'histoire se repete as the Germans say ;-)

kind regards,

Jos
Sep 26 '07 #5
r035198x
13,262 8TB
Yup, l'histoire se repete as the Germans say ;-)

kind regards,

Jos
I prefer king Solomon's "There is nothing new under the sun".
Sep 26 '07 #6
JosAH
11,448 Expert 8TB
I prefer king Solomon's "There is nothing new under the sun".
As in "been there; done that"? ;-)

kind regards,

Jos
Sep 26 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Bruce | last post by:
Which of the popular templating systems (e.g. PHLib, FastTemplate, Smarty) allow you to setup conditions in your templates, for example: <-- IF Variable=value --> this HTML <-- ELSEIF...
62
by: Reinhold Birkenfeld | last post by:
Hi, after Guido's pronouncement yesterday, in one of the next versions of Python there will be a conditional expression with the following syntax: X if C else Y which is the same as today's...
3
by: Chuck Reed | last post by:
I am working on a sales report where I show weekly sales by category for each of the 52 weeks in the year. Each record in my table/report has the 52 weeks of sales in it. I want to highlight to top...
92
by: Raghavendra R A V, CSS India | last post by:
hie.. Do any one knows how to write a C program without using the conditional statements if, for, while, do, switch, goto and even condotional statements ? It would be a great help for me if...
6
by: Robert Zurer | last post by:
In his paper on Coding Standard found on http://www.idesign.net/idesign/DesktopDefault.aspx Juval Lowy discourages the use of the ternary conditional operator with no specific reason given. ...
3
by: John | last post by:
Hi I have an app to deal with two companies A & B within a group of companies depending on which database is selected. Is it possible for setup to ask the user at install time which company they...
2
by: booksnore | last post by:
..eh I was stuck thinking up a subject title for this post for a while.. So I am processing a really big file (scary big). Each record is fixed length, I need to test conditions on certain fields...
4
by: Basilisk96 | last post by:
This topic is difficult to describe in one subject sentence... Has anyone come across the application of the simple statement "if (object1's attributes meet some conditions) then (set object2's...
1
by: mikebrengartner | last post by:
Hi all, I've got a data bound list view that I'm trying to work with. I'm trying to fill out the item template with a table row that displays a link to a file if the job status is complete. I've...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.