Modify the program below:
public class LenghtArray {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] ages = {16, 19, 30, 60, 21, 25, 2, 13};
//print all elements
for (int count = 0; count < ages.length; count ) {
System.out.print(ages[count] " ");
}
//sum of all ages
System.out.println(" ");
int total = 0;
for (int count = 0; count < ages.length; count ) {
total = total ages[count];
}
System.out.print("Total :" total " ");
}
}
Output must be:
Input length of an array: 4
age[0]: 65
age[1]: 10
age[2]: 60
age[3]: 18
Display all elements in an array: 65, 10, 60, 18
Total: 153
so far here's what I have, I don't know what's wrong with it. My professor said you just need to add 2 string of lines. I keep on adding more
import java.util.Scanner;
public class array2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner inp = new Scanner(System.in);
int[] ages = {0};
System.out.println("Input length of an array:");
int number = inp.nextInt();
for (int count = 0; count < number; count ) {
System.out.println("age[" count "]: ");
ages[count] = inp.nextInt();
}
//print all elements
for (int count = 0; count < ages.length; count ) {
System.out.print(ages[count] " ");
}
//sum of all ages
System.out.println(" ");
int total = 0;
for (int count = 0; count < ages.length; count ) {
total = total ages[count];
}
System.out.print("Total :" total " ");
}
}
CodePudding user response:
You need to change place and style of the declaration and creation of the age
array:
- declare it after you know it's desired length
- create it with the desired length:
int number = inp.nextInt();
int[] ages = new int[number];
Rest of your code looks good. You might want to improve it a bit by printing the texts before the inputs (like "Input length of an array:") only with System.out.print
instead of System.out.println
- this will make the input on the same row as the text before.
In contrary you should use the println
for the total output.
CodePudding user response:
In your Case int ages = {0}
means your size of array is one and you also declare 0
at first position so, after inserted first value in array compiler produce array ArrayIndexOutOfBoundException
. You have to declare array after inputed number
because without inputed number how can you define what is the length of array. Here down is solution of problem.
System.out.println("Input length of an array:");
// First define size of array
int number = inp.nextInt(); // Suppose number = 4
int[] ages = new int[number]; // Declare array with size 4
Why you make lengthy your code, use Java Stream for shorten your code. See Here down.
import java.util.*;
import java.util.stream.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Input length of an array:");
int length = sc.nextInt();
int[] ages = new int[length]; // Initialize array
for (int i = 0; i < length ; i ) {
System.out.print("age[" i "]: ");
ages[i] = sc.nextInt();
}
// Print the array
System.out.println("Display all elements in an array: " Arrays.toString(ages));
// Total of array
int total = Arrays.stream(ages).sum();
System.out.print("Total: " total);
}
}
CodePudding user response:
I would recommend first of all making decomposition of the task into smaller element steps. Then implement each step and check the result. In this case, you can easier find a bug in the program.
P.S. Your teacher could give a
sign seeing this
public class LengthArray {
public static void main(String... args) {
int[] ages = createArray();
printArray(ages);
printTotal(ages);
}
private static int[] createArray() {
Scanner scan = new Scanner(System.in);
System.out.print("Input length of an array: ");
int[] ages = new int[scan.nextInt()];
for (int i = 0; i < ages.length; i ) {
System.out.format("age[%d]: ", i);
ages[i] = scan.nextInt();
}
return ages;
}
private static void printArray(int... ages) {
System.out.println("Display all elements in an array: " Arrays.toString(ages));
}
private static void printTotal(int... ages) {
System.out.println("Total: " calcTotal(ages));
}
private static int calcTotal(int... ages) {
int total = 0;
for (int age : ages)
total = age;
return total;
}
}
Output:
Input length of an array: 4
age[0]: 65
age[1]: 10
age[2]: 60
age[3]: 18
Display all elements in an array: [65, 10, 60, 18]
Total: 153