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 3 of 3

Thread: File don't work

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy File don't work

    I have problem making my file work. Wrong message in console is "Index out of bounds" or "Index 1 out of bounds for length 1". How do I solve it?
     
     
    /**
     * Class for reading capitals from file and creating Capital objects from them
     */
    class CapitalReader {
     
        /**
         * Returns random Capital object from list of capitals
         * @return random Capital
         */
        public static Capital getRandomCapital() {
     
            ArrayList<Capital> capitals = getCapitalsFromFile();
            Random randomGenerator = new Random();
            int randomIndex = randomGenerator.nextInt(capitals.size());
            return capitals.get(randomIndex);
        }
     
        /**
         * Read file with capitals and create a list containing all Capital objects
         * @return list of capital objects
         */
        private static ArrayList<Capital> getCapitalsFromFile() {
     
            String txtFile = "words.txt";
            BufferedReader br = null;
            String line = "";
            int COUNTRY_COL = 0;
            int CAPITAL_COL = 1;
            ArrayList<Capital> capitals = new ArrayList<Capital>();
     
            try {
     
                br = new BufferedReader(new FileReader(txtFile));
     
                while ((line = br.readLine()) != null) {
                    String[] capital = " \\|".split(line);
                    capitals.add(new Capital(capital[CAPITAL_COL] , capital[COUNTRY_COL]));
                }
     
            } catch (FileNotFoundException e) {
                System.out.println("File not found CapitalReader");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return capitals;
        }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: File don't work

    Index 1 out of bounds for length 1
    The only valid index for an array of length 1 is 0. The max index for an array is the array's length - 1.
    An array requires 2 elements for the index of 1 to be valid.

    Check that the array has at least two elements in it before using an index of 1.

    See what is in the capital array by printing its contents using the Arrays toString method:
        System.out.println("capital= "+ java.util.Arrays.toString(capital));
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2021
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: File don't work

    Quote Originally Posted by Norm View Post
    The only valid index for an array of length 1 is 0. The max index for an array is the array's length - 1.
    An array requires 2 elements for the index of 1 to be valid.

    Check that the array has at least two elements in it before using an index of 1.

    See what is in the capital array by printing its contents using the Arrays toString method:
        System.out.println("capital= "+ java.util.Arrays.toString(capital));
    Thank you it did work, also my split argument was wrong so now everything is working good

Similar Threads

  1. Can't work out how to read object from .txt file
    By goformickey in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 25th, 2012, 01:27 AM
  2. Trouble getting zip file code to work??
    By monoposto in forum Object Oriented Programming
    Replies: 0
    Last Post: July 16th, 2012, 07:26 PM
  3. Jar file does not work with database (Hibernate)
    By shomid in forum JDBC & Databases
    Replies: 2
    Last Post: December 27th, 2011, 11:03 AM
  4. How to make Eclipse work with txt file?
    By Prostak in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 4th, 2011, 05:43 PM
  5. File IO Compiles, but wont work?
    By StarKannon in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: February 2nd, 2011, 09:05 AM