import java.util.Scanner; import java.io.*; public class ConfigProcessor { private String filename; public ConfigProcessor(String filename) { this.filename = filename; } public ConfigInfo processFile() throws InvalidGameException, InvalidNumDecksException, InvalidJokerSelectionException, InvalidWildCardException, MalformedConfigException { ConfigInfo ci = null; //open file Scanner s; try { s = new Scanner(new File(filename)); } catch(FileNotFoundException fnfe) { throw new MalformedConfigException(filename + " cannot be found. " + fnfe.getMessage()); } String line; //String type; String game; String tmpvalue; int numdecks; char usejokers; String[] wildcards; line = s.nextLine(); //System.out.println("LINE " + line); //verify that data type is game if(line.startsWith("game=")) { game = line.substring(5); //System.out.println("GAME " + game); //verify that game is a valid game if(!game.equals("blackjack") && !game.equals("poker")) { throw new InvalidGameException("You can't play game " + game + " here!"); } } else { throw new MalformedConfigException("Config file busted. Expected \"game=\""); } line = s.nextLine(); if(line.startsWith("num_decks=")) { tmpvalue = line.substring(10); try { numdecks = Integer.parseInt(tmpvalue); } catch(NumberFormatException nfe) { throw new InvalidNumDecksException(tmpvalue + " is a String, int expected."); } if(numdecks < 1 || numdecks > 6) { throw new InvalidNumDecksException("Number of decks must be 1-6. Found value " + numdecks); } } else { throw new MalformedConfigException("Config file busted. Expected \"num_decks=\""); } line = s.nextLine(); if(line.startsWith("use_jokers=")) { tmpvalue = line.substring(11); if(tmpvalue.length() > 1 || tmpvalue.length() == 0) { throw new InvalidJokerSelectionException("Expected y/n and found " + tmpvalue); } usejokers = tmpvalue.charAt(0); if(usejokers != 'y' && usejokers != 'n') { throw new InvalidJokerSelectionException("Expected y/n and found " + tmpvalue); } } else { throw new MalformedConfigException("Config file busted. Expected \"use_jokers=\""); } //ci = new ConfigInfo(game, numdecks, usejokers...); return ci; } }