• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Validating an Int array

 
jin kazama
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to validate an array of int data to check if the number is a certain number, ie. a mobile number 0 7 7 7 1 8 7 6 5 4 3.

So far the only way I can do this is to iterate through the array and check if each value matches one of the above numbers, eg.

(arrayInts[x] == 1) || (arrayInts[x] == 2) || (arrayInts[x] == 3) etc...

{
return true;
}

Can I ask for advice on a better way of accomplishing this?
 
Henry Wong
author
Marshal
Pie
Posts: 21415
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How about instead of hardwiring the number, simply have a method that compares two arrays? And you can also use a loop too.

Henry
 
Ragav Baskaran
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about pushing the Integers in a ArrayList<Integer> and call its contains(obj) method to find the match ?

Thanks,
Ragav.
 
Rob Spoor
Sheriff
Pie
Posts: 20609
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
How about instead of hardwiring the number, simply have a method that compares two arrays?

It already exists: java.util.Arrays.equals(int[], int[]).
 
jin kazama
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your advice with this task.

I have looked at the equals method and would I be correct if it returned true if the two objects were the same, in this case the two arrays would have to have the same numbers in the same order for it to return true?

I have created a new int array:

int[] validNumbers = {0,1,2,3,4,5,6,7,8,9};

but now I am struggling to have the method iterate through both arrays.
 
Syed Ullah
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still not quite sure what you are trying to do here. Can you elaborate with some examples?

Why are you defining an array with 0-9 digits. Also, why did you use the OR(||) operator in your
comparison? Should it not be the AND(&&) operator?
 
Henry Wong
author
Marshal
Pie
Posts: 21415
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jin kazama wrote:
but now I am struggling to have the method iterate through both arrays.


In pseudo code....

 
Campbell Ritchie
Sheriff
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Syed Ullah.

Sorry for not noticing you earlier; had a busy day yesterday.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic