• Post Reply Bookmark Topic Watch Topic
  • New Topic

scanner nextInt method returnig array type?How array[i] holds all elements ?  RSS feed

 
srinivas sy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one explain ..how nextInt() returns array type here..because it returns int type..how it works here.


Scanner input = new Scanner(System.in);
int array[] = new int[10];

System.out.println("Enter the numbers now.");

for (int i = 0 ; i < array.length; i++ ) {
if (input.nextInt() == 999){
break;
} else {
array[i] = input.nextInt();//////////***********returning int type
}
}
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
array[i] = 4; will work here.

.nextInt() returns and int, not array of int.

Hope you got it.
 
srinivas sy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
will it store only one element? or 10 elements(size) will it store?if so how does it work?
Here, array[i] having multiple values i guess..


Scanner input = new Scanner(System.in);
int array[] = new int[10];

System.out.println("Enter the numbers now.");

for (int i = 0 ; i < array.length; i++ ) {
int next = input.nextInt();
if (next == 999)
break;
array[i] = next;*********************************************
}
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
srinivas sy wrote:will it store only one element? or 10 elements(size) will it store? . . .
If you mean nextInt, that doesn't store anything. If you use nextInt to fill an array in a loop, it still returns one int at a time. [You doubtless know a Java® method returns one thing and always one thing, unless it is marked void.]
If you fill a ten‑element array using nextInt, then nextInt runs ten times.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!