0

Tested this code from an exercise book and I don't understand why it refuses to compile because of "Incompatible types"

The version of Java is 7 update 51 and I am using a college computer so I suspect it is not a problem with versions or hardware.

There is something else going on, but I can't seem to figure it out.

The problem is at line 40 where the getState() method returns a String for a switch that has String cases. Incompatible.

I'd love to know what the machine is trying to do. Any help is greatly appreciated. Thank you so much for your time in advance.

public class AutoPolicy {

    private int accountNumber;
    private String makeAndModel, state;

    public AutoPolicy(int accountNumber, String makeAndModel, String state) {
        this.accountNumber = accountNumber;
        this.makeAndModel = makeAndModel;
        this.state = state;
    }

    public void setAccountNumber(int accountNumber) //Start set and get
    {
        this.accountNumber = accountNumber;
    }

    public int getAccountNumber() {
        return accountNumber;
    }

    public void setMakeAndModel(String makeAndModel) {
        this.makeAndModel = makeAndModel;
    }

    public String getMakeAndModel() {
        return makeAndModel;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getState() {
        return state;
    }

    public boolean isNoFaultState() {
        boolean noFaultState;

        switch (getState()) {
            case "MA":
            case "NJ":
            case "NY":
            case "PA":
                noFaultState = true;
                break;

            default:
                noFaultState = false;
        }

        return noFaultState;
    }
}
  • It would really help if you told us what programming language this was. – Dour High Arch Jul 07 '15 at 23:04
  • 6
    Switch on Strings is Java 7+ – qqilihq Jul 07 '15 at 23:17
  • 1
    If this is Java, what version of Java is it? Java only recently started supporting string switch statements. If you don't have Java 7, you may have to make due with if statements. – MeetTitan Jul 07 '15 at 23:18
  • FYI everyone's first question gets downvoted by unhelpful/unprofessional people who prefer not to leave feedback or guidance, so don't take it personally. Although this is not the worst first question I have seen by a long way. Anyway if you stick with it, your questions and responses get better. Java 7 definitely sounds like a likely culprit although its hard to imagine anyone using an exercise book today would have installed an older JDK (unless you are just working on an old machine). – gnomed Jul 07 '15 at 23:32
  • @gnomed Maybe these voters are as "sluggish" as OP, because he didn't do the least amount of research [to google](https://www.google.de/search?q=java+switch+string+incompatible+types). But I don't know that for sure. – Tom Jul 07 '15 at 23:48
  • possible duplicate of [Switch Statement gives Incompatible Types error](http://stackoverflow.com/questions/12521355/switch-statement-gives-incompatible-types-error) – Tom Jul 07 '15 at 23:50
  • It is indeed Java, sorry for leaving out that little detail. – Steven Young Jul 08 '15 at 12:34
  • The version of Java should be updated. I am using JCreator LE from one of the computers of my college so it is highly unlikely they would have an outdated machine. – Steven Young Jul 08 '15 at 12:37
  • Tom I checked that post out, thanks for the tip. Sadly, it didn't help me solve my problem. I really want to get this code to work, huge headache over here hahaha – Steven Young Jul 08 '15 at 12:42
  • After compiling in several different computers with the same version of Java (Java 7) it still has an incompatible types error. – Steven Young Jul 08 '15 at 12:57
  • Sorry about the multiple comments, I mean to make a new line but enter functions as a new comment instead. Not used to that at all. Can anyone compile this code? – Steven Young Jul 08 '15 at 12:59
  • It compiles without any errors here (java 8), but gives an "incompatible types" compile error using java 6. By the way, line 57 it's not the switch statement. Actually, your code has only 54 lines... – cesarse Jul 08 '15 at 13:09
  • Someone changed the original code for more readability I guess. I'll be moving on in the book, but I'll check back and see if anyone can help me out. Thanks for the confirmation cesarse. – Steven Young Jul 08 '15 at 13:15

0 Answers0