Home > Software engineering >  Java -how can I store an array in switch case
Java -how can I store an array in switch case

Time:12-04

I need to build code that first gets a 2d array and then prints it. for this, I built a menu with a switch case.

when the user clicks 0, the user types the size of the array (the size is always n*n), and then the user types the values. then I need to create a function that uses this info to build a char array.(the values is hex base 0-F)

when the user clicks 1, the code needs to print the same 2d array. I have a difficult time understanding how I can move the array from case 0.

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;
    
    
    do {
    
        user_selction=Menu();
        switch(user_selction) {
        case 0:
            Menu_0(user_selction);
            break;
        case 1:
            
            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 int Menu_0 (int a)
{
    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 a;   
    }
    System.out.println("Please insert the photo value>");
    String strPhoto;
    do {
    strPhoto = reader.nextLine();
     } while(strPhoto.length() == 0);
    if(strPhoto.length()!=Ps*Ps)
    {
        System.out.println("Invalid Photo Input!");
        return a;   
    }
    for(int i=0;i<Ps*Ps;i  )
    {
        if(strPhoto.charAt(i)<'0'||strPhoto.charAt(i)>'F')
        {
            System.out.println("Invalid Photo Input!");
            return a;   
        }
    }
    return a;
}

CodePudding user response:

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();
    }
}
}

CodePudding user response:

Use Scanner like this:

            Scanner sc = new Scanner(System.in);
            System.out.println("Select 1 to input array size or 2 to do sth");
            int option = sc.nextInt();


            switch(option) {    

            case 1 :
                Scanner sc = new Scanner(System.in);
                System.out.println("Enter array size: ");
                int size = sc.nextInt()
                int[] array = new array[size*size];
                break;

            case 2 :
                something;
                break;

            default:
                System.out.println("No such option ");
                break;
            }
  • Related