Skip to content
Advertisement

Tag: switch-statement

Can you invert a switch in java?

Is there a way to invert a switch for example switch (!(x))? I’m writing a program and I only want the user to be able to input certain letters. If they input an invalid letter, they would have to input it again. I’m trying to use a switch for this avoiding doing something like because the number of valid inputs

How to use null in switch

In the code above I can’t use null in the switch case statement. How can I do this differently? I can’t use default because then I want to do something else. Answer This is was not possible with a switch statement in Java until Java 18. You had to check for null before the switch. But now, with pattern matching,

Java switch statement multiple cases

Just trying to figure out how to use many multiple cases for a Java switch statement. Here’s an example of what I’m trying to do: versus having to do: Any ideas if this possible, or what a good alternative is? Answer Sadly, it’s not possible in Java. You’ll have to resort to using if-else statements.

Count words, java

I want to count words. I use the methods hasNextChar and getChar. The sentence may contain all kind of chars. Here’s my code: It works so far but e.g. when I have a ” . ” at the end it gives me 8 instead of 7 words. Here are some examples of sentences: *„Schreiben Sie ein Praktikanten-Vermittlungs-Programm“ – words: 6

Why can’t I use switch statement on a String?

Is this functionality going to be put into a later Java version? Can someone explain why I can’t do this, as in, the technical way Java’s switch statement works? Answer Switch statements with String cases have been implemented in Java SE 7, at least 16 years after they were first requested. A clear reason for the delay was not provided,

Advertisement