Advertisement
Guest User

InputHandler.java

a guest
Jan 16th, 2014
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.io.LineNumberReader;
  6. import java.util.ArrayList;
  7.  
  8.  
  9. public class InputHandler
  10. {
  11. public String object;
  12. static File in;
  13.  
  14. //create streams
  15. public InputHandler(){
  16. InputHandler.in = new File("input.txt");
  17. }
  18.  
  19. private static int lineCounter() throws IOException{
  20. LineNumberReader lnr = new LineNumberReader(new FileReader(in));
  21. lnr.skip(Long.MAX_VALUE);
  22. int lineCount = lnr.getLineNumber();
  23. lnr.close();
  24. return lineCount+1;
  25. }
  26.  
  27. //grabs object and query for program run loads into arrays for each and checks to ensure that there are an equal number
  28. public ArrayList<String>[] getObjectsandQueries() throws IOException{
  29. int lineCount = lineCounter();
  30. System.out.println(lineCount);
  31. if((lineCount % 2) == 0){
  32. @SuppressWarnings("unchecked")
  33. ArrayList<String>[] out = new ArrayList[2];
  34. out[0] = new ArrayList<String>();
  35. out[1] = new ArrayList<String>();
  36. BufferedReader reader = new BufferedReader(new FileReader(in));
  37. String line = "";
  38. boolean flag = true;
  39. while((line=reader.readLine()) != null){
  40. if(flag){
  41. out[0].add(line);
  42. flag = false;
  43. }
  44. else{
  45. out[1].add(checkQuery(line));
  46. flag = true;
  47. }
  48. }
  49. reader.close();
  50. return out;
  51. }
  52. else
  53. throw new IllegalArgumentException("Malformed input.txt");
  54.  
  55. }
  56.  
  57. public static String checkQuery(String query){
  58. query = query.trim();
  59. if(query.contains("Id"))
  60. return query;
  61. else{
  62. String select = query.substring(0,6);
  63. select += " Id, ";
  64. select += query.substring(6);
  65. return select;
  66. }
  67. }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement