# Reading given numbers in Array...

Page 1 of 1

## 14 Replies - 836 Views - Last Post: 05 April 2013 - 11:37 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=317291&amp;s=5d3202604be7ae8c01a7e60b75f189d2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sayham

Reputation: 3
• 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

• D.I.C Lover

Reputation: 2985
• Posts: 12,769
• 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

### #3 sayham

Reputation: 3
• 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.

### #4 pbl

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

Reputation: 8362
• Posts: 31,955
• 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};

```

```int[] values = new int[20];

```

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

### #5 g00se

• D.I.C Lover

Reputation: 2985
• Posts: 12,769
• 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?

### #6 sayham

Reputation: 3
• 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.

### #7 sayham

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

## Re: Reading given numbers in Array...

Posted 31 March 2013 - 01:45 PM

pbl, 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};

```

```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.

### #8 pbl

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

Reputation: 8362
• Posts: 31,955
• 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

```

### #9 g00se

• D.I.C Lover

Reputation: 2985
• Posts: 12,769
• 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)

### #10 pbl

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

Reputation: 8362
• Posts: 31,955
• 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);

sayham, 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.

### #11 sayham

Reputation: 3
• 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.

### #12 ashok.5656

Reputation: 0
• 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){
}
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();
}
}

```

### #13 pbl

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

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Reading given numbers in Array...

Posted 01 April 2013 - 11:46 AM

ashok.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){
}
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();
}
}

```

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

### #14 ashok.5656

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

## Re: Reading given numbers in Array...

Posted 01 April 2013 - 09:14 PM

pbl, on 01 April 2013 - 11:46 AM, said:

ashok.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){
}
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();
}
}

```

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.

### #15 sayham

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

## Re: Reading given numbers in Array...

Posted 05 April 2013 - 11:37 AM

ashok.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){
}
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.