import java.util.*;
import java.util.Scanner;
public class InputconsoleArray2
{
public static void main(String args[]) throws java.io.IOException
{
int[] a=new int[100]; // create an array of type interger size
// Objects of type Scanner are useful for breaking down formatted input into tokens and
// translating individual tokens according to their data type
// By default, a scanner uses white space to separate tokens. (White space characters include blanks, tabs,
// and line terminators.
Scanner sc=new Scanner(System.in);
System.out.println("Please enter elements...");
int j=0;
//int next= sc.nextInt();
//System.out.println("Next = " + next);
while ( sc.hasNextInt())
{
a[j]=sc.nextInt();
System.out.println(" a[j] " + j +" " + a[j]);
j++;
}
sc.close();
int arrayCurrentSize = j--;
System.out.println("Array elements are : ");
for (int i=0; i< arrayCurrentSize; i++)
System.out.println(a[i]);
}
}
aomran
0
Newbie Poster
Recommended Answers
Jump to Postthe only way to kill it is to inter a character and not an integer. sc.hasNextInt() suppose to return a boolean true if the next token is an integer and false if anything else including eol.
Well apparently the last part is not correct. I just checked the API and …
Jump to PostWell apparently the last part is not correct. I just checked the API and it doesn't say such thing. It simply returns true or false if the next token is int BUT the the next token is not a new line. It is when you enter value. The new line, …
Jump to PostYou need a closing ). And why would you make a new Scanner that scans the string
[COLOR="Green"
anyway? That doesn't make any sense. Is that what you meant to put there?
All 10 Replies
aomran
0
Newbie Poster
Diomedes
0
Newbie Poster
aomran
0
Newbie Poster
javaAddict
900
Nearly a Senior Poster
Team Colleague
Featured Poster
BestJewSinceJC
700
Posting Maven
aomran
0
Newbie Poster
BestJewSinceJC
700
Posting Maven
Ezzaral
2,714
Posting Sage
Team Colleague
Featured Poster
aomran
0
Newbie Poster
javaAddict
900
Nearly a Senior Poster
Team Colleague
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.