Results 1 to 7 of 7
  1. #1
    farahm is offline Member
    Join Date
    Nov 2010
    Location
    Beirut, Lebanon
    Posts
    36
    Rep Power
    0

    Default array out of bound exception

    so the program i wrote is supposed to read from a file a set of numbers and see if in every line there is a number who is a double of the other and give out how many numbers are doubles of each other in the output. by the way it should stop if the line contains only (-1) and every line ends with a zero which is not considered part of the list.
    my sample file contains the following numbers:
    1 4 3 2 9 7 18 22 0
    2 4 8 10 0
    7 5 11 13 1 3 0
    -1
    Last edited by farahm; 12-19-2010 at 10:17 AM.

  2. #2
    DerekRaimann is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    Please repost the code using code formatting. What you have to do is type a square left bracket, the word code, and a square right bracket. Put this code before the first line of the Java code you are trying to display in your message. Then after the last line of your Java code, type a square left bracket, the slash character that should be on the same key as the question mark, the word code, and a square right bracket. Sorry I had to list everything like that but if I typed it out it wouldn't appear in the message.

    Once your code looks nice it is quicker for us to look it over and test it out ourselves and give you an answer. Thanks,

    -Derek Raimann

  3. #3
    farahm is offline Member
    Join Date
    Nov 2010
    Location
    Beirut, Lebanon
    Posts
    36
    Rep Power
    0

    Default

    thanks for the advice. its much better to read this way.
    your help concerning my problem would be appreciated.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default

    Sorry I had to list everything like that but if I typed it out it wouldn't appear in the message.
    [code]CODE[/code] will display as
    Java Code:
    CODE
    Quote this to see how it's done ;)

    db

  5. #5
    DerekRaimann is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    I figured out the array out of bounds problem. What is happening is the first line of data is read, the int[] a variable is created with 9 slots, and the loop from lines 31 to 35 iterates the i variable up to the number 9. Then in line 37 you attempt to create an integer called temp and give it the value from the array called "a" from slot 9 (that is what "i" is equal to), which is actually the tenth slot, because array indexing starts at 0. Line 37 should be changed from:

    int temp = a[i];

    to

    int temp = a[i-1];

    However, all that line will do is take the last value of the line you have read, which is 0, and place it in the variable temp. I believe you need to look at the code again and re-evaluate your looping process to check for the double values and everything, because it a little more complex than the code is attempting at the moment. There should probably be a loop that keeps changing the temp value to go through each value in the "a array" and test it against other values in that array to see if it is double. At the current time, you are testing the value 0 for double values every time the test runs. It will test you that there is one result per line because 0 = 2*0 (temp == 2*a[j]). If you have any questions on what I just wrote please let me know, I hope I cleared up the issue.

    -Derek Raimann

  6. #6
    farahm is offline Member
    Join Date
    Nov 2010
    Location
    Beirut, Lebanon
    Posts
    36
    Rep Power
    0

    Default

    yes thank you and i even needed to adjust [i-1] in the break
    however i cant figure out how to fix the logic i tried entering a loop that goes up to the count but that doesnt seem to work.
    any suggestions concerning the problem would be appreciated

  7. #7
    DerekRaimann is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    I have gone over the code and revised it to do what you are attempting to do, here goes:

    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class trial
    {
        public static void main (String args[]) throws FileNotFoundException
        {
            Scanner input = new Scanner(new File(args[0]));
            
    		mainLoop:
    			while (input.hasNextLine())
    			{
    				String str = input.nextLine();
    				
    				if (str.equals("-1"))
    				{
    					break;    
    				}
    				
    				Scanner string = new Scanner(str);
    				int count =0;
    				while (string.hasNext())
    				{
    					count++;
    					string.next();
    				}
    				
    				int [] a = new int [count];
    				Scanner S2 = new Scanner (str);
    				int i =0;
    				
    				while (S2.hasNextInt())
    				{
    					a[i] = S2.nextInt();
    					i++;
    				}
    				
    				int temp = 0;
    				int numcount = 0;    
    				
    				for(int tempIndex = 0; tempIndex<(count-1); tempIndex++) {
    					temp = a[tempIndex];
    					for(int j =0; j<(count-1) ; j++)
    					{
    						if (temp == (2*a[j]))
    						{
    							numcount++;
    							System.out.println(temp + " is a double value of " + (a[j]));
    							System.out.println(numcount);
    						}
    					}
    				}
    				
    				// the prior "break" statement used in this loop in the old code only broke out
    				// of this for loop. Using a label will bring execution back to the initial while
    				// loop upon finding a zero, check to make sure the input has a next line,
    				// and continue. I used the continue statement instead of break because break was 
    				// ending the execution of the program, but continue would continue execution at the
    				// label "mainLoop". This program can actually do without this loop, but I kept it
    				// included because I wasn't completely sure of your intention. If you comment it
    				// out the program will still run as expected.
    				
    				for(int k =0; k<(count-1) ; k++)
    				{
    					if(a[i-1] == 0)
    					{
    						continue mainLoop;
    					}
    				}
    			}
        }
    }
    If you look at the code where the temp variable is created, you will notice that I have created a second loop, using a variable called tempIndex as the iterator. The temp variable acquires each value of the "a" array, one at a time, and then tests that value against each variable in the "a" array in a for loop using the "j" variable as its iterator. This allows the program to test every variable in a single line individually against every number in the same line. I added an output statement so that you can see which numbers are doubles of other numbers and it will display on the screen. You can comment out or delete this output statement if you wish, I put it there so you could see the program at work.

    Let me know if this solves the problem for you or if you have any further questions regarding this program. I had fun working on it and hope that I was able to bring you the solution you were looking for.

    -Derek Raimann

Similar Threads

  1. Array out of bound- Recursive Method
    By hpayandah in forum New To Java
    Replies: 2
    Last Post: 11-12-2010, 08:02 PM
  2. Array index out of bound exception error
    By rahulkrishnanr in forum Threads and Synchronization
    Replies: 7
    Last Post: 10-12-2010, 05:57 PM
  3. Array index Out of bound Exception
    By nitin_daviet88 in forum New To Java
    Replies: 9
    Last Post: 07-28-2010, 05:32 AM
  4. Array Index Out of bound exception
    By abhijit in forum Networking
    Replies: 7
    Last Post: 09-25-2009, 07:25 PM
  5. ArrayIndexout of Bound exception
    By Preethi in forum New To Java
    Replies: 2
    Last Post: 02-14-2008, 09:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •