I wrote this program in Java : Write a program that reads a list of integers and outputs those integers in reverse. The input begins with an integer indicating the number of integers that follow. For coding simplicity, follow each output integer by a comma, including the last one. Assume that the list will always contain fewer than 20 integers.
Ex: If the input is:
5 2 4 6 8 10
the output is:
10,8,6,4,2
As you can see, this is my expected out^ However, I got 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 8 6 4 2
as my output. What is wrong?
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int[] userList = new int[20]; // List of numElement integers specified by the user
int numElements; // Number of integers in user's list
int i;
numElements = scnr.nextInt(); // Input begins with number of integers that follow
for (i = 0; i < numElements; i) {
userList[i] = scnr.nextInt();
}
for (i = userList.length - 1; i >= 0; --i) {
System.out.print(userList[i] " ");
}
System.out.println();
}
}
I tried to ask a good question and I expect an answer to my question.
CodePudding user response:
you have to create a list of size numElements,
int[] userList = new int[numElements];
for(i = 0;i < numElements; i) {
userList[i] = scnr.nextInt();
}
for (i = userList.length-1; i >= 0; --i) {
System.out.print(userList[i] " ");
}
System.out.println();
CodePudding user response:
Your code is fine but you just need to read the number of element before creating your userList
:
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int numElements; // Number of integers in user's list
numElements = scnr.nextInt(); // Input begins with number of integers that follow
int[] userList = new int[numElements]; // List of numElement integers specified by the user
int i;
for (i = 0; i < numElements; i) {
userList[i] = scnr.nextInt();
}
for (i = userList.length - 1; i >= 0; --i) {
System.out.print(userList[i] " ");
}
System.out.println();
}
}
CodePudding user response:
Your array size is 20 and your are trying to print whole array in reverse order which includes unfilled indexes also. so try to make array size as numElements and print it will work
CodePudding user response:
Explanation
You are getting zero maybe due to the array size declaration. You created an array with 20 as the size. But assuming you set the numElements
to 10, that means that the input iteration will only loop 10 times thus only loading 10 positions in your array.
As you had defined an array with 20 indexes, the rest are initiated with a zero during array declaration. So assuming the iterations goes 10 times that means only 10 indexes will be updated and the other 10/20 left with their initial zeros.
You have to re-declare your array after getting a value for the numElements
and set the numElements
as the new array size. The code would look similar to below
Scanner scanner = new Scanner(System.in); // Create Scanner object
int[] userList = new int[0]; // Create int array
int numElements = scanner.nextInt(); // Get number of elements
userList = new int[numElements]; // Re-declare array with new size
Solution
The full code in your approach is as below:
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int numElements; // Number of integers in user's list int i;
System.out.println("Input length of the array");
numElements = scnr.nextInt(); // Input begins with number of integers that follow
int[] userList = new int[0];
// Check size value (number of elements)
if (numElements > 0) {
userList = new int[numElements]; // Re-Declare array giving it a new size
} else {
System.out.println("This array size cannot be less than 1");
}
System.out.println();
for(int i = 0; i < numElements; i) {
System.out.println("Input number at position " i);
userList[i] = scnr.nextInt();
}
for (int i = userList.length -1; i >= 0; i--) {
//for (int i = userList.length-1; i >= 0; --i) {
System.out.print(userList[i] " ");
}
System.out.println();
}