0

I am calling a method from another class. The method contains an integer array. I am trying to stay away from inputting the index manually.

I am trying to search for numbers within a range.

example: ArrayList: {1,5}, {5,10}, {10,15}

Input: enter 3

Process: search for number within range

output: 1,5

The driver class is storing the objects from the main class called Numbers into ArrayList. The main class have an accessor call getNumbers. getNumbers contains an integer array with 2 elements. The driver is calling getNumbers to validate the entry that users input.

The code below works but I'm told it's consider bad coding to code entering the indexes. I want to know how to output the array from getNumber method without knowing the array length of getNumber?

example of what I have:

for(int i = 0; i < example.size(); i++) 
         //number is the integer that is inputted.
         if(example.get(i).getNumbers()[1] > number &&
         example.get(i).getNumbers()[0] <= numbers)
            System.out.println(example.get(i));

Should I add another for loop?

example of what I am thinking of:

for(int i = 0; i < example.size(); i++)
    for(int j = 0; j < example.get(i).getNumbers.length; j++){
         if(example.get(i).getNumbers()[j] > number &&
         example.get(i).getNumbers()[j] <= numbers)
            System.out.println(example.get(i));
  }
}

Edit: Changed how I worded some things and fixed the code of what I think I should do.

4
  • Format your code correctly by indenting - the way you're presenting it now is also an example of bad coding. Commented Nov 7, 2016 at 5:49
  • Your question is also badly structured, consider editing. Include important classes that you use like what class is your example, and what is ListArray? Commented Nov 7, 2016 at 5:58
  • I edited the way I worded things. Hope it makes more sense now. If not please point it out and I'll try to fix it. Commented Nov 7, 2016 at 6:24
  • @Touchpad You can look at the code below, you need to modify the Numbers class as below to hold two elements Commented Nov 7, 2016 at 6:46

1 Answer 1

1

The code below works but I'm told it's consider bad coding to code entering the indexes. I want to know how to output the array from getNumber method without knowing the array length of getNumber ?

If you don't want to do the validations with array indexes for your first element and second element in the array, then you can solve the problem by modifying your Numbers class as shown below:

(1) Define two int variable members (currently you have only one)

(2) Add a method isInLimits(int input) to validate the range

(3) Override toString() which can be used to print the object as String

Numbers class (modified):

public static class Numbers {

        private int firstElement;
        private int secondElement;

        public int getFirstElement() {
            return firstElement;
        }
        public void setFirstElement(int firstElement) {
            this.firstElement = firstElement;
        }
        public int getSecondElement() {
            return secondElement;
        }
        public void setSecondElement(int secondElement) {
            this.secondElement = secondElement;
        }

        //checks the input is in the range of this object elements
        public boolean isInLimits(int input) {
            if(input >= firstElement && input < secondElement) {
                return true;
            } else {
                return false;
            }
        }

        @Override
        public String toString() {
            return "{"+firstElement+","+secondElement+"}";
        }
    }

Usage of Numbers Class:

public static void main(String[] args) {

      int userInput = 10; //get it from user 

      List<Numbers> example = new ArrayList<>();
      //Add Numbers objects to example list

       for(int i=0;i< example.size();i++) {
            Number numberTemp = example.get(i);
            //call Numbers object's isInLimits
            if(numberTemp.isInLimits(userInput)) {
                System.out.println(numberTemp);
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.