-3

I'm new to Java and working on a basic program that looks through an array and gives prints the amount of numbers in the array that are divisible by 3. I'm having some trouble getting it to work right. Here is the code that I've got so far.

package arraysearch;

public class Intsearch {

    public static void main(String[] args) {

    }

    public static void multiple_3 (int[] a, int b)  {
        b=0;        
    }
    {
        int[] numarray ={3, 9, 45, 88, 23, 27, 68};
        {
            if (numarray % 3)==0;
                b = b+1;
        }
        System.out.println("This is the amount of numbers divisible by 3:" +b)
    }   
}
user unknown
  • 35,537
  • 11
  • 75
  • 121
mattj.ware
  • 13
  • 1
  • 1
  • 3
  • 1
    You should really go through a tutorial on the proper syntax of if statements and on how to use for loops. – trutheality Jun 06 '11 at 02:52
  • Can you tell us what is not working right? You never call multiple_3 so it will never execute. – mrtsherman Jun 06 '11 at 02:52
  • 1
    Could you be more specific about what errors you're getting exactly? Are they compile time errors or run-time errors? –  Jun 06 '11 at 02:55
  • You should work on your indentation, on if-statements (their core syntax), explain, what an empty main-method is useful for (make the compiler happy?). b = b + 1 is, by the way equivalent to b += 1 which is the same as ++b given no other code around. – user unknown Jun 06 '11 at 03:01
  • you should not have } { after b =0; – Rudy Jun 06 '11 at 03:10
  • As this, your program don't even compile... The problem for me is that it seem that you don't understand java syntax, and thus it is likely that you'll not really benefit from a working solution. What you really need is to read a gentle introduction to java syntax and do the exercices by yourself. – Nicolas Bousquet Jun 06 '11 at 08:33

3 Answers3

5

Try this (Java 7):

public static void main(String[] args) {
    multiple_3(new int[] { 3, 9, 45, 88, 23, 27, 68 });
}

public static void multiple_3(int[] ints) {
    int count = 0;
    for (int n : ints) {
        if (n % 3 == 0) {
            count++;
        }
    }
    System.out.println("This is the amount of numbers divisible by 3: " + count);
}

Java 8 update:

public static void multiple_3(int[] ints) {
    long count = IntStream.of(ints).filter(n -> n % 3 == 0).count();
    System.out.println("This is the amount of numbers divisible by 3: " + count);
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Thank you! The code works perfectly, and I see what all I was doing wrong now. I'm not sure how, but I completely forgot about the ++ way to increment a variable like that. – mattj.ware Jun 07 '11 at 02:52
  • Sorry, I realise it's largely a style thing, but I edited the code to use brackets. I think beginners should be advised to always use them; experts can drop them later if they like. – Adrian Mouat Dec 04 '13 at 20:59
0

You'll need a for loop to evaluate each item in the array sequentially:

 int[] numarray = { 1, 2, 3 };
 for (int i = 0; i < numarray.Length; i++)
 {
     if (numarray[i] % 3 == 0)
     {
         b++;
     }
 }
jonsca
  • 10,218
  • 26
  • 54
  • 62
  • @mods: I goofed on the edit summary due to an autocomplete, I had only fixed the code tags. Sorry. – jonsca Jun 06 '11 at 03:08
  • `numarray.Length;` isn't Java - maybe dot-net? However - the simplified for-loop would fit well here: `for (int i: numarray) if (i % 3 == 0) ++count;` To use `i` for an index is fine, to use b for a count is not. – user unknown Jun 06 '11 at 20:25
  • Thank you! That really helped. The numarray.Length is sort of beyond my skill level right now but it does work. – mattj.ware Jun 07 '11 at 02:51
0

Please try :

   int b=0;        

   int[] numarray ={3, 9, 45, 88, 23, 27, 68};

   for ( int i=0; i<numarray.length; i++)
   {   
       if (numarray[i]%3==0)
           b++;
   }   
   System.out.println("This is the amount of numbers divisible by 3:" +b)
sudipto
  • 2,472
  • 1
  • 17
  • 21
  • The simplified for-loop would fit well here: for (int i: numarray) if (i % 3 == 0) ++count; To use i for an index is fine, to use b for a count is not. – user unknown Jun 06 '11 at 20:26
  • @user unknown: Thanks! The reason behind using b is not from naming-convention world but for Mr. mattj.ware who is trying with variable named b and not to confuse him much with another name (where name is secondary to the logic sought for in this context) – sudipto Jun 06 '11 at 20:41
  • @user unknown: Seems Mr. mattj.ware who is trying basics of Java like yours truly, I find a closer kinship with these usage as it seems with Mr. mattj.ware. We would surely reach to higher levels soon once we clear the basics – sudipto Jun 06 '11 at 20:52
  • Thank you for your input, I was able to figure out what all I was doing wrong. – mattj.ware Jun 07 '11 at 02:50