-5

the If condition doesn't work always trade in allowance= 3,250.00 I need it depends on the state and car year. I attached what my code and more explanation.

The trade-in allowance depends on the year of manufacture of the vehicle and the state in which it had been registered at initial purchase. FHM does not accept vehicles manufactured earlier that 1990 for trade-in allowance. For vehicles manufactured between

1990 and 1999 and initially registered in Illinois, Indiana, Iowa, Kansas, Michigan, Minnesota, Missouri, or Nebraska, the trade-in allowance is $3000.00, for all other states it is $ 2750.00.

For vehicles manufactured between 2000 and 2009 and initially registered in Illinois, Indiana, Iowa, Kansas, Michigan, Minnesota, Missouri, Nebraska, North Dakota, Ohio, South Dakota or Wisconsin, the trade-in allowance is $3250.00, for all other states it is $ 3000.00.

For vehicles manufactured in 2010 and after, the trade-in allowance is $5000.00, irrespective of the state of initial registration.

if (vehiclesYear <= 1999 || vehiclesYear >= 1990
        & (stateNames.getSelectedItem() == "IL")
        || (stateNames.getSelectedItem() == "IN")
        || (stateNames.getSelectedItem() == "IA")
        || (stateNames.getSelectedItem() == "KS")
        || (stateNames.getSelectedItem() == "MI")
        || (stateNames.getSelectedItem() == "MN")
        || (stateNames.getSelectedItem() == "MO")
        || (stateNames.getSelectedItem() == "NE")) {
    tradeIn = 3000.00;
} else {
    tradeIn = 2750.00;
}

if (vehiclesYear >= 2000
        & (stateNames.getSelectedItem() == "IL")
        || (stateNames.getSelectedItem() == "IN")
        || (stateNames.getSelectedItem() == "IA")
        || (stateNames.getSelectedItem() == "KS")
        || (stateNames.getSelectedItem() == "MI")
        || (stateNames.getSelectedItem() == "MN")
        || (stateNames.getSelectedItem() == "MO")
        || (stateNames.getSelectedItem() == "NE")
        || (stateNames.getSelectedItem() == "ND")
        || (stateNames.getSelectedItem() == "OH")
        || (stateNames.getSelectedItem() == "SD")
        || (stateNames.getSelectedItem() == "WI")) {
    tradeIn = 3250.00;
} else {
    tradeIn = 3000.00;
}

if (vehiclesYear >= 2010) {
    tradeIn = 5000.00;
}
Susheel Singh
  • 3,824
  • 5
  • 31
  • 66
  • 3
    Make your question brief with an example of what you're asking and provide the relevant code (well indented). We don't need the *whole* code. Post an [MCVE](http://stackoverflow.com/help/mcve). – user1803551 May 09 '14 at 22:30
  • 1
    `vehiclesYear<=1999 ||vehiclesYear>=1990` this doesn't actually do anything. – shuttle87 May 09 '14 at 22:31
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Sotirios Delimanolis May 09 '14 at 22:33

4 Answers4

1

let us walk through your code.

if you want to have a between check

change following

if (vehiclesYear <= 1999 || vehiclesYear >= 1990)

to

if (vehiclesYear <= 1999 && vehiclesYear >= 1990)

also i suggest you define a String variable and assign it to stateNames.getSelectedItem() like

String item = stateNames.getSelectedItem();

and use this item for comparison

also as it is a string i suggest you to use .equals() method for comparison than == to avoid reference comparison issue

also for comparison you can implement method using switch(item) java 7 and above will support the same

like

switch (item)
{
    case "IL":
    case "IN":
    .
    .
    case "KS":
        return true;//or do something here
    break;
}
dev2d
  • 4,245
  • 3
  • 31
  • 54
1

One main problem is that you're comparing Strings using the == operator, which is not guaranteed to give the expected result.

In Java, Strings are objects, and the == operator compares for object reference equality. So if I do

String myString = "Foo";
if (myString == "Foo") {
  System.out.println("They are the same");
} else {
  System.out.println("They are not the same");
}

the results are not guaranteed. To compare Strings for equality, use the String.equals(String) method.

On a more stylistic note, I would probably have inserted the state codes into separate Set<String> instances like so:

private boolean isInTheNineties(int year) {
  return year >= 1990 && year <= 1999;
}

private double getTradeIn(int vehiclesYear, SomeClass stateNames) {

 Set<String> s0 = new HashSet<>();
 s0.addAll(Arrays.asList("IL", ... , "NE"));
 String registrarState = stateNames.getSelectedItem();
 if (isInTheNineties(vehiclesYear)) {
   if (s0.contains(registrarState)) {
     return 3000.00;
   } else {
     return 2750.00;
   }
 }

 // And so on for the 2000s
}

Also, the Why not use Double or Float to represent currency? question seems relevant here.

ansjob
  • 175
  • 7
0

Currently any year satisfies the following condition:

if (vehiclesYear <= 1999 || vehiclesYear >= 1990)

This is because every year is either before 1999 or after 1990.

If you want the years between 1999 and 1990 then you need to do this:

if (vehiclesYear <= 1999 && vehiclesYear >= 1990)
shuttle87
  • 15,466
  • 11
  • 77
  • 106
-1

I'm not completely sure about this, but you might have to put the "tradein=3000.00;" inside of a block to get it to work out. {} after your 'if' statement and before your 'else' statement, everything should be surrounded in a block, this might alter and fix your problem!

  • While many do [consider it to be good practice](http://programmers.stackexchange.com/questions/16528/single-statement-if-block-braces-or-no), it doesn't make any functional difference here. – femtoRgon May 09 '14 at 22:43
  • Alright, thanks for the pointer :) I am new to java and trying to rack up those reputation points, thought i got the research right but alright, thanks for the help – David Ross May 09 '14 at 22:47
  • **Don't** "try to rack up" reputation. You need to *earn* it with proper, well formed and well informed answers. If you are new to Java, get experienced with it before answering other new-to-Java users' questions. After that, try to answer questions which weren't downvoted 5 times... it says something about the question. – user1803551 May 10 '14 at 12:05