Adding to arrays and printing arrays in Java
Posted
by
nfoggia
on Stack Overflow
See other posts from Stack Overflow
or by nfoggia
Published on 2013-10-22T03:19:22Z
Indexed on
2013/10/22
3:54 UTC
Read the original article
Hit count: 227
I need help figuring out how to get the user to input a number of integers no more than 10, and then add them to an array and print them out from the array. The code I have below, when run, asks the user for the integers and then runs forever and doesn't work. What am I doing wrong?
public static void main(String[] args) {
Scanner input = new Scanner(System.in); // create a new scanner
System.out.print("Enter integers between 1 and 100\n ");
int[] nextNumber = new int[10];
int i = 0;
int number = input.nextInt();
while (i < nextNumber.length){
i++;
nextNumber[i] = number;
number = input.nextInt();
}
int a = 0;
while (a < nextNumber.length){
a++;
System.out.println(nextNumber[a]);
}
© Stack Overflow or respective owner