# Help determining data type

**URL:** <https://discourse.processing.org/t/help-determining-data-type/26233>\
**Category:** Coding Questions\
**Created:** [December 14, 2020, 3:03pm UTC](https://discourse.processing.org/t/help-determining-data-type/26233 "2020-12-14T15:03:24Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 14, 2020, 3:03pm UTC](https://discourse.processing.org/t/help-determining-data-type/26233/1 "2020-12-14T15:03:24Z")

</div>

Hi!  
I started doing the java coding challenges from [edabit.com](http://edabit.com). There is a challenge where you need to filter out Strings from an array and only leave numbers. [https://edabit.com/challenge/vNszi3mbJA5BhgGFX](https://edabit.com/challenge/vNszi3mbJA5BhgGFX)  
( turn [4,123,124,“aaa”,14,553,22,42,1,“bbb”,…] -\> [4,123,124,14,553,22,42,1,…] ) what kind of data type should I use to make it? Should I just do it with strings like 4 = “4” while string “aaaaa” = ‘aaaaa’? What do you think?

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [December 14, 2020, 5:11pm UTC](https://discourse.processing.org/t/help-determining-data-type/26233/2 "2020-12-14T17:11:11Z")

</div>

> [@CodeMasterX](#):
>
> you need to filter out Strings from an array and only leave numbers

How is this array given? In a text file?  
I would try to store them in a generic object ArrayList, which then is easily filtered.

```auto
void setup() {
  ArrayList<Object> obj_list = new ArrayList <Object>();  
  ArrayList<Float> numbers = new ArrayList <Float>();  
  obj_list.add("ddd");
  obj_list.add(2);
  obj_list.add(11122.33);    
  for (int i = 0; i < obj_list.size(); i++) {
    println(obj_list.get(i));
    Object obj = obj_list.get(i);
    try {
      Float a_float = Float.parseFloat(obj.toString());
      numbers.add(a_float);
    } 
    catch (Exception e) {     
      println("Not a number!");
    }
  }
  printArray(numbers);
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 15, 2020, 5:31pm UTC](https://discourse.processing.org/t/help-determining-data-type/26233/3 "2020-12-15T17:31:54Z")

</div>

This is my Java attempt on that positive number filter challenge: ☕

## “Program.java”:

```java
import java.util.Collection;
import java.util.LinkedHashSet;

public final class Program {
  @SafeVarargs static public final String[] filterArray(final String... strs) {
    if (strs == null || strs.length == 0) return new String[0];

    final Collection<String> nums = new LinkedHashSet<>();

    for (final String str : strs) try {
      Integer.parseUnsignedInt(str);
      nums.add(str);
    }

    catch (final NumberFormatException notPositiveNumber) {
    }

    return nums.toArray(new String[nums.size()]);
  }
}

```

But b/c I haven’t created an account on [Edabit.com](http://Edabit.com) yet I couldn’t check my code there. 🥴

However I’ve come up with this PDE sketch to test my “Program.java” file on Processing: 😳

```auto
void setup() {
  println(Program.filterArray("1", "2", "a", "b"));
  println(Program.filterArray("1", "a", "b", "0", "15"));
  println(Program.filterArray("1", "2", "aasf", "1", "123", "123"));
  println(Program.filterArray("jsyt", "4", "yt", "6"));
  println(Program.filterArray("r", "5", "y", "e", "8", "9"));
  println(Program.filterArray("a", "e", "i", "o", "u"));
  println(Program.filterArray("4", "z", "f", "5"));
  println(Program.filterArray("abc", "123"));
  println(Program.filterArray("$%^", "567", "&&&"));
  println(Program.filterArray("w", "r", "u", "43", "s", "a", "76", "d", "88"));
  exit();
}

```

```auto
1 2
1 0 15
1 2 123
4 6
5 8 9

4 5
123
567
43 76 88

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [December 15, 2020, 7:38pm UTC](https://discourse.processing.org/t/help-determining-data-type/26233/4 "2020-12-15T19:38:27Z")

</div>

> [@GoToLoop](#):
>
> This is my Java attempt

I thought that the challenge was [4,123,124,“aaa”,14,553,22,42,1,“bbb”,…] , meaning not only strings in the array to filter. Anyway, I don’t know what the full challenge is, but it seems more like a P5.js mixed objects array; so a topic with the wrong tag…

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 15, 2020, 7:44pm UTC](https://discourse.processing.org/t/help-determining-data-type/26233/5 "2020-12-15T19:44:01Z")

</div>

> [@noel](#):
>
> ; so a topic with the wrong tag…

If we follow the OP’s link: 🔗  
[Edabit.com/challenge/vNszi3mbJA5BhgGFX](http://Edabit.com/challenge/vNszi3mbJA5BhgGFX)

The selected category is “Java”. Although we can click it to switch it for another language. 😉

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [December 15, 2020, 7:54pm UTC](https://discourse.processing.org/t/help-determining-data-type/26233/6 "2020-12-15T19:54:35Z")

</div>

Oh, I see, but I don’t get it. This doesn’t look like a java function, with strings and numbers that way in the array. There’s something wrong with that.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 15, 2020, 7:58pm UTC](https://discourse.processing.org/t/help-determining-data-type/26233/7 "2020-12-15T19:58:58Z")

</div>

> [@noel](#):
>
> There’s something wrong with that.

Seems like those challenges are presented in some language-agnostic terms.  
Then we have to solve them restricted to our chosen language syntax.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [December 15, 2020, 8:29pm UTC](https://discourse.processing.org/t/help-determining-data-type/26233/8 "2020-12-15T20:29:43Z")

</div>

> [@GoToLoop](#):
>
> we have to solve them restricted to our chosen language syntax

Well if we may write it as a string I would solve it this way.

```auto
import java.util.Arrays;
String str = "4,123,124,aaa,14,seila,553,22,42,1,bbb";   
str = str.replaceAll("[^-?0-9]+", " "); 
println(Arrays.asList(str.trim().split(" ")));

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 15, 2020, 10:43pm UTC](https://discourse.processing.org/t/help-determining-data-type/26233/9 "2020-12-15T22:43:42Z")

</div>

> [@noel](#):
>
> … I would solve it this way.

In order to check whether your code returns the expected values you’re gonna need to sign in on their site, go to the challenge, click at the “Code” tab, paste your solution and hit the “Check” button. 💦

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [December 15, 2020, 10:46pm UTC](https://discourse.processing.org/t/help-determining-data-type/26233/10 "2020-12-15T22:46:57Z")

</div>

😊 Thanks, but I find this forum the best place to learn.

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 15, 2020, 10:51pm UTC](https://discourse.processing.org/t/help-determining-data-type/26233/11 "2020-12-15T22:51:57Z")

</div>

barging into conversation but I’m honesly running out of ideas. I couldn’t get ideas for a year at some point. The projects were either too hard or too easy. It was nice to find a site that gives ideas to me. BTW do you have any interesting projects that aren’t at insane difficulty?

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [December 15, 2020, 11:01pm UTC](https://discourse.processing.org/t/help-determining-data-type/26233/12 "2020-12-15T23:01:29Z")

</div>

> [@CodeMasterX](#):
>
> do you have any interesting projects that aren’t at insane difficulty?

You might like to join the [Rosetta Examples development club](https://discourse.processing.org/t/rosetta-examples-development/18945). Plenty of tasks [to still fulfill](http://www.rosettacode.org/wiki/Reports:Tasks_not_implemented_in_Processing) on the Rosetta page.

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [December 15, 2020, 11:16pm UTC](https://discourse.processing.org/t/help-determining-data-type/26233/13 "2020-12-15T23:16:49Z")

</div>

Also since the beginning of december there’s the [codecember](https://codecember.ink/) event.

The idea is to do one processing sketch a day based on a concept or another artist work. This is actually really interesting and I can just encourage you to check it out!

The creators Pine and Anthony are giving great resources each day on a different subject and it’s a great way to learn new skills.

I am doing it right now but it needs more popularity 😉

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 16, 2020, 4:31am UTC](https://discourse.processing.org/t/help-determining-data-type/26233/14 "2020-12-16T04:31:28Z")

</div>

Well, finally made up my mind and made an account on [Edabit.com](http://Edabit.com). 🧠  
And my Java solution simply worked on 1st try! 🥳  
Also coded solutions for JS & Python: 🐍

## JS:

```javascript
function filterArray(arr) {
  return arr.filter(Number.isInteger);
}

```

## Python:

```auto
from collections import OrderedDict

def isnt_str(obj): return type(obj) is not str

def filter_list(lst): return list(filter(isnt_str, OrderedDict.fromkeys(lst)))

```

And this is Processing’s output test for my Python solution: 😹

```auto
def setup():
    print filter_list([1, 2, "a", "b"])
    print filter_list([1, "a", "b", 0, 15])
    print filter_list([1, 2, "aasf", "1", "123", 123])
    print filter_list(["jsyt", 4, "yt", "6"])
    print filter_list(["r", 5, "y", "e", 8, 9])
    print filter_list(["a", "e", "i", "o", "u"])
    print filter_list([4, "z", "f", 5])
    print filter_list(["abc", 123])
    print filter_list(["$%^", 567, "&&&"])
    print filter_list(["w", "r", "u", 43, "s", "a", 76, "d", 88])
    exit()

```

---

<div class="post-metadata">

**Author:** ![PHuzzeyMusic](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@PHuzzeyMusic](https://discourse.processing.org/u/PHuzzeyMusic)\
**Post date:** [December 19, 2020, 11:57pm UTC](https://discourse.processing.org/t/help-determining-data-type/26233/15 "2020-12-19T23:57:03Z")

</div>

A little late to this and I am not sure if PDE has full stream support yet, but here is my java solution

```auto
public static Integer[] filterStrings(List<?> genericList) {
        return genericList.stream()
                .filter(entry -> entry instanceof Integer)
                .toArray(Integer[]::new);
    }

```

Then my simple demo program

```auto
public static void main(String[] args) {
        List<Object> input = Arrays.asList(1, 2, 3, "aa", 'b', "ccc");
        Integer[] output = filterStrings(input);
        System.out.println(Arrays.toString(output));
    }

```
