aspose file tools*
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
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: 18842
    
  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: 19696
    
  20

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: 18842
    
  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: 38859
    
  23
Welcome to the Ranch Syed Ullah.

Sorry for not noticing you earlier; had a busy day yesterday.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Validating an Int array