943,565 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 442
  • Java RSS
Jun 24th, 2009
0

Loading from text file help.

Expand Post »
So I have my text files saving perfectly from my program in the exact way I want them to. Here is a copy of the text file that my program saves that I am now trying to load back in, parse, and set up internally as another object:

Java Syntax (Toggle Plain Text)
  1. scott-survey
  2. m/c
  3. mctext
  4. a.b.c.d.
  5.  
  6.  
  7. essay
  8. write about me
This is just one of the samples.

Here is the code that is supposed to take in this text file and set it all up internally. When I run the code, it recognizes that the text file exists and the directory it is in, but when it goes through all the loading code I have, it seems to just return a blank survey, like it doesn't load anything right. No errors, just doesn't work right. Why?

Here is my loading code:

java Syntax (Toggle Plain Text)
  1. public survey loadSurvey(String fileName) throws SecurityException, IOException {
  2.  
  3. survey loadedSurvey = new survey();
  4.  
  5.  
  6. // all the code for reading and parsing .txt file and setting up survey (loading)
  7. System.out.println("What directory is your file in? ");
  8. String whatDir;
  9. BufferedReader readWhatDir = new BufferedReader(new InputStreamReader(System.in));
  10. whatDir = readWhatDir.readLine();
  11.  
  12. File file = null;
  13. FileReader freader = null;
  14. LineNumberReader lnreader = null;
  15.  
  16. try{
  17. file = new File(whatDir + fileName + ".txt");
  18. freader = new FileReader(file);
  19. lnreader = new LineNumberReader(freader);
  20. String line = "";
  21. while ((line = lnreader.readLine()) != null) {
  22.  
  23. if (lnreader.getLineNumber() == 0) {
  24. loadedSurvey.setName(line);
  25. }
  26. else {
  27. if ((line.equals("m/c"))) {
  28. mc newMC = new mc();
  29. String text = lnreader.readLine();
  30. newMC.setQuesText(text);
  31. String lchoices = lnreader.readLine();
  32. while (lchoices.indexOf(".") != -1) {
  33. lchoices = lchoices.substring(0, lchoices.indexOf("."));
  34. newMC.addLeftChoice(lchoices);
  35. }
  36. String rchoices = lnreader.readLine();
  37. while (rchoices.indexOf(".") != -1) {
  38. rchoices = rchoices.substring(0, rchoices.indexOf("."));
  39. newMC.addRightChoice(rchoices);
  40. }
  41. String answer = lnreader.readLine();
  42. newMC.setQuesAnswer(answer);
  43. loadedSurvey.addQues(newMC);
  44. }
  45. else if ((line.equals("t/f"))) {
  46. tf newTF = new tf();
  47. String text = lnreader.readLine();
  48. newTF.setQuesText(text);
  49.  
  50. // here there is always just T and F but the save function creates two empty lines
  51. // just readLine() twice to get to the user answer
  52. for (int i = 0; i < 2; i++) {
  53. lnreader.readLine();
  54. }
  55.  
  56. String answer = lnreader.readLine();
  57. newTF.setQuesAnswer(answer);
  58. loadedSurvey.addQues(newTF);
  59. }
  60. else if ((line.equals("matching"))) {
  61. matching newMatching = new matching();
  62. String text = lnreader.readLine();
  63. newMatching.setQuesText(text);
  64. String lchoices = lnreader.readLine();
  65. while (lchoices.indexOf(".") != -1) {
  66. lchoices = lchoices.substring(0, lchoices.indexOf("."));
  67. newMatching.addLeftChoice(lchoices);
  68. }
  69. String rchoices = lnreader.readLine();
  70. while (rchoices.indexOf(".") != -1) {
  71. rchoices = rchoices.substring(0, rchoices.indexOf("."));
  72. newMatching.addRightChoice(rchoices);
  73. }
  74. String answer = lnreader.readLine();
  75. newMatching.setQuesAnswer(answer);
  76. loadedSurvey.addQues(newMatching);
  77. }
  78. else if ((line.equals("ranking"))) {
  79. ranking newRanking = new ranking();
  80. String text = lnreader.readLine();
  81. newRanking.setQuesText(text);
  82. String lchoices = lnreader.readLine();
  83. while (lchoices.indexOf(".") != -1) {
  84. lchoices = lchoices.substring(0, lchoices.indexOf("."));
  85. newRanking.addLeftChoice(lchoices);
  86. }
  87.  
  88. // right choices are just the numbers to be ranked which is in the constructor
  89. // just perform one readLine() to get to next line
  90. lnreader.readLine();
  91.  
  92. String answer = lnreader.readLine();
  93. newRanking.setQuesAnswer(answer);
  94. loadedSurvey.addQues(newRanking);
  95. }
  96. else if ((line.equals("essay"))) {
  97. essay newEssay = new essay();
  98. String text = lnreader.readLine();
  99. newEssay.setQuesText(text);
  100. String answer = lnreader.readLine();
  101. newEssay.setQuesAnswer(answer);
  102. loadedSurvey.addQues(newEssay);
  103. }
  104. else if ((line.equals("s/a"))) {
  105. sa newSA = new sa();
  106. String text = lnreader.readLine();
  107. newSA.setQuesText(text);
  108. String answer = lnreader.readLine();
  109. newSA.setQuesAnswer(answer);
  110. loadedSurvey.addQues(newSA);
  111. }
  112. }
  113.  
  114. }
  115. }
  116.  
  117. finally {
  118.  
  119. freader.close();
  120. lnreader.close();
  121.  
  122. }
  123.  
  124. return loadedSurvey;
  125. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
gotm is offline Offline
33 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Need help...!!!
Next Thread in Java Forum Timeline: IOException when opening FileInputStream





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC