0

is it possible to use array in switch statment in this code? because I try several times and I could not, also is there is another way to declare the BloodType then assign multiple String values to it without this array? e.g.(enum Or ArrayList ?) can I use them in this case?

private String []BloodType  = new String[8];
public String[] getBloodType() {
        return BloodType;
    }

    public void setBloodType(String[] BloodType) {
        this.BloodType = BloodType;
    }
        private void BloodType()
        {
            BloodType[0]="A+";
            BloodType[1]="A-";
            BloodType[2]="B+";
            BloodType[3]="B-";
            BloodType[4]="O+";
            BloodType[5]="O-";
            BloodType[6]="AB+";
            BloodType[7]="AB-";
        }
public void BloodInfomation () // use BloodType here as Array
    {

            switch (BloodType) {
                case "A+":
                    BloodCode=1;
                    System.out.println("Blood Type A+ & Blood Code is : " + BloodCode + "No of Bags avaliable"
                            + s.checkStockType.equals("A+"));
                    break;
                case "A-":
                    BloodCode=2;
                    System.out.println("Blood Type A- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("A-"));
                    break;
                case "B+":
                    BloodCode=3;
                    System.out.println("Blood Type B+ & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("B+"));
                    break;
                case "B-":
                    BloodCode=4;
                    System.out.println("Blood Type B- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("B-"));
                    break;
                case "O+":
                    BloodCode=5;
                    System.out.println("Blood Type O+ & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("O+"));
                    break;
                case "O-":
                    BloodCode=6;
                    System.out.println("Blood Type O- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("O-"));
                    break;
                case "AB+":
                    BloodCode=7;
                    System.out.println("Blood Type AB+ & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("AB+"));
                    break;
                case "AB-":
                    BloodCode=8;
                    System.out.println("Blood Type AB- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("AB-"));
                    break;
                default:
                    System.out.println("There is no code for the type you write !!");
                    break;
            }
        }
2
  • What are you stuck on? Do you get an error? Commented Apr 26, 2018 at 1:17
  • @GBlodgett I can't use BloodType array as a case in the switch statement as i mentioned above. Commented Apr 26, 2018 at 1:21

3 Answers 3

2

You're right. There is another way to implement your BloodType stuff. You mention enum and it is a better implementation for objects like blood type (with a finite number of instance variations).

enum BloodType {
    A_MINUS("A+", 1), 
    B_PLUS("B+", 2), 
    B_MINUS("B-", 3), 
    O_PLUS("AO", 4), 
    O_MINUS("O-", 5), 
    AB_PLUS("AB+", 6), 
    AB_MINUS("AB-", 7);

    BloodType(String value, int code) {
        this.value = value;
        this.code = code;
    }

    String value;
    int code;

}

void getBloodInformation(BloodType type) {
    switch (type) {
    case A_MINUS:
        // do something ...
        break;
    case O_MINUS:
        // do other things
        break;
    default:
        // default behavior if you didn't define all cases above
    }
}

That is better to use enums with switches. This way you cannot miss a case and you avoid potential errors due to wrong string spelling.

Sign up to request clarification or add additional context in comments.

4 Comments

@mada alternative solution with enum seems better in your case ;)
You can also add the BloodCode in the enum directly rather than using a switch to find it from BloodType.
Thank you, i also think it's better to use enum, But in enum can I assign values like A_MUNUS("A+")? then use it in a switch?
@Mada Yes you can add attributes to your enum. Enum is like classes (they are in fact). The main difference with classes is you cannot instantiate an enum (call the constructor). Otherwise you can define methods and attributes like you want. I edited my answer in that way.
0

The array works fine to store the blood types but your comparison will not work. In your switch you are comparing cases which are Strings to a String[]. What I think you want is two separate variables one that is the blood type array and another which is the specific blood type of the object you are referring too.

public class person{
//Defining the array of possible blood types which
private String[] BloodTypeArray = new String[8];
private void BuildBloodTypeArray(){
    BloodTypeArray[0]="A+";
    BloodTypeArray[1]="A-";
    BloodTypeArray[2]="B+";
    BloodTypeArray[3]="B-";
    BloodTypeArray[4]="O+";
    BloodTypeArray[5]="O-";
    BloodTypeArray[6]="AB+";
    BloodTypeArray[7]="AB-";
}
//Defining the specific blood type of this instance
private String BloodType;
public String getBloodType(){
    return BloodType;
}
public void setBloodType(String BloodType){
    this.BloodType = BloodType;
}
//The switch needs something to compare the case to so it takes an input string
public void BloodInformation(){
    int BloodCode;
    switch(this.BloodType){
        case "A+":
        BloodCode=1;
        System.out.println("Blood Type A+ & Blood Code is : " + BloodCode + " No of Bags avaliable" );
        break;
    case "A-":
        BloodCode=2;
        System.out.println("Blood Type A- & Blood Code is : " + BloodCode + " No of Bags avaliable" );
        break;
    case "B+":
        BloodCode=3;
        System.out.println("Blood Type B+ & Blood Code is : " + BloodCode + " No of Bags avaliable");
        break;
    case "B-":
        BloodCode=4;
        System.out.println("Blood Type B- & Blood Code is : " + BloodCode + " No of Bags avaliable");
        break;
    case "O+":
        BloodCode=5;
        System.out.println("Blood Type O+ & Blood Code is : " + BloodCode + " No of Bags avaliable");
        break;
    case "O-":
        BloodCode=6;
        System.out.println("Blood Type O- & Blood Code is : " + BloodCode + " No of Bags avaliable");
        break;
    case "AB+":
        BloodCode=7;
        System.out.println("Blood Type AB+ & Blood Code is : " + BloodCode + " No of Bags avaliable");
        break;
    case "AB-":
        BloodCode=8;
        System.out.println("Blood Type AB- & Blood Code is : " + BloodCode + " No of Bags avaliable");
        break;
    default:
        System.out.println("There is no code for the type you write !!");
        break;
    }
}
public static void main(String[] args){
    person p = new person();
    p.BuildBloodTypeArray();
    p.setBloodType("AB-");
    System.out.println(p.getBloodType());
    p.BloodInformation();

}

Here is an example using the code you have posted and a class called person. Also notice that a loop can be used in this case rather than a switch because of all the repete code. You could replace your switch with this.

public void BloodInformation(){
    int BloodCode;
    for(int i=0; i<BloodTypeArray.length; i++){
        BloodCode = i+1;
        if(this.BloodType == BloodTypeArray[i]){
            System.out.println("Blood Type "+ BloodTypeArray[i] + " & Blood Code is : " + BloodCode + " No of Bags avaliable" );
            return;
        }
    }
    System.out.println("There is no code for the type you write !!");
}

Comments

0

Switching on an array won't work. Instead, look over the items in the array and switch on each item. To do this, you'll need to wrap the switch in a loop to iterate over the blood types in the BloodInformation method.

public void BloodInfomation () // use BloodType here as Array
{
          for(String type : BloodType) {
            switch (type) {
                case "A+":
                    BloodCode=1;
                    System.out.println("Blood Type A+ & Blood Code is : " + BloodCode + "No of Bags avaliable"
                            + s.checkStockType.equals("A+"));
                    break;
                case "A-":
                    BloodCode=2;
                    System.out.println("Blood Type A- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("A-"));
                    break;
                case "B+":
                    BloodCode=3;
                    System.out.println("Blood Type B+ & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("B+"));
                    break;
                case "B-":
                    BloodCode=4;
                    System.out.println("Blood Type B- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("B-"));
                    break;
                case "O+":
                    BloodCode=5;
                    System.out.println("Blood Type O+ & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("O+"));
                    break;
                case "O-":
                    BloodCode=6;
                    System.out.println("Blood Type O- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("O-"));
                    break;
                case "AB+":
                    BloodCode=7;
                    System.out.println("Blood Type AB+ & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("AB+"));
                    break;
                case "AB-":
                    BloodCode=8;
                    System.out.println("Blood Type AB- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("AB-"));
                    break;
                default:
                    System.out.println("There is no code for the type you write !!");
                    break;
            }
        }
     }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.