Quantcast
Viewing all articles
Browse latest Browse all 5661

Using Strings with Switch statements

So I have searched around, and read that in the JDK 1.7 it allows for Strings to be used in Switch statements. I am using a book to learn java, and it uses a string with the switch statement. In the book however, it is using NetBeans instead of IntelliJ. Here is my code:

 

publicclass Commodity {    publicstaticvoid main(String[] args) {        String command = "BUY";        int balance = 550;        int quantity = 42;         switch(command) {            case"BUY":                quantity += 5;                balance -= 20;                break;            case"SELL":                quantity -= 5;                balance += 15;        }        System.out.println("Balance: " + balance +                "\n" + "Quantity: " + quantity);        }    }

 

 

But I keep getting the error on the switch(command) line. I have the JDK 1.7 and it will still not let me do this. Anyone know how I can do this? Thanks


Viewing all articles
Browse latest Browse all 5661

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>