File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes validating an 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 array" Watch "validating an array" New topic
Author

validating an array

Akshata Alandker
Greenhorn

Joined: Sep 11, 2012
Posts: 19

I have done something like this, to validate the user input array in my program,its seems working correct, but i feel its not the right way to do so, can any one help me in validating it in a good programming manner? thank you.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Welcome to the Ranch.

Why do you feel it's not right? Because you have an empty block after the if-statement? You could avoid that by putting the opposite expression between the ( and ) of the if, and move the code that's in the block of the else-statement to the block of the if-statement.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Akshata Alandker wrote:I have done something like this, to validate the user input array in my program,its seems working correct, but i feel its not the right way to do so, can any one help me in validating it in a good programming manner? thank you.

Well, a few things:
1. Your program prints out "enter valid numbers" and then exits, so the user doesn't have a chance to enter anything.
2. Validating an entire array may be a bit frustrating for a user. Suppose they enter 99 correct values and one bad one: Presumably they now have to start all over again. Depending on your world-view of users, you may think this is perfectly reasonable; but I suspect they won't.
3. How about, instead, validating each value separately; and only adding it to the array if it IS valid?

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Akshata Alandker
Greenhorn

Joined: Sep 11, 2012
Posts: 19
Thanks for the suggestions... i found another solution for the same, its that, I can make separate method for the validation and call it inside the method which uses the array the method looks like this
private boolean isValidInput(int[] marks) {
boolean result = true;
for (int m = 0; m < marks.length; m++) {
if (marks[m] < 0 || marks[m] > 100) {
result = false;
break;
}
}
if its true then the method will execute else it will exit..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Still not happy with that.
  • 1: I think you should have a separate method which allows for the numbers to be entered.
  • 2: As Winston has already said, do that validation of the entry as soon as possible. Don’t let marks of -1 or 101 anywhere near your array in the first place.
  • Search my old pasts for “utility class” and “hasNextInt” If you are lucky, you will find a method for entering numbers from the keyboard. If you are really lucky, you might find a method which kills those two birds with one stone.
    Akshata Alandker
    Greenhorn

    Joined: Sep 11, 2012
    Posts: 19
    The programs I am working on are assignment programs and I don't have to get user input, I am just hard coding the array in the main method and executing them, so this method works fine for me.
    I had another prob validating the length of the array, i tried using similar method as above, which is

    private boolean isValidArray(int[] marks) {
    boolean result = true;

    for (int j = 1; j < marks.length; j++) {
    if (marks[j] > marks[50]) {
    result = false;
    break;
    }
    }
    return result;
    }
    here i have some problem, if I give array input mare then 50 then it works fine, it gives a message 'enter valid inputs' but if i give valid inputs i.e. if i give inputs less then 50 it gives me an error arrayIndexOutOfBounds at the line if (marks[j] > marks[50]). can any one tell me what is wrong in this method??
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38075
        
      22
    Akshata Alandker wrote: . . . I am just hard coding the array in the main method and executing them,
    That looks like dubious design. the main method is for starting the application, not for running real code.
    . . . if i give inputs less then 50 it gives me an error arrayIndexOutOfBounds at the line if (marks[j] > marks[50]). can any one tell me what is wrong in this method??That isn’t an error message, but an Exception.
    The error is in the original code. You are assuming you will have more than 50 elements in the array. Using arr[50] looks even more dubious. Why did you choose the 51st element? Please explain your decision.
    Akshata Alandker
    Greenhorn

    Joined: Sep 11, 2012
    Posts: 19
    Can any one help me with syntax to delete an element from an array? i have an array say [20,59,30,98,0,12,45] i'll sort them using Arrays.sort now i want to delete first element from the sorted and even the last element that is smallest and the largest numbers, please help me in this regards, Thank you.
    Akshata Alandker
    Greenhorn

    Joined: Sep 11, 2012
    Posts: 19
    Campbell Ritchie wrote:
    The error is in the original code. You are assuming you will have more than 50 elements in the array. Using arr[50] looks even more dubious. Why did you choose the 51st element? Please explain your decision.


    yes, that was something silly I did. but I am choosing 50 because my limit for the array is 50 elements, it should not exceed the limit. I got another solution to the prob, its similar i.e am writing it inside a method but this is working fine for me

    private boolean isValidArray(int[] marks) {
    boolean result = true;
    if(marks.length>50){
    result = false;


    }
    return result;
    }
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38075
        
      22
    The way you are changing your code makes it look as if you are guessing.
    Work out what you are trying to compare. Are you simply trying to work out that the array has fewer than 50 elements? There is a much simpler way to do that. These coding conventions are old, but they should allow you to work out that “simpler way”.
    Why do you need to sort the array?
    Why are you removing an element? That is awkward; you would have to create an array one element smaller, and copy the remaining elements into that new array. It might be easier (a lot easier) to use a List.

    You will find your code a lot easier to write if you work out a few of these “why”s first.
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7552
        
      18

    Akshata Alandker wrote:Can any one help me with syntax to delete an element from an array?

    No, because you can't. What you can do is set it to some value that is recoginsed as meaning "deleted" or "has no value" (null?). If you truly want to delete elements, use an ArrayList.

    i have an array say [20,59,30,98,0,12,45] i'll sort them using Arrays.sort now i want to delete first element from the sorted and even the last element that is smallest and the largest numbers, please help me in this regards, Thank you.

    Same as above. What you could do is write a method that returns you another array, containing only those elements you want from the original one.

    Winston
    Akshata Alandker
    Greenhorn

    Joined: Sep 11, 2012
    Posts: 19
    Campbell Ritchie wrote:
    Why do you need to sort the array?
    Why are you removing an element? That is awkward; you would have to create an array one element smaller, and copy the remaining elements into that new array. It might be easier (a lot easier) to use a List.



    In my prob statement i have example inputs and expected outputs, I have to write the code and use same inputs, so I have to give the same elements in the array, I should sort the array and delete the first and last element as per the problem, I know it is lot easy to use 'list' but according to the statement I have to use array only. but I am unable to find how to delete element from the array, please help
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38075
        
      22
    You have already been told you can’t delete an element from an array. I have told you what you could do already once: create a new array one smaller and copy all the other elements into it. There is a method in the System class which may make that easier.
     
    jQuery in Action, 2nd edition
     
    subject: validating an array
     
    Similar Threads
    Pls help on this read Input
    getting problem in creating an image from int array
    Re: Urgent with this program, Pls help
    Read input,but got error
    help in fixing the error