I'm very new to this and I was stuck on solving a problem that has to do with arrays and I need help. So the program Im supposed to write is that first the user inputs a number (int). Then the user can input any number (int) and each time the user enters a number, it prints out an array of those numbers in a sorted order. When the user inputs "end" in string, then the program ends and shows the whatever the array of sorted numbers it was. The thing is I don't know how the user can input a string when I declared that the scanner would take int. When I put in a string, it shows an error. I am a beginner so please don't use anything complicated like "something Exception" and things like that. I've only learned loops and just got in to learning Arrays. Thank you.
import java.util.*;
import java.util.Scanner;
public class Homework3 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Type :");
int numbers = scan.nextInt();
int [] a = {numbers};
System.out.println(Arrays.toString(a));
while ( a.length < 5 ) {
System.out.print("Type: ");
int number = scan.nextInt();
int[] b = Arrays.copyOf(a, a.length + 1);
b[b.length-1] = number;
a = b;
System.out.println(Arrays.toString(a));
}
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}
}