array example containing 9s

Hi ,

I am trying below array example containing 9s
http://codingbat.com/prob/p184031


I wrote as below but could not proceed further. Please advise on how to proceed and complete this.

public class Test22 {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub

      }
      public int arrayCount9(int[] nums) {
              int[] arrayCount9= {1, 2, 9};
             
              System.out.println("9s are"+arrayCount9.
             
             
            }
}
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

krakatoaCommented:
public int arrayCount9(int[] nums) {
  int numnines=0;
  for(int n=0;n<nums.length;n++){if(nums[n]==9){numnines++;}}
  return numnines;
}

Open in new window


or this . . .

public int arrayCount9(int[] nums) {
  int numnines=0;
  int ninetally=0;
  for(int n=0;n<nums.length;n++){if (!(nums[n]==9)){nums[n]=0;}}

  for(int n =0;n<nums.length;n++){ninetally=ninetally+nums[n];}
  
  return ninetally/9;
  
}

Open in new window


although this would be preferable . . .

public int arrayCount9(int[] nums) {

  int ninetally=0;
  for(int n=0;n<nums.length;n++){ninetally= nums[n]==9?ninetally+9:ninetally ;}
  return ninetally/9;
  
}

Open in new window

0
gudii9Author Commented:
public class Test22 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		arrayCount9;//how to call array from main method??

	}
	public int arrayCount9(int[] nums) {
		  int[] arrayCount9= {1, 2, 9};
		int numNine=0;
		  for(int i=0;i<=arrayCount9.length;i++){
			  if(arrayCount9[i]==9){
				  
				  numNine=numNine+1;
			  }
		  System.out.println("9s are"+numNine);
		  }
		  
		  return numNine;
		}
}

Open in new window


I wonder how to call this arrayCount9 method from main method.

your first solution is easy to understand to me. One challenge i face while working on different projects is i need to understand different styles of code written by different developers at different past years. That lead me to learn java completely(including different styles of writing code ) even though i like particular simple way of writing code irrespective of my preference. Please advise
0
krakatoaCommented:
how to call this arrayCount9 method from main method

without sounding indelicate, I thought that it was already crystal clear how you call a method from within a class or instance?? We have covered the point countless times in other questions.

You build your class, and, if it is for example for test purposes - which it mainly is at this stage for you - then you may as well make methods static, so that you can call them from the static main() method, and save yourself the trouble of having to instantiate an instance of the class, just to try things out.

So what you have above is effectively, that. Now all you need to do is to add the static access qualifier to the method signature, and you can call it from main(). When you call it, you pass the same arguments of course - which in this case is the int array.

As to the other part of your comment above, about styles, I would say that that is a weak approach. You should rather concentrate on understanding the mechanics of the language, and not mistaking how different mechanics are used by different programmers, and conclude that one is a better style than the other. At the moment, I'd say you need to show yourself that you completely understand the for next loop, the while loop, the for-each loop and the ternary operator, and the conditions under which these various elements operate - i.e. begin, end, and produce a result.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

krakatoaCommented:
Now to talk about your code . . .
public class Test22 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

               static int[] nums = {1,2,9};
		// TODO Auto-generated method stub
		//arrayCount9;//how to call array from main method??//like this : arrayCount9(nums);

	}
	public static int arrayCount9(int[] nums) {//declare as static so your main() can call it.
		 // int[] arrayCount9= {1, 2, 9};//[b]Why[/b] are you doing this?? You've already been passed the array - "nums".
		int numNine=0;
		  //for(int i=0;i<=arrayCount9.length;i++){// NO . . do:i<nums.length
			  if(arrayCount9[i]==9){//No . . . use: nums[i]==9
				  
				  numNine=numNine+1;
			  }
		 // System.out.println("9s are"+numNine);// don't need this
		  }
		  
		  return numNine;
		}
}

Open in new window


So now make the corrections marked, and try to run again.
0
gudii9Author Commented:
public class Test22 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		arrayCount9(null);//how to call array from main method??

	}
	public static int arrayCount9(int[] nums) {
		  int[] arrayCount9= {1, 2, 9};
		int numNine=0;
		  for(int i=0;i<=arrayCount9.length;i++){
			  if(arrayCount9[i]==9){
				  
				  numNine=numNine+1;
			  }
		  System.out.println("9s are"+numNine);
		  }
		  
		  return numNine;
		}
}

Open in new window


i know calling a method uisng the object instance without static before method or keeping the static before method and call without object instance.

Where i am struck now is i need to pass {1, 2, 9} from the main method not declaring in arrayCount9.


Please advise
0
krakatoaCommented:
So look at lines 8 and 10 of my last comment.
0
awking00Commented:
You've already seen how a for-each loop works, so let's use it.

public static void main(String[] args) {
            int[] nums= {1, 9, 9, 3, 9};
            System.out.println(arrayCount9(nums));

}
      public static int arrayCount9(int[] nums) {
         int count = 0;
         for (int num : nums) {
            if (num == 9) {
              count++;
            }
        }
            return count;
      }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
krakatoaCommented:
I'm counting myself out of this one, as there doesn't seem to me to be anything more to add to any of it that hasn't been said countless times already, and it's starting to fold back in on itself, in the way that has happened several times before.
0
GeisrudSystems AdministratorCommented:
Is there a problem with simply iterating through a given array and incrementing an integer if a value equals 9?

edit: reading back through other comments, I guess that is what krakatoa did in the very first comment.  Seems the OP is making this harder than it is.
0
gudii9Author Commented:
package eePackage;

public class Test21 {

      public static void main(String[] args) {
        int[] nums= {1, 9, 9, 3, 9};
        System.out.println(arrayCount9(nums));

}
  public static int arrayCount9(int[] nums) {
     int count = 0;
     for (int num : nums) {
        if (num == 9) {
          count++;
        }
    }
        return count;
  }
}

in the above solution i see for each loop. Can i simply use traditional for loop instead like below
for(initialization; condition increment)

Please advise
0
ozoCommented:
Yes, you can.
0
awking00Commented:
>>Can i simply use traditional for loop instead like below<<
I don't see any loop "like below" but, if you mean can I replace "for (int num : nums)" and "if (num == 9)" with
"for (int i = 0; i < nums.length; i++)" and "if( nums[ i] == 9)" then, as ozo stated, of course you can.
0
gudii9Author Commented:
package eePackage;

public class Test23 {

      public static void main(String[] args) {
        int[] nums= {1, 9, 9, 3, 9,8,9};
        System.out.println(arrayCount9(nums));

}
  public static int arrayCount9(int[] nums) {
     int count = 0;
    // for (int num : nums) {
    	 //"for (int i = 0; i < nums.length; i++)" and "if( nums[ i] == 9)" 
    	 // if (num == 9) {
     for (int i = 0; i < nums.length; i++){  
         if (nums[i] == 9) {
          count++;
        }
    }
        return count;
  }
}

Open in new window


I see it is working as above. I personally feel comfortable, easy to understand as above with for loop and if condition rather than foreach loop as commented above.
0
awking00Commented:
>> I personally feel comfortable, easy to understand as above with for loop and if condition rather than foreach loop as commented above. << Not surprising since a loop using a counter, condition and increments is usually the one you learn first and if it works don't fix it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.