I'm quite new to java programming so excuse the basic misunderstandings and interpretations of the fundamentals. This program is supposed to ask the user for the size of an array, then ask the user for input n amount of times and then prints the array back to the user. So far, the first 2 parts work. The program asks for the size of the array and then prints "Enter element n: " based on how many times the user specified in part 1. However, i can't seem to figure out how to print back the string input back out (In my first for loop) the second for loop i tried does not work and just ends the program straight after the first for loop finishes executing. If anyone could help me it would be much appreciated and contribute to my learning of the basics of java. Cheers.
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter array size: ");
int arraySize = scanner.nextInt();
int[] array = new int[arraySize];
for (int i = 0; i<array.length; i++){
System.out.print("Enter element " + (i + 1) + ": ");
String element = scanner.next();
}
System.out.print(array[0]);
for (int i = 1; i < array.length; i++){
System.out.print(array[i]);
}
System.out.println("}");
}
You're not storing any input value into the array.
replace this line:
String element = scanner.next();
with the following one:
array[i] = scanner.nextInt();