eightmoons

NumberRetriever.java

Feb 16th, 2021
625
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class NumberRetriever{
  2.  
  3.     public static ArrayList<Integer> getNumberFromString(String input) {
  4.         ArrayList<Integer> ints = new ArrayList<>();
  5.         for (char a : input.toCharArray()) {
  6.             try {
  7.                 ints.add(Integer.parseInt(String.valueOf(a)));
  8.             }catch (NumberFormatException ignored) { }
  9.         }
  10.         return ints;
  11.     }
  12.  
  13.     public static int getResultFromNumber(ArrayList<Integer> numbers) {
  14.         boolean isEven = numbers.get(numbers.size()-1) % 2 == 0;
  15.         int result = isEven ? 1 : 0;
  16.         for (int num: numbers) {
  17.             if (isEven) result *= num;
  18.             else result += num;
  19.         }
  20.         return result;
  21.     }
  22.  
  23.     public static void main(String[]args){
  24.         ArrayList<Integer> numberList = getNumberFromString("a6s.,<Mv3d4_34(()2^&24!5");
  25.         int result = getResultFromNumber(numberList);
  26.         System.out.println(numberList);
  27.         System.out.println(result);
  28.     }
  29. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×