• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
}
}
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;*********************************************
}
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic