| | |
Loading from text file help.
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2008
Posts: 33
Reputation:
Solved Threads: 0
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:
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)
scott-survey m/c mctext a.b.c.d. essay write about me
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)
public survey loadSurvey(String fileName) throws SecurityException, IOException { survey loadedSurvey = new survey(); // all the code for reading and parsing .txt file and setting up survey (loading) System.out.println("What directory is your file in? "); String whatDir; BufferedReader readWhatDir = new BufferedReader(new InputStreamReader(System.in)); whatDir = readWhatDir.readLine(); File file = null; FileReader freader = null; LineNumberReader lnreader = null; try{ file = new File(whatDir + fileName + ".txt"); freader = new FileReader(file); lnreader = new LineNumberReader(freader); String line = ""; while ((line = lnreader.readLine()) != null) { if (lnreader.getLineNumber() == 0) { loadedSurvey.setName(line); } else { if ((line.equals("m/c"))) { mc newMC = new mc(); String text = lnreader.readLine(); newMC.setQuesText(text); String lchoices = lnreader.readLine(); while (lchoices.indexOf(".") != -1) { lchoices = lchoices.substring(0, lchoices.indexOf(".")); newMC.addLeftChoice(lchoices); } String rchoices = lnreader.readLine(); while (rchoices.indexOf(".") != -1) { rchoices = rchoices.substring(0, rchoices.indexOf(".")); newMC.addRightChoice(rchoices); } String answer = lnreader.readLine(); newMC.setQuesAnswer(answer); loadedSurvey.addQues(newMC); } else if ((line.equals("t/f"))) { tf newTF = new tf(); String text = lnreader.readLine(); newTF.setQuesText(text); // here there is always just T and F but the save function creates two empty lines // just readLine() twice to get to the user answer for (int i = 0; i < 2; i++) { lnreader.readLine(); } String answer = lnreader.readLine(); newTF.setQuesAnswer(answer); loadedSurvey.addQues(newTF); } else if ((line.equals("matching"))) { matching newMatching = new matching(); String text = lnreader.readLine(); newMatching.setQuesText(text); String lchoices = lnreader.readLine(); while (lchoices.indexOf(".") != -1) { lchoices = lchoices.substring(0, lchoices.indexOf(".")); newMatching.addLeftChoice(lchoices); } String rchoices = lnreader.readLine(); while (rchoices.indexOf(".") != -1) { rchoices = rchoices.substring(0, rchoices.indexOf(".")); newMatching.addRightChoice(rchoices); } String answer = lnreader.readLine(); newMatching.setQuesAnswer(answer); loadedSurvey.addQues(newMatching); } else if ((line.equals("ranking"))) { ranking newRanking = new ranking(); String text = lnreader.readLine(); newRanking.setQuesText(text); String lchoices = lnreader.readLine(); while (lchoices.indexOf(".") != -1) { lchoices = lchoices.substring(0, lchoices.indexOf(".")); newRanking.addLeftChoice(lchoices); } // right choices are just the numbers to be ranked which is in the constructor // just perform one readLine() to get to next line lnreader.readLine(); String answer = lnreader.readLine(); newRanking.setQuesAnswer(answer); loadedSurvey.addQues(newRanking); } else if ((line.equals("essay"))) { essay newEssay = new essay(); String text = lnreader.readLine(); newEssay.setQuesText(text); String answer = lnreader.readLine(); newEssay.setQuesAnswer(answer); loadedSurvey.addQues(newEssay); } else if ((line.equals("s/a"))) { sa newSA = new sa(); String text = lnreader.readLine(); newSA.setQuesText(text); String answer = lnreader.readLine(); newSA.setQuesAnswer(answer); loadedSurvey.addQues(newSA); } } } } finally { freader.close(); lnreader.close(); } return loadedSurvey; }
![]() |
Similar Threads
- loading a text file into word document (C#)
- Cant readline in text file.... URGENT help~ (VB.NET)
- text file contains non delimited data into access database (VB.NET)
- loading non delimited text file into access database using VB.net (Visual Basic 4 / 5 / 6)
- Loading A Text File into a Array into a Listbox (Visual Basic 4 / 5 / 6)
- Binary Search on a text file (Java)
Other Threads in the Java Forum
- Previous Thread: Need help...!!!
- Next Thread: IOException when opening FileInputStream
| Thread Tools | Search this Thread |
android api applet application applications array arrays automation balls bank binary bluetooth business chat class classes clear client code codesnippet collections component database db defaultmethod development dice dragging draw ebook eclipse error event exception formatingtextintooltipjava fractal game givemetehcodez graphics gui hql html ide image infinite input integer invokingapacheantprogrammatically j2me java javaprojects jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie numbers openjavafx oracle parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads time tree windows





