This discussion is archived
4 Replies Latest reply: Jan 8, 2013 10:04 AM by baftos RSS

need algorthim help

user575089 Newbie
Currently Being Moderated
I have int[] intValues

I want to write a method.

boolean method(intValues){

method logic is:

// if all values in the intValues are 4 then return true

// if all values in the intValues are 5 then return true

// else return false

}


my issue is how do I check if all values in the intValues are 4 or 5 ? I want to use a loop

Is there any solution ?
  • 1. Re: need algorthim help
    gimbal2 Guru
    Currently Being Moderated
    user575089 wrote:
    Is there any solution ?
    Nope, sorry, can't be done with Java.

    Of course there is a solution for this incredibly simple requirement. You're not going to get the source handed to you so you'll have to keep studying the Java language until you know enough to be able to do this (really, its very simple when you know a little about arrays and loops). Also you may want to stick to the New to Java forum for now.

    EDIT: I don't really get this question. Previously you were asking about regular expressions, quite an advanced topic, yet now it seems you don't know the basics of the basics. How can that be?
  • 2. Re: need algorthim help
    rp0428 Guru
    Currently Being Moderated
    See the Java Tutorial. The section 'The for Statement' explains how to use a 'for' loop and has an example using it to iterate through an int array. All you need to do is add the value check that you want to do.
    http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
    The following program, EnhancedForDemo, uses the enhanced for to loop through the array:
    
    class EnhancedForDemo {
        public static void main(String[] args){
             int[] numbers = 
                 {1,2,3,4,5,6,7,8,9,10};
             for (int item : numbers) {
                 System.out.println("Count is: "
                                    + item);
             }
        }
    }
    The tutorial also has a section for using IF statements for 'Equality, Relational, and Conditional Operators
    http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
  • 3. Re: need algorthim help
    user575089 Newbie
    Currently Being Moderated
    for (int item : numbers) {
    System.out.println("Count is: " + item);
    }
    How to check if all values in the numbers are 4 or 5 ?
    how many lines code you use here to do this.

    (This is an example. I am looking from optimization perspective . I have a long dataset )
  • 4. Re: need algorthim help
    baftos Expert
    Currently Being Moderated
    gimbal2 wrote:
    EDIT: I don't really get this question. Previously you were asking about regular expressions, quite an advanced topic, yet now it seems you don't know the basics of the basics. How can that be?
    Let me guess. Convert the array to a String using Arrays.toString() and validate the String using a regular expression?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points