0

I am trying to find the largest number in a string array that includes letters. How do I remove elements that aren't numeric from the array, whilst its in a For loop?

String word = "abcd:1234";
String[] testArray = test2.split("");


int max = Integer.MIN_VALUE, maxIndex = 0;
int secondMax = Integer.MIN_VALUE;

for (int i = 0; i < testArray.length; i++) {
    if (Integer.parseInt(testArray[i]) > max) {
        secondMax = max;
        max = Integer.parseInt(testArray[i]);
        maxIndex = i;
    }else if(secondMax < Integer.parseInt(testArray[i])){
        secondMax = Integer.parseInt(testArray[i]);
    }
}
System.out.println(secondMax);
Butiri Dan
  • 1,759
  • 5
  • 12
  • 18
  • 3
    Possible duplicate of [How to check if a String is numeric in Java](https://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-numeric-in-java) – Matthew Jun 27 '19 at 12:26
  • If I were doing this, I would split the string on letters using [regular expressions](https://stackoverflow.com/questions/9856916/java-string-split-regex) that match everything but numbers - so it would split on non-numbers, resulting in an array of numbers-only. – JeremyW Jun 27 '19 at 12:27

4 Answers4

3

you can use Java 8 stream for this

int max = word.chars()
            .filter(Character::isDigit)
            .map(Character::getNumericValue)
            .max()
            .orElse(Integer.MAX_VALUE);
Adrian
  • 2,984
  • 15
  • 27
1

Or without lambda

String word = "abcd:1234";
int max = Integer.MIN_VALUE, maxIndex = 0;

for (int i = 0; i < word.length(); i++) {
    if (Character.isDigit(word.charAt(i))) {
        int number = Character.getNumericValue(word.charAt(i));
        if(number > max) {
            max = number;
            maxIndex = i;
        }
    }
}
System.out.println("Max number: " + max);
System.out.println("Max index: " + maxIndex);

Output

Max number: 4
Max index: 8
Butiri Dan
  • 1,759
  • 5
  • 12
  • 18
1

To remove the elements from a String array that do not conform to integers, you can do the following:

      // simple method that doesn't reply on exception handling
      String[] vals = { "amdjd", "22arss", "2213", "22133", "alal", "aala", "33"
      };
      List<String> list = new ArrayList<>();
      for (String s : vals) {
         // check to ensure all characters are digits.
         if (s.replaceAll("\\d", "").equals("")) {
            list.add(s);
         }
      }
      String[] result = list.toArray(new String[0]);
      System.out.println(Arrays.toString(result));

Sometimes, it helps to have a helper method make the whole process cleaner. Both of the following will filter out anything that isn't a number.

      List<String> list = new ArrayList<>();
      for (String s : vals) {
         if (isANumber(s)) {
            list.add(s);
         }
      }
      result = list.toArray(new String[0]);
      System.out.println(Arrays.toString(result));

Using java 8+ streams.

      result = Arrays.stream(vals).filter(a -> isANumber(a)).toArray(
            String[]::new);
      System.out.println(Arrays.toString(result));

Helper method to test whether string is a number.

   private static boolean isANumber(String s) {
      try {
         Double.parseDouble(s);
         return true;
      }
      catch (IllegalArgumentException ne) {
         return false;
      }
   }
WJS
  • 36,363
  • 4
  • 24
  • 39
0

You can use isNumeric() method to check if your array element contains letters or not

Paolo Mossini
  • 1,064
  • 2
  • 15
  • 23