• Post Reply Bookmark Topic Watch Topic
  • New Topic

Validating an Int array  RSS feed

 
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?
 
author
Sheriff
Posts: 23329
125
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
 
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.
 
Sheriff
Posts: 21204
87
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.
 
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
Sheriff
Posts: 23329
125
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....

 
Marshal
Posts: 57276
175
  • 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.
 
Yes, my master! Here is the tiny ad you asked for:
The WEB SERVICES and JAX-RS Course
https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!