# Thread: Need help with array

1. Member
Join Date
Jan 2011
Posts
7
Rep Power
0

## Need help with array

Hey!

Im having a problem with an assignment , i need to make an array that stores the users input (20 numbers) and then prints out the averege number, the smallest number, the largest number and then all the 20 numbers that the user put in but (5 numbers each line)

I dont know how to do that, my code only shows the 20 numbers. Can somebody please help me with this?

code:

Java Code:
```import java.util.*;
public class Laboration6 {

public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
int[] num = new int [21];
for (int i = 0; i < num.length; i++)
{
System.out.print("Enter Value " + (i+1) + ": ");
num[i] = scanner.nextInt();
}

System.out.print("You have entered: ");

for(int n: num)
{
System.out.print(n + " ");
}

}

}```
Last edited by Fubarable; 01-16-2011 at 03:59 PM. Reason: Moderator Edit: code tags added

2. You will need to add some new variables to your code, for instance, I'd create variables with names like largestNumber, smallestNumber, totalNumber. Then as you have the user create the array, in the for loop, use these variables to get what you want. Set the totalNumber, largestNumber and smallestNumber = to the first number entered (you'll need an if block for this). Then as new numbers are enterered add them to the totalNumber variable, and compare the new number to the current largestNumber and if bigger, make the largestNumber = the new number, and like-wise with the smallest number.

Luck!

3. Member
Join Date
Jan 2011
Posts
7
Rep Power
0
Thanks for the help! im still a bit confused because im new and i dont really get how things work, now i can get the smallest, largest and the average number but i dont know how to connect it with my first code, how can i show all the 20 numbers and 5 numbers each line?

Java Code:
```import java.util.*;
public class ScanArray {

public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
int[] num = new int [21];

Scanner keyboard = new Scanner(System.in);

System.out.println("Skriv in 20 tal och avsluta med ett negativt tal .");
//enter 20 numbers and end with a negative number
int count = 0, maxInteger = 0, minInteger = -1, sum = 0;

while(keyboard.hasNextInt())

{

int n = keyboard.nextInt();

if(n < 0) break;

if(minInteger == -1) minInteger = n;

sum += n; count++;

if(n > maxInteger) maxInteger = n;

if(n < minInteger) minInteger = n;

}

System.out.println("det största numret är: " + maxInteger);

System.out.println("Det minsta numret är: " + minInteger);

System.out.println("Medelvärdet är: " + (sum/count));
System.out.println("Du har skrivit in" + num.length);

for (int i = 0; i < num.length; i++)
{
System.out.print("Enter Value " + (i+1) + ": ");
num[i] = scanner.nextInt();
}

System.out.print("You have entered: ");

for(int n: num)
{
System.out.print(n + " ");
}

}

}```

4. Now is the time to expand your knowledge and learn how to use methods. You have several tasks (highest, lowest, average etc). Place each one of these tasks in a separate method and call them one at a time.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•