By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,573 Members | 938 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,573 IT Pros & Developers. It's quick & easy.

using arrays

P: 3
create a class called Findsum
using arrays and the scanner.
store numbers into the array and use a method called find_sum(int [] arrayNumbers), that takes the array as an argument and return the total/sum of array elements
Aug 10 '15 #1
Share this Question
Share on Google+
5 Replies

Expert 100+
P: 785
OOOps ... You just copied your assignment.
So where is your problem?
Can you tell us what you have done so far (list code here) and where you are stuck?
Aug 10 '15 #2

P: 3
I copied as it is because it was one the question i was asked by my lecturer
am just new in the course and am bit struggling.........
Aug 10 '15 #3

Expert 100+
P: 785
No problem Onalenna, everyone was a beginner once.
I hope you read the forum guidelines and knew from it that we do not do your homework for you, else you do not learn. And learning you want, else you would not do the course. And you can only learn by trying yourself and discuss your upcoming problems with others. So tell us what specific is it that you are struggling with. Show us what you have done so far.
Aug 11 '15 #4

P: 3
word taken and got into consideration and thank you very much too much appreciating it so here i tried the code but am not sure that is okay according to the question above..

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  3. public class Main {
  5.     public static void main(String[] args) {
  6.         Scanner in=new Scanner (;
  7.         int num[]=new int[5];
  8.         int i=0;
  9.         int sum=0;
  11.         for (i=0;i<num.length;i++) {
  12.             System.out.println("enter a number");
  13.             num[i]=in.nextInt();
  14.             sum=sum+num[i];
  15.         }
  16.         System.out.println("Total="+sum);
  17.     }
  18. }
Aug 13 '15 #5

Expert 100+
P: 785
Hi Onalenna,
the program looks promising. But you did 2 task in your for-loop in each step. First task: you scan in the integer (line 13) and second task: you build the sum (line 14).

So you should separate the tasks and use 2 for-loops. One for the first task, and one for the second task. You can do that by copying the whole loop (line 11 to 15) and place the copy right below the end of the loop. Then delete the second task (sum) from the first for-loop. Then delete the first task from the second copied loop.

So, now that you have the 2 tasks separated, you can just put the second loop into its own method easily, as described in your assignment. If you are using Eclipse, it will do it for you automatically by marking the second loop as selected, then right-click --> refactor --> extract method.

And if you want to become professional, then you can clean-up a bit and replace the second loop with a newer for-each loop
Aug 14 '15 #6

Post your reply

Sign in to post your reply or Sign up for a free account.