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.
Code :
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.
Code :
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.
Re: Please help, outofbounds error
Quote:
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.
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
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.
Code :
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
}
}
Re: Please help, outofbounds error
Quote:
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.