All Questions
Tagged with switch-statement java
2,495
questions
-1
votes
1
answer
60
views
Write a method that takes 3 integer as parameters, return true if they are equal, return false otherwise without using if statement [closed]
I am trying to write a method that compares three integers without using the if statement. I tried the switch statement, but it wouldn't work. Eclipse shows case expressions must be constants.
Here is ...
0
votes
1
answer
50
views
Switch expression vs switch statement which one to use [closed]
Java 14 standardized switch expression and since Java 17 they have enhanced the switch expression to include pattern matching.
Is there any guideline to when one should be using switch statement vs ...
1
vote
2
answers
56
views
How can i make my code rerun after Ending?
switch(choice) {
case 1:
double sum = 0;
for (int i = 1; i <= 5; i++) {
System.out.println("Enter " + i + "th number: ...
-3
votes
1
answer
83
views
Can you define a variable in ranges in java [closed]
When using variables can you define or specify that variable as all the numbers within a range like 1-10? I am using Switch statements and want to assign one of the case variables all the variables ...
-3
votes
1
answer
52
views
Java Calculator Not Working - Need Help Troubleshooting ( Computer Science Beginner ) [closed]
I've written a simple calculator in Java that takes user input for two numbers and an operator. It's supposed to perform the corresponding mathematical operation and output the result. However, my ...
1
vote
1
answer
44
views
Switch executes default even with break in every case
So i'm trying to break out of the switch and while statements using the break in every case, yet even using the break statement my program keeps executing the default after the specific case code is ...
0
votes
1
answer
87
views
Can you break out of a switch case from within a loop?
I have a list of objects that I don't want to contain duplicates. Since the objects are expensive to create, I don't want to use a set, as that requires an object to be created first, before ...
0
votes
1
answer
41
views
The print statements for my printAnimals method won't stop executing
Whenever my printAnimals method executes, it won't stop repeating whats being printed. I've tried moving the break statements, and changing how the program determines what to print, but so far nothing ...
2
votes
3
answers
135
views
Convert to a switch statement
How do I convert this to a switch statement? I have tried this a few times. I am not sure what I am missing.
double totalPrice = 0.0;
if (venue.equals("Rod Laver")) {
totalPrice = 225.25 ...
2
votes
1
answer
193
views
Why Must Variables used in Guarded Pattern Case Labels be Final/Effectively Final? (Java 21)
With the new Java 21 features, we have better Pattern Matching. With these features, we have guarded pattern case labels, where one can define a pattern guard for case labels that looks like the ...
0
votes
0
answers
95
views
Using String values in switch and StringLists in case
I have two different String lists: firstList [a, b, c] and secondList [d, e, f]. Is there a way I can use a list in a case statement?
To be clear, the intended behavior is something like this:
switch(&...
0
votes
2
answers
191
views
How to set an error message when a field is empty?
I am doing an exercise in Java. How can I add an error message if the field Country is blank?
This is part of my code:
public class Nationality {
public static void main(String[] args) {
...
-1
votes
2
answers
819
views
What is the difference between a rule switch and a regular switch in Java?
I couldn't find this question anywhere else so I figured I might as well ask it. Is it purely aesthetic? Is it faster in any way? What are the main differences between the two? By regular switch, I ...
0
votes
1
answer
39
views
Method to turn input into numbers is not working [duplicate]
I am trying to make a method which converts certain inputs to a numbered output.
I am trying to make a method, which I call enumerate. It looks like this:
public static int enumerate(String user_input)...
-3
votes
2
answers
396
views
Assign value to variable in Java with Switch Stament [closed]
I am trying to assign value to variable based on case with old Switch case it is not allowing is there any way or should i need to write initialize variable in each single case?
public static void ...