Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: ArrayOutOfBoundsException

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question ArrayOutOfBoundsException

    Hi all. I am very new to programing and don't understand what I am doing wrong. I believe it is because my var. "line" will always have a value, thus the while loop never terminates.

    How do I fix this so that the program will output the reg. numbers?



    ------------------------------------------------------------------------------------------------------------------------------
    import java.io.*;
    //import java.util.StringTokenizer;
    public class CarReg {
     
     
       public static void main (String []args) throws IOException {
     
          BufferedReader fr;
          fr = new BufferedReader (new FileReader("carreg.txt"));
          String line = fr.readLine();
          int count = 0;
     
          String carArr[];
          carArr = new String [50];
     
          while (line !=null) {
             carArr[count] = fr.readLine();
             count++;
          }
     
          for (int loop = 0; loop <=count ;loop++) {
             System.out.println ("Car Reg: " + carArr[loop]);
          }
       }
    }


    ------------------------------------------------------------------------------------------------------------------------------

    This is the information in the text file called carreg.txt:

    BMK123GP
    HJD678GP
    HAAI677NW
    HJK676GP
    JJK678NW
    GGH898GP
    HJK678GP
    GHJ878GP
    BMK123GP
    JKO677NW
    JAMBLIK6GP
    KDK872NW
    JFJ765GP
    HDF809NW
    DFG567GP
    BMK123GP
    JKO677NW
    HJD678GP
    JKO677NW
    HJK676GP
    DFG567GP
    JKJ343NW
    HJK678GP
    GHJ878GP
    YOYO55GP
    SDF789NW
    DFS678GP
    HJD678GP
    BMK123GP
    HDF809NW
    HJD678GP
    JKO677NW

    Some help would really be appreciated

    Thanks,
    bmaan.
    Last edited by jps; November 8th, 2012 at 06:42 AM. Reason: added code tags & formatting

  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: ArrayOutOfBoundsException

    don't understand what I am doing wrong. I believe it is because my var. "line" will always have a value, thus the while loop never terminates.
    That looks correct to me. Think about the condition that should make the loop stop. Change the condition of the while loop so that the loop stops when the condition meets the requirements.

    Please see the announcements page for the use of code tags and other forum tips.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: ArrayOutOfBoundsException

    Please debug the problem with a debugger --- as to check where the logic is breaking.
    Last edited by techiepro; November 8th, 2012 at 08:57 AM. Reason: spoonfeeding

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: ArrayOutOfBoundsException

    @techiepro please read the problem with spoonfeeding

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayOutOfBoundsException

    The arrayOutofBoundsException means that the capacity of array you wanna store the elements is not big enough. So it is out of bounds. Here is the figuration:
    1. you can change the capacity of your array, in this example can be seen as carArr = new String [number_bigger];
    2. or you can use the collections Java API provides, it frees you from caring how big you allocate

    and in your programming, the loop statement may be considered to use follows:
    String line = "";
    while ((line = fr.readLine()) != null) {
    // do storage here
    }