Switch Statement Where Value Is Int But Case Can Contain Array

Switch statement where value is Int but case can contain array

You can use case let with where for that.

let intValues = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
let inputValue = 30 // or some other int value
switch inputValue {
case let x where intValues.contains(x):
// do something more:
case 101:
// do something lol
case 13131:
// do another thing
default:
// do default
}

Matching int with element of int array in switch case

switch labels are evaluated at compiling time, so it must be constant expressions. You cannot put an expression that evaluates to anything.

If you want an equivalent algorithm, you have to use an if statement

Java -how can I store an array in switch case

This is an example

import java.util.Scanner;

public class Assignment3 {
static Scanner reader = new Scanner (System.in);

public static void main(String[] args) {

int checker=1;
int user_selction;
char [][]array = null;

do {
user_selction=Menu();
switch(user_selction) {
case 0:
array = Menu_0();
break;
case 1:
print_array(array);
break;
case 2:
break;
case 3:
break;
case 4:
checker=GoodBye(checker);
break;
default:
break;
}

}while(checker==1);

}
public static int Menu ()
{
int menu_num;
System.out.println("~ Photo Analyzed ~");
System.out.println("0. Load Photo");
System.out.println("1. Print Photo");
System.out.println("2. Circle Check");
System.out.println("3. Random Check");
System.out.println("4. Exit");
System.out.println("Please select an option>");
menu_num=reader.nextInt();
if(menu_num>4||menu_num<0)
{
System.out.println("Invalid input");

}

return menu_num;
}
public static int GoodBye(int GB)
{
GB=0;
System.out.println("Goodbye!");
return GB;
}

public static char[][] Menu_0 ()
{
int Ps;
System.out.println("Please insert the photo size>");
Ps=reader.nextInt();
if(Ps<0||Ps>12)
{
System.out.println("Invalid Photo Input!");
return null;
}
System.out.println("Please insert the photo value>");

char [][]array = new char[Ps][Ps];

for(int i=0;i<Ps;i++)
{
for (int j = 0 ; j < Ps ; j++)
{
char c = reader.next().charAt(0);
if(c<'0'||c>'F')
{
System.out.println("Invalid Photo Input!");
return null;
} else {
array[i][j] = c;
}
}
}
return array;
}

public static void print_array(char [][]array){
for (int i = 0 ; i < array[0].length ; i++)
{
for (int j = 0 ; j < array[0].length ; j++)
{
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}

Using int array for switch case

You can't pass an array into a switch() you need to pass one of the elements in the array:

int item = 2; // or whatever

switch(option_numbers[item])
{

EDIT:

Based on your comment, if you want to take user input and have them pick one of the 4 options it's pretty simple, the array is totally unneeded.

int selection = -1;
printf("Which option do you want? (1-4): ");
scanf("%d", &selection);

switch(selection)
{

side note:

int option_numbers[3] = {1,2,3,4}; // that's not size 3, it's got 4 elements should 
// have been:
// int option_numbers[4] = {1,2,3,4};

How to return arrays in switch-case statement in Java?

Your code contains multiple unrelated errors.

Array initializers not allowed

{"saturday", "sunday"}

That's not legal java, except in very specific places.

Specifically, in the initializing expression of either a local variable, or field declaration whose type is an array, that is legal, and it isn't legal anywhere else. In other words, okay:

String[] x = {"saturday", "sunday"};

And that's about the only place you can legally write that. If you want an expression that resolves to a string array with 2 'values', the first one being "saturday" and the second being "sunday", you write:

new String[] {"saturday", "sunday"};

This is also legit in variable declarations:

String[] x = new String[] {"saturday", "sunday"};

But you can 'shorten' it and omit the new String[] part there.

Parameter confusion

In your main app you pass "long" as parameter to your getWeekends method, but your getWeekends() method declaration accepts zero parameters. You need to fix that:

public static void getWeekends(String type) {

Return type confusion

You write return in your getWeekends method, i.e. you want to return something. Specifically, you want to return a string array. However, you've declared it to return nothing (void means: I return nothing). You must fix this:

public static String[] getWeekends(String type) {

Pointless creation of day

It looks like that whole day thing is you trying random stuff, flailing about and having no idea what to do. Let's just get rid of it? Remove the stuff from String[] day to day[3]=; you can simply switch on type, which is the parameter you got from whatever called you.

Your app does nothing

A return value is just... Returned. To the caller. The caller needs to do something with it. Perhaps, here, as an example, we shall print it. Let's add that to your main method using System.out.println(Arrays.toString(the-array-here)).

null confusion

null should not be used unless you really know what you are doing and you have a clear definition of things. It most certainly should not be used as 'stand in' for an error condition. Just throw something if a code path occurs that you don't know how to deal with. The default case should throw something instead of returning null.

Putting it all together

Thus:

class App {

public static void main(String[] args){
String[] dayNames = App.getWeekends("long");
System.out.println(Arrays.toString(dayNames));
}

public static String[] getWeekends(String type) {
switch(type) {
case "long":
return new String[] {"saturday","sunday"};

case "short":
return new String[] {"sat", "sun"};

default:
throw new IllegalArgumentException("Pass either 'short' or 'long'");
}
}
}

Use an array as a case statement in switch

NO, simply you cannot.

SwitchStatement:
switch ( Expression ) SwitchBlock

The type of the Expression must be char, byte, short, int, Character, Byte, Short, Integer, String, or an enum type (§8.9), or a compile-time error occurs.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.11

Why Can't I Use An Array in a Switch Statement in C?

You do not need the single quotes around the numbers in the case statement. You are creating character literals by including the single quotes. Just take them off and you'll have integer literals, which is what you intend.



Related Topics



Leave a reply



Submit