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: Please help, outofbounds error

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help, outofbounds error

    I am making a program that has a user input a code(in this case xBoxLive Gold membership code, 25 characters) and goes to another class which has a file of codes and compares them characters to see if they match up. I am not getting any errors but it gives me the response that its not equal every time, regardless if the code i typed in matched.

    public class XboxLive
    {
        Scanner keyboard = new Scanner(System.in);
        String result;
        private Scanner inputFile;                                          //Create a scanner object
        public ArrayList<String> codeArray = new ArrayList<String>();       //Create array to input lines from file into
     
        public XboxLive(String userCode)
        {
            String code = userCode;                                                //Constructor
        }
     
        public void open() throws IOException
        {
            File file = new File("CODES.txt");            //Open the file to read the authentic codes
            inputFile = new Scanner(file);                //to compare to user entered code.
        }
     
        public void codesToArray() throws IOException
        {
            boolean lineRead;   //Flag Variable
     
            lineRead = inputFile.hasNext();
     
            if (lineRead)
            {
                String line = inputFile.nextLine();
                codeArray.add(line);                        //line is then transferred to an element of the array
            }
        }
     
        public boolean testCode(String user)
        {
            boolean allGood = false;
            int index = 0;
            if(user.length() == 25)
                allGood = true;                            //if over 25 characters, doesn't fit the format
     
            while(allGood && index < 25)
            {
                if(Character.isLetterOrDigit(user.charAt(index)));       //Checks to see if any character is not a letter or number
                        allGood = true;
                index++;
            }
     
            if(allGood == false)                              //Allows user to re-enter code if he has commited an error in entry before.
            {
                System.out.println("Im sorry, the code you've enter is not in the correct form, please try again.");
                String input = keyboard.nextLine();
                System.out.println();
                testCode(input);
            }
     
            return allGood;
        }
     
        public String validateCode(String user)
        {
            boolean res = false;                                 //boolean value to hold if codes match
            int i = 0;
            while(i<codeArray.size() && !res)
            {
                String str = codeArray.get(i);
                if(str.equalsIgnoreCase(user))    //This part of the code will compare the user entered code
                {                                                   //with the codes that are in the file
                    res = true;
                    break;
                }
                else
                {
                    i++;
                    res = false;                                    //If a character is NOT a match, continue to next code and boolean set to false
                }
            }
            if(res == true)
                result = "Congrats! You are now an Xbox Live Gold Member! Enjoy gaming with your friends!";
            else
                result = "Im sorry, the code you have entered is not a match. Please recheck your card and try again.";
            return result;
        }
     
        public void close() throws IOException
        {
            inputFile.close();                              //Method to close the file
        }
    }

    This is the demo program i use to call it.

    public class XboxLiveDemo
    {
        public static void main(String[] args) throws IOException
        {
            Scanner keyboard = new Scanner(System.in);
     
            System.out.println("Please enter the code off the back of your Xbox Live Gold Member Card.");
            String input = keyboard.nextLine();
            System.out.println();
     
            XboxLive match = new XboxLive(input);
     
            if(match.testCode(input))
               System.out.println(match.validateCode(input));
     
        }
    }
    These are the codes i have in my CODES.txt file

    WX6H0J2YB4PP7BFQM66YV7W3X
    NNFH9P167MJ4HHLG5Y88CC1ZA
    J22JYX45TGK77NND26CBB352P
    G8QVBZWRF5HXS40WZN46TD4AR
    PWW3SB2B2BA4EW8SFAL33MNAF
    MFT8WW7N3IWF7BMM29PL73JN7
    LK82N7FJA94LSUI7DFHN37LSM
    N82LM47B2KSD9KA6D82B3LAJ2
    MA7N2BN7FJ289NLLA7NFG2B3B
    L0AP2ND7FNL278FN99LB2V3B4
    QW81BBHF6GGT9D12BBKF7PAJF



    any help would be greatly appreciated. thanks.


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

    Default Re: Please help, outofbounds error

    it gives me the response that its not equal every time, regardless if the code i typed in matched.
    Can you post the output from your program that shows the problem? Add comments to the output that shows what the problem is and also show what the output should be.

    Add some printlns to the program to show what variables values are and how the execution flow goes.

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

    Default Re: Please help, outofbounds error

    What compiler are you using? I get this warning when I compile your code:
    warning: [empty] empty statement after if

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help, outofbounds error

    im using textpad, and here is the output

    Please enter the code off the back of your Xbox Live Gold Member Card.
    l0ap2nd7fnl278fn99lb2v3b4

    Exception in thread "main" java.lang.NullPointerException
    at XboxLive.codesToArray(XboxLive.java:35)
    at XboxLiveDemo.main(XboxLiveDemo.java:20)
    Press any key to continue . . .


    Also i changed the code a little here it is.
    import java.util.*;
    import java.io.*;
    /*
        This program demonstrates the neccesary methods required to check for equal codes.
        If they enter a correct code, their account will be upgraded to a Gold Membership
    */
     
     
    public class XboxLive
    {
        Scanner keyboard = new Scanner(System.in);
        String result;
        private Scanner inputFile;                                          //Create a scanner object
        public ArrayList<String> codeArray = new ArrayList<String>();       //Create array to input lines from file into
     
        public XboxLive(String userCode)
        {
            String code = userCode;                                                //Constructor
        }
     
        public void open() throws IOException
        {
            File file = new File("CODES.txt");            //Open the file to read the authentic codes
            inputFile = new Scanner(file);                //to compare to user entered code.
        }
     
        public void codesToArray() throws IOException
        {
            boolean lineRead;   //Flag Variable
     
            lineRead = inputFile.hasNext();
     
            if (lineRead)
            {
                String line = inputFile.nextLine();
                codeArray.add(line);                        //line is then transferred to an element of the array
            }
        }
     
        public boolean testCode(String user)
        {
            boolean allGood = false;
            int index = 0;
            if(user.length() == 25)
                allGood = true;                            //if over 25 characters, doesn't fit the format
     
            while(allGood && index < 25)
            {
                if(Character.isLetterOrDigit(user.charAt(index)));       //Checks to see if any character is not a letter or number
                        allGood = true;
                index++;
            }
     
            if(allGood == false)                              //Allows user to re-enter code if he has commited an error in entry before.
            {
                System.out.println("Im sorry, the code you've enter is not in the correct form, please try again.");
                String input = keyboard.nextLine();
                System.out.println();
                testCode(input);
            }
     
            return allGood;
        }
     
        public String validateCode(String user)
        {
            boolean res = false;                                 //boolean value to hold if codes match
            int i = 0;
            while(i<codeArray.size() && !res)
            {
                System.out.println("value of res = " +res);
                String str = codeArray.get(i);
                if(str.equalsIgnoreCase(user))    //This part of the code will compare the user entered code
                {                                                   //with the codes that are in the file
                    res = true;
                    break;
                }
                else
                {
                    i++;
                    res = false;                                    //If a character is NOT a match, continue to next code and boolean set to false
                }
            }
            if(res == true)
                result = "Congrats! You are now an Xbox Live Gold Member! Enjoy gaming with your friends!";
            else
                result = "Im sorry, the code you have entered is not a match. Please recheck your card and try again.";
            return result;
        }
     
        public void close() throws IOException
        {
            inputFile.close();                              //Method to close the file
        }
    }

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

    Default Re: Please help, outofbounds error

    java.lang.NullPointerException
    at XboxLive.codesToArray(XboxLive.java:35)
    Look at line 35 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 35 and print out the values of all the varibles on that line.

Similar Threads

  1. Looping / OutOfBounds code problem
    By thisbeme in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 3rd, 2011, 07:10 AM
  2. ArrayList to String/outOfBounds
    By Scotty in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 28th, 2011, 04:41 PM
  3. Outofbounds error
    By tendulkarfan4life in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 14th, 2011, 11:20 AM

Tags for this Thread