Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Validating an Int array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Validating an Int array" Watch "Validating an Int array" New topic
Author

Validating an Int array

jin kazama
Greenhorn

Joined: Mar 09, 2010
Posts: 7
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
Sheriff

Joined: Sep 28, 2004
Posts: 18532
    
  40


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

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ragav Baskaran
Ranch Hand

Joined: Nov 04, 2007
Posts: 36
How about pushing the Integers in a ArrayList<Integer> and call its contains(obj) method to find the match ?

Thanks,
Ragav.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

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[]).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
jin kazama
Greenhorn

Joined: Mar 09, 2010
Posts: 7
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

Joined: Mar 10, 2010
Posts: 9
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

Joined: Sep 28, 2004
Posts: 18532
    
  40

jin kazama wrote:
but now I am struggling to have the method iterate through both arrays.


In pseudo code....

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
Welcome to the Ranch Syed Ullah.

Sorry for not noticing you earlier; had a busy day yesterday.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Validating an Int array
 
Similar Threads
simple sorting algorithm
Understanding Multi-dimension Arrays
Subtle Array question
I am a real JAVA beginner, please take a look at this code.
Something i'm extremely confused on