I am trying to write a Java program in which we would take input from the user until
the user enters -1. The input would be integers and has to be stored in a integer array.
I am a newbie in Java and i am trying to self study here. Please tell me what am i doing wrong. It would be of great help.
the user enters -1. The input would be integers and has to be stored in a integer array.
I am a newbie in Java and i am trying to self study here. Please tell me what am i doing wrong. It would be of great help.
Code:
class storearray{
public static void main(String args[]) throws IOException
{
BufferedReader dis= new BufferedReader(new InputStreamReader(System.in));
int arr[]= new int[10];
System.out.println("Enter elements of an array");
System.out.println("Enter -1 to quit");
int i=0;
String data;
System.out.flush();
for(i=0;arr[i]!= -1; i++){
data= dis.readLine();
try{
arr[i]= Integer.parseInt(data);
} catch(NumberFormatException e)
{
System.out.println("Invalid FOrmat");
//arr[i]=0;
}
System.out.println("The values of the array are :"+" "+arr[i]+" "+"i= "+i);
}
for(int x:arr)
System.out.println(x);
}
}
Comment