-1

I'm trying to geet my program to ignore the strings in an array which contains all sorts of special characters and print the ones which do not contain any the array that I'm try to get my program to read is as follows

TestCase[] testCases = { 
      new TestCase("", false),
      new TestCase("5875123699", true),
      new TestCase("123456", false),
      new TestCase("12312312S1", false),
      new TestCase("1234567841", true),
      new TestCase("12312312312", false),
      new TestCase("2222222222", false),
      new TestCase("2222222232", true),
      new TestCase("-875123699", false),
      new TestCase("58751236.9", false),
      new TestCase(null, false),`
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Adam Dewhurst
  • 91
  • 1
  • 1
  • 7
  • 5
    Every character is special. – Wietlol Apr 03 '17 at 13:48
  • 2
    What is the condition that makes a string of characters return true in this test? – TEK Apr 03 '17 at 13:50
  • Did you try anything? Come up with any idea? What is your exact issue? What is the specific question? – domsson Apr 03 '17 at 13:50
  • 2
    Oh, and one unrelated piece of advice: you haven't marked any answer as accepted on any of your previous questions. This makes it less likely for people to want to help you in the future, so you might want to check up on those. – domsson Apr 03 '17 at 13:53
  • Also, what does *"ignore"* mean? You want to delete those? Or just exclude them when you process the array in some way? – domsson Apr 03 '17 at 14:15
  • just exclude them – Adam Dewhurst Apr 03 '17 at 14:17
  • 1
    Now we still need an answer to @TEK's question. What are valid strings, what aren't? I thought you want [0...9], but then again "123456" apparently fails your test. I can't see a pattern here. Also, you still haven't provided any attempt whatsoever. As is, it is *unclear what you're asking*. – domsson Apr 03 '17 at 14:23

4 Answers4

1

Not sure what you need (what is SPECIAL character in your case) but if you want just to pass digits try to use one of these: How to check if a String is numeric in Java

Community
  • 1
  • 1
Akceptor
  • 1,914
  • 3
  • 26
  • 31
  • what I mean by special character is characters such as £ , $ , % etc – Adam Dewhurst Apr 03 '17 at 13:53
  • @AdamDewhurst You mean non-numeric? So it can only contain the digits `0-9`? – TEK Apr 03 '17 at 13:59
  • There are no such characters in the `testCases` array you showed us. Please edit your question with details on what characters could be in the input and what characters you want to allow / disallow. – domsson Apr 03 '17 at 14:02
  • theres a - in the second to last one and a full stop in the last one – Adam Dewhurst Apr 03 '17 at 14:15
  • And why would "123456" and "2222222222" fail your test? What about latters [a..z]? You really need to define "special characters". Please check the comments on your question, then edit accordingly. – domsson Apr 03 '17 at 14:18
0

You need to proceed carfully since you have fields that can be null...(like the last TestCase)

assuming that, you can filter using java 8 and the criteria is that the String match the numeric only pattern, your filter can be improved, modified by changing that filter only...

Object[] t = Arrays.stream(testCases).filter(x -> 
          x.getName() != null && x.getName().matches("\\d+") 
 ).toArray();
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

From your comment I guess you want to check if your Strings contain non-alphanumeric characters. An answer to that is here.

To apply this to an array you would have to check for every element in your array. Either with a simple for loop or you could use e.g. Streams for that. Since for loop is rather simple, I'll show the stream solution. This goes through all the strings, than each character in each string and filters non alphanumeric strings:

String [] array = new String[]{"asdf", ", ", "jklö", "%&"};
String[] filtered = Stream.of(array).filter(s -> s.chars().allMatch(Character::isLetterOrDigit)).toArray(String[]::new);
//will print [asdf, jklö]
System.out.println(Arrays.toString(filtered));

If you'd want to adapt the filtering to exclude for example ö which I used (it is alphanumeric, but kind of special) just replace the isLetterOrDigit function with one of your own that checks for that.

Oh and unrelated as domdom mentioned in his comment, you should really mark correct answers on your old questions as correct.

Community
  • 1
  • 1
findusl
  • 2,454
  • 8
  • 32
  • 51
0

I think you should use pattern and matcher with some regular expressions to check if your string contains some special character.

Anyways, I guess the next link will provide you the best solution.

check a string if there is a special character in it

Community
  • 1
  • 1