Results 1 to 4 of 4
  1. #1
    searcher is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default array index out of bounds

    Hi ppl heres my code and i keep getting array index out of bounds

    private static int breakFast , nightSingle , nightPPS , nightSingleBreakFast , nightPPSBreakFast , discountRate , discountInterval ,
    dinner;
    private static File dir = new File("C:\\Mariette'sB&B");
    private static File e = new File(dir , "prices.file");
    private static BufferedReader in;
    private static BufferedWriter out;
    private static String [] prices = new String [7];


    public static void readFile()throws FileNotFoundException , IOException{// readFile and add to array
    if(e.exists()){
    in = new BufferedReader(new FileReader(e));
    String read = null;
    int count = 0;
    do{
    read = in.readLine();
    prices [count] = read;
    count++;
    }while(read != null);
    in.close();
    updatePrices();
    }
    else{
    dir.mkdir();
    e.createNewFile();
    }
    }

    public static void writeFile()throws IOException{// writeFile
    out = new BufferedWriter(new FileWriter(e));
    for(int i = 0; i < prices.length ; i++){
    out.write(prices[i]);
    out.newLine();
    }
    out.flush();
    out.close();
    }

    public static void updatePrices(){
    breakFast = Integer.parseInt(prices[0]);
    nightSingle = Integer.parseInt(prices[1]);
    nightPPS = Integer.parseInt(prices[2]);
    nightSingleBreakFast = Integer.parseInt(prices[3]);
    nightPPSBreakFast = Integer.parseInt(prices[4]);
    discountRate = Integer.parseInt(prices[5]);
    discountInterval = Integer.parseInt(prices[6]);
    dinner = Integer.parseInt(prices[7]);
    }

    and heres my file's content
    36
    272
    195
    300
    230
    15
    5
    80

    i just cant get the error to go away ive tried resizing the array and then i get then i get numberformat exception but my array shud only contain the 8 elements in the file so i dont see why i need to make it bigger

  2. #2
    searcher is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: array index out of bounds

    Ahhh i figured it out gosh im silly the file containing my information had a space behind a number lol human error i figured it out using a try statement and printing out my error

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: array index out of bounds

    Good for you, and thanks for posting your solution -- very thoughtful!

    Please consider using [code] [/code] tags when next posting code here as it will make your posts easier to read. Best of luck!

  4. #4
    searcher is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

Similar Threads

  1. array index out of bounds exception, AGAIN!
    By samanyu in forum New To Java
    Replies: 8
    Last Post: 06-08-2011, 12:27 PM
  2. having an array index out of bounds problem
    By samanyu in forum New To Java
    Replies: 18
    Last Post: 06-03-2011, 09:12 AM
  3. Array Index Out Of Bounds Exception
    By manowar689 in forum New To Java
    Replies: 3
    Last Post: 06-19-2010, 12:25 AM
  4. array Index out of Bounds exception== 0
    By Allgorythm in forum New To Java
    Replies: 6
    Last Post: 02-11-2010, 05:02 PM
  5. Array Index out of bounds
    By leapinlizard in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 06:11 AM

Posting Permissions

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