0

I have 2 methods in my program, one to add ***** above and below the smallest int in the array and one to add %%%%% above and below the largest. The method for the largest is essentially the same as the other but for some reason isn't adding what is needed.

Here is the smallest element method:

public static ArrayList smallestElement() {

        int smallest = array[0];

        for (int i = 0; i < array.length; i++) 
            if (array[i] < smallest) 
                smallest = array[i];

        String smallestString = String.valueOf(smallest);

        ArrayList<String> list = new ArrayList<String>();

        for(int i = 0; i < array.length; i++) {
            if (smallestString.equals(String.valueOf(array[i]))) {
                list.add("*****"); 
                list.add(Integer.toString(array[i]));
                list.add("*****");
            } else {
                list.add(Integer.toString(array[i]));
            }


}

            return list;

    }

Here is the method for the largest element:

public static ArrayList largestElement() {

        int largest = array[0];

        for (int i = 0; i < array.length; i++) 
            if (array[i] > largest) 
                largest = array[i];

        String largestString = String.valueOf(largest);

        for(int i = 0; i < array.length; i++) {
            if (largestString.equals(String.valueOf(array[i]))) {
                smallestElement().add("%%%%%"); 
                smallestElement().add(Integer.toString(array[i]));
                smallestElement().add("%%%%%");
            } else {
                smallestElement().add(Integer.toString(array[i]));
            }
        }

        System.out.println(smallestElement());
        return smallestElement();
    }


}

If anyone knows why this isn't performing correctly, I would really appreciate the help

3
  • 1
    Where is array declared and instantiated? Also, do not name your variables things like array and list Commented Jan 12, 2017 at 20:18
  • 1
    You are comparing ints so why do you cast them to Strings? And that's only to parse them back to Integer while adding to list.. Commented Jan 12, 2017 at 20:19
  • 2
    Every time you call smallestElement(), you're creating a new List. Commented Jan 12, 2017 at 20:19

3 Answers 3

4

You are creating a new object every time you are executing the smallestElement function. Instead do something like,

ArrayList<String> list = smallestElement();

Then use this list object every time you are calling smallestElement() method

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

Comments

0

You have already created the list 3 times over by this line

smallestElement().add("%%%%%"); 
smallestElement().add(Integer.toString(array[i]));
smallestElement().add("%%%%%");

Create just 1 list and use it instead of calling the smallestelementelement() function multiple times

Comments

0

You are overcomplicating things here. There is no need to turn that minimum array value into a string right there (and to then do String comparisons later on). Btw: those string comparisons are also your problem: your code will definitely not work when your minimal value shows up several times in your array - because your code will put in those patterns for each match!

Instead, you could do something like:

int indexToUse = 0;

for (int i = 0; i < array.length; i++) { // please always use braces!
 if (array[i] < array[indexToUse]) {
   indexToUse = i;
  }
}

List<String> valuesWithMarkerStrings = new ArrayList<>();
for (int i = 0; i < array.length; i++) {
  if (i == indexToUse -1 || i == indexToUse+1) {
    valuesWithMarkerStrings.add("******");
  } else {
    valuesWithMarkerStrings.add(Integer.toString(array[i]);
  }
}

(where my solution assumes that you want to have *** ... instead of array[i] for such rows ... )

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.