Home > Software engineering >  How to deal with IndexOutOfBounds error in arrays in such a case?
How to deal with IndexOutOfBounds error in arrays in such a case?

Time:01-06

I need to create a method for my OOP lab, the details are as the following:

A ThreeWayLamp class models the behavior of a lamp that uses a three-way bulb. These bulbs have four possible states: off, low light, medium light, and high light. Each time the switch is activated, the bulb goes to the next state (from high, the next state is off, from off to low etc). The ThreeWayLamp class has a single method called switch() which takes a single int parameter indicating how many times the switch is activated. (you need to throw an exception if its negative). The Switch() method should simply print out to System.out a message indicating the state of the bulb after it has changed.

public class ThreeWayLamp {

public String[] States = {"Off","LowLifght", "MediumLifght", "HighLight"}; // an array of the 4 states
    
    
    public void Switch(int switchState){
    
      //used an if condition to determine what to print based on the parameter switchState

        if ((switchState <= States.length) && (switchState >= 0)){
            System.out.println(States[switchState]);
            
        }else if (switchState < 0 ){
            System.out.println("Wrong input, try again with diffrent number");
           
        }else if (switchState >= States.length){
            
        } //This condition is the issue, how to form a condition that will solve this problem
    }

If the parameter is larger than the array's length, an error will occur, so the issue is how to form a condition that will make the array loop again around itself when it reaches its last index. For example, if the input was 5, then the method should print LowLight. Is there a possible condition or function that could solve this issue, or should I change the entire structure of the code?

CodePudding user response:

You can solve this problem by using a modulo operator:

System.out.println(States[switchState % States.length]);

CodePudding user response:

Instead of using the less than condition, you can try using mod to repeat the indexes of your String array.

Try below code:

public class ThreeWayLamp {

public String[] States = {"Off","LowLifght", "MediumLifght", "HighLight"}; // an array of the 4 states
    
    
    public void Switch(int switchState){

        
    if ((switchState > 0){
        System.out.println(States[switchState % States.length]); // states.length = 4 in your case
    } else {
        System.out.println("Wrong input, try again with diffrent number");
    }
    
           
    }

CodePudding user response:

Using modulo you can ensure the index will always be within the size of the array

4 % 4 = 0
5 % 4 = 1
6 % 4 = 2
7 % 4 = 3
8 % 4 = 0
9 % 4 = 1
...
public void Switch(int switchState){
    if ((switchState < States.length) && (switchState >= 0)){
        System.out.println(States[switchState]);
    } else if (switchState < 0 ){
        System.out.println("Wrong input, try again with diffrent number");
    } else {
        Switch(switchState % 4);
        // or
        // System.out.println(States[switchState % 4]);
    }
}

Also, variable and method names should start with a lower case

  • Related