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.
These are the codes i have in my CODES.txt filepublic 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)); } }
WX6H0J2YB4PP7BFQM66YV7W3X
NNFH9P167MJ4HHLG5Y88CC1ZA
J22JYX45TGK77NND26CBB352P
G8QVBZWRF5HXS40WZN46TD4AR
PWW3SB2B2BA4EW8SFAL33MNAF
MFT8WW7N3IWF7BMM29PL73JN7
LK82N7FJA94LSUI7DFHN37LSM
N82LM47B2KSD9KA6D82B3LAJ2
MA7N2BN7FJ289NLLA7NFG2B3B
L0AP2ND7FNL278FN99LB2V3B4
QW81BBHF6GGT9D12BBKF7PAJF
any help would be greatly appreciated. thanks.