14 Replies - 446 Views - Last Post: 05 April 2013 - 11:37 AM Rate Topic: -----

#1 sayham  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 73
  • Joined: 21-March 13

Reading given numbers in Array...

Posted 31 March 2013 - 01:07 PM

This is my code, but it is giving me 0 while running. Need help how to compromise main method with the sumArray method. Any help will be appreciated.
/*Complete the method, named sumArray, in the class named ArrayOps.java, so that it sums all the integer values in an array. 
The one parameter coming into the method is an array of integers. The integer value returned is the sum of these integers. 
Complete the following code:*/
   import java.util.Scanner;
   public class ArrayOps
   {
   /**
      This method adds up the integer values in an array,
      with the integer sum returned at the end of the method.
      @param values, an array of integers, may be positive or negative
      @ return, the sum of the integers
   */
      public static int sumArray(int values[])
      {
         int sum = 0;
			for (int n = 0; n < 10; n++)
			{
			sum = sum + values[n];
			}
         /*for (int element : values)
         {
            sum = sum + element;
         }*/
         return sum;
      }
      public static void main(String[] args)
      {
         System.out.print("Enter numbers: ");
         Scanner in = new Scanner(System.in);
         int[] values = new int[20];
         int sjb = sumArray(values);
         System.out.print(sjb);
      }
   }//No.43



Is This A Good Question/Topic? 0
  • +

Replies To: Reading given numbers in Array...

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2735
  • View blog
  • Posts: 11,525
  • Joined: 20-September 08

Re: Reading given numbers in Array...

Posted 31 March 2013 - 01:15 PM

Quote

but it is giving me 0 while running.
Because all the values in your array are zero
Was This Post Helpful? 0
  • +
  • -

#3 sayham  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 73
  • Joined: 21-March 13

Re: Reading given numbers in Array...

Posted 31 March 2013 - 01:19 PM

Thanks g00se, how can I fix that? I appreciate your help.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Reading given numbers in Array...

Posted 31 March 2013 - 01:30 PM

May be something like:

int[] values = {3, 10, 5, 23, 1, 45, 5, 22, 11, 10, 13, 99, 34, 123, 321};


instead of simply
int[] values = new int[20];


This post has been edited by pbl: 31 March 2013 - 01:30 PM

Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2735
  • View blog
  • Posts: 11,525
  • Joined: 20-September 08

Re: Reading given numbers in Array...

Posted 31 March 2013 - 01:31 PM

Quote

how can I fix that? I appreciate your help.
Well you need to put some values in your array. How you do that depends on how your app is meant to work - so i can't say. Random initialization?
Was This Post Helpful? 1
  • +
  • -

#6 sayham  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 73
  • Joined: 21-March 13

Re: Reading given numbers in Array...

Posted 31 March 2013 - 01:38 PM

Exactly, random initialization. Such as, I will put some integers values that not more than 20, and my code will give me the sum. Actually, I am also confused that what my code requires to print.
Was This Post Helpful? 0
  • +
  • -

#7 sayham  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 73
  • Joined: 21-March 13

Re: Reading given numbers in Array...

Posted 31 March 2013 - 01:45 PM

View Postpbl, on 31 March 2013 - 01:30 PM, said:

May be something like:

int[] values = {3, 10, 5, 23, 1, 45, 5, 22, 11, 10, 13, 99, 34, 123, 321};


instead of simply
int[] values = new int[20];


Thanks Paul, my code is good using your suggestion, but I need to know how to random initialize. I appreciate your help.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Reading given numbers in Array...

Posted 31 March 2013 - 01:45 PM

import java.util.Random;

         int[] values = new int[20];
         Random ran = new Random();
         for(int i = 0; i < values.length; ++i)
             values[i] = ran.nextInt(20);
         int sjb = sumArray(values);  // shoul be around 20 * 10 = 200


Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2735
  • View blog
  • Posts: 11,525
  • Joined: 20-September 08

Re: Reading given numbers in Array...

Posted 31 March 2013 - 01:46 PM

Quote

, an array of integers, may be positive or negative

Bearing that in mind, i would suggest you use Random.nextInt() (check api docs)
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Reading given numbers in Array...

Posted 31 March 2013 - 01:51 PM

The OP asked for values not mor than 20 so ran.nextInt(21);

View Postsayham, on 31 March 2013 - 04:38 PM, said:

I will put some integers values that not more than 20, and my code will give me the sum.

Was This Post Helpful? 0
  • +
  • -

#11 sayham  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 73
  • Joined: 21-March 13

Re: Reading given numbers in Array...

Posted 31 March 2013 - 01:57 PM

You guys are awesome, thank you very much. My code is behaving exactly, what I wanted.
Was This Post Helpful? 0
  • +
  • -

#12 ashok.5656  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-April 13

Re: Reading given numbers in Array...

Posted 01 April 2013 - 11:36 AM

hi sayham,

try this,

 import java.util.*;
   public class ArrayOps
   {
       ArrayOps(){
           System.out.print("Enter Total numbers: ");
         Scanner in = new Scanner(System.in);
         int n=in.nextInt();
           System.out.println("Enter numbers:");
           
           ArrayList ar=new ArrayList();
           while(ar.size()<n){
               ar.add(in.nextInt());
           }
           int sum = 0;
            for (int i = 0; i < n; i++)
            {
            sum = sum + Integer.parseInt(String.valueOf(ar.get(i)));
            }
           System.out.println(sum);
      }
      public static void main(String[] args)
      {
         ArrayOps ob=new ArrayOps();
      }
   }




Was This Post Helpful? -1
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Reading given numbers in Array...

Posted 01 April 2013 - 11:46 AM

View Postashok.5656, on 01 April 2013 - 02:36 PM, said:

hi sayham,

try this,

 import java.util.*;
   public class ArrayOps
   {
       ArrayOps(){
           System.out.print("Enter Total numbers: ");
         Scanner in = new Scanner(System.in);
         int n=in.nextInt();
           System.out.println("Enter numbers:");
           
           ArrayList ar=new ArrayList();
           while(ar.size()<n){
               ar.add(in.nextInt());
           }
           int sum = 0;
            for (int i = 0; i < n; i++)
            {
            sum = sum + Integer.parseInt(String.valueOf(ar.get(i)));
            }
           System.out.println(sum);
      }
      public static void main(String[] args)
      {
         ArrayOps ob=new ArrayOps();
      }
   }




This is very bad code
First ArrayList are generic so it should be defined as ArrayList<Integer>
nd it won't work, as you store the element as integer in the ArrayList the
Integer.parseInt() is useless the ArrayList elements are already Integer

And the OP just wanted randomly defined number so your post is completly useless
Was This Post Helpful? 0
  • +
  • -

#14 ashok.5656  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-April 13

Re: Reading given numbers in Array...

Posted 01 April 2013 - 09:14 PM

View Postpbl, on 01 April 2013 - 11:46 AM, said:

View Postashok.5656, on 01 April 2013 - 02:36 PM, said:

hi sayham,

try this,

 import java.util.*;
   public class ArrayOps
   {
       ArrayOps(){
           System.out.print("Enter Total numbers: ");
         Scanner in = new Scanner(System.in);
         int n=in.nextInt();
           System.out.println("Enter numbers:");
           
           ArrayList ar=new ArrayList();
           while(ar.size()<n){
               ar.add(in.nextInt());
           }
           int sum = 0;
            for (int i = 0; i < n; i++)
            {
            sum = sum + Integer.parseInt(String.valueOf(ar.get(i)));
            }
           System.out.println(sum);
      }
      public static void main(String[] args)
      {
         ArrayOps ob=new ArrayOps();
      }
   }




This is very bad code
First ArrayList are generic so it should be defined as ArrayList<Integer>
nd it won't work, as you store the element as integer in the ArrayList the
Integer.parseInt() is useless the ArrayList elements are already Integer

And the OP just wanted randomly defined number so your post is completly uselessh



Hello Pbl,
Thanks for your feedback.I am new for java,so i could learn from your ideas and feedback. i need your guidance for further posts.
Was This Post Helpful? 0
  • +
  • -

#15 sayham  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 73
  • Joined: 21-March 13

Re: Reading given numbers in Array...

Posted 05 April 2013 - 11:37 AM

View Postashok.5656, on 01 April 2013 - 11:36 AM, said:

hi sayham,

try this,

 import java.util.*;
   public class ArrayOps
   {
       ArrayOps(){
           System.out.print("Enter Total numbers: ");
         Scanner in = new Scanner(System.in);
         int n=in.nextInt();
           System.out.println("Enter numbers:");
           
           ArrayList ar=new ArrayList();
           while(ar.size()<n){
               ar.add(in.nextInt());
           }
           int sum = 0;
            for (int i = 0; i < n; i++)
            {
            sum = sum + Integer.parseInt(String.valueOf(ar.get(i)));
            }
           System.out.println(sum);
      }
      public static void main(String[] args)
      {
         ArrayOps ob=new ArrayOps();
      }
   }




Thanks Ashok.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1