Store the numbers in a array in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shashankraj1231
    New Member
    • May 2010
    • 13

    Store the numbers in a array in Java

    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.

    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);
    		}
    		
    	}
    Last edited by Niheel; May 25 '10, 08:57 AM. Reason: Don't need the hellos and thank yous. :)
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Your for loop need some changes
    Code:
    for(i=0;arr[i]!= -1; i++)
    Your code runs till arr[i] != -1
    Your program will throw array index out of bound exception. Since you defined array of size 10 and your for loop do not terminate. User is able to add 11th elements.

    Your for loop should look like
    Code:
    for(int iCount=0;iCount<10;iCount++)
    {
            //Logic here
    }

    Please catch a generic exception and see what exception it will throw.

    Regards
    Dheeraj Joshi
    Last edited by Dheeraj Joshi; May 25 '10, 09:03 AM. Reason: Spell correction

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      for(i=0;arr[i]!= -1; i++)
      The problem is that in a for loop, the increment runs before the conditional check.
      So let's say the first number is -1. You put -1 in arr[0].
      Then i++ -> i = 1
      The for loop checks, is arr[1] != -1? No.

      But you meant to check arr[0], not arr[1].

      You might want to have a different check INSIDE the for loop, eg:
      if number entered was -1, break out of the for loop.

      Comment

      • shashankraj1231
        New Member
        • May 2010
        • 13

        #4
        Originally posted by jkmyoung
        for(i=0;arr[i]!= -1; i++)
        The problem is that in a for loop, the increment runs before the conditional check.
        So let's say the first number is -1. You put -1 in arr[0].
        Then i++ -> i = 1
        The for loop checks, is arr[1] != -1? No.

        But you meant to check arr[0], not arr[1].

        You might want to have a different check INSIDE the for loop, eg:
        if number entered was -1, break out of the for loop.
        Thank you very much for that explanation, I understand the For-Loop much better now. I was not aware that the the increment runs before the conditional check.
        I corrected the program by adding
        if (arr[i]== -1)
        break;

        and it works like magic now.

        Regards,
        Shashank.

        Comment

        • shashankraj1231
          New Member
          • May 2010
          • 13

          #5
          Originally posted by dheerajjoshim
          Your for loop need some changes
          Code:
          for(i=0;arr[i]!= -1; i++)
          Your code runs till arr[i] != -1
          Your program will throw array index out of bound exception. Since you defined array of size 10 and your for loop do not terminate. User is able to add 11th elements.

          Your for loop should look like
          Code:
          for(int iCount=0;iCount<10;iCount++)
          {
                  //Logic here
          }

          Please catch a generic exception and see what exception it will throw.

          Regards
          Dheeraj Joshi
          Thank you for the explanation of the exception. That gave me a clear understanding of how it works.

          Regards,
          Shashank.

          Comment

          Working...