4 Replies - 617 Views - Last Post: 10 May 2010 - 01:55 PM Rate Topic: -----

#1 Codey09   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 214
  • Joined: 20-August 09

Reading contents of a file [SOLVED]

Posted 10 May 2010 - 01:22 PM

I working on my assignment where I need to read 5 lines of text from a txt file, these will be stored as 1 string variable and the rest into an array.

Here is my code :
try{
FileReader = fr;
String fileName = "C:\\Users\\Darren\\Documents\\Uni Work\\Java\\Assignment\\Task B\\questionFile.txt";
fr = new FileReader (fileName);
BufferedReader br = new BufferedReader(fr);
question = br.readLine();
for (int i = 0; i < 4; i++) {
answers[i] = br.readLine();
}
fr.close();
}
catch (IOException err) {
Dialogs.errorMessage("File Error");
}


These are the errors I'm getting to which I dont understand
C:\Users\Darren\Documents\Uni Work\Java\Assignment\Task B>javac Questions.java
Questions.java:13: illegal start of type
try{
^
Questions.java:13: ';' expected
try{
   ^
Questions.java:14: <identifier> expected
FileReader = fr;
          ^
Questions.java:16: <identifier> expected
fr = new FileReader (fileName);
  ^
Questions.java:18: <identifier> expected
question = br.readLine();
        ^
Questions.java:19: illegal start of type
for (int i = 0; i < 4; i++) {
^
Questions.java:19: ')' expected
for (int i = 0; i < 4; i++) {
          ^
Questions.java:19: illegal start of type
for (int i = 0; i < 4; i++) {
             ^
Questions.java:19: <identifier> expected
for (int i = 0; i < 4; i++) {
              ^
Questions.java:19: ';' expected
for (int i = 0; i < 4; i++) {
               ^
Questions.java:19: <identifier> expected
for (int i = 0; i < 4; i++) {
                   ^
Questions.java:19: illegal start of type
for (int i = 0; i < 4; i++) {
                     ^
Questions.java:19: '(' expected
for (int i = 0; i < 4; i++) {
                        ^
Questions.java:22: <identifier> expected
fr.close();
        ^
Questions.java:24: class, interface, or enum expected
catch (IOException err) {
^
Questions.java:26: class, interface, or enum expected
}
^
Questions.java:28: class, interface, or enum expected
        public void setResult(int result){              // this method set the a
nswers from the vote to the correct place in the results array and adds the valu
e of 1 each time that selection has been made
               ^
Questions.java:31: class, interface, or enum expected
                } else if (result == 2){
                ^
Questions.java:33: class, interface, or enum expected
                } else if (result == 3){
                ^
Questions.java:35: class, interface, or enum expected
                } else if (result == 4){
                ^
Questions.java:37: class, interface, or enum expected
                }
                ^
Questions.java:40: class, interface, or enum expected
        public int[] getResults(){              // returns the results array
               ^
Questions.java:42: class, interface, or enum expected
        }
        ^
Questions.java:44: class, interface, or enum expected
        public String getQuestion(){            // returns the poll question
               ^
Questions.java:46: class, interface, or enum expected
        }
        ^
Questions.java:48: class, interface, or enum expected
        public String[] getAnswers(){           // returns the answers array
               ^
Questions.java:51: class, interface, or enum expected
        }
        ^
27 errors


Have I missed something really obvious that I cant see it for looking?

This post has been edited by Codey09: 10 May 2010 - 01:52 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Reading contents of a file [SOLVED]

#2 NoobKnight   User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: Reading contents of a file [SOLVED]

Posted 10 May 2010 - 01:37 PM

I'm not an expert, but you can try this...

Just change the first line, and see what happens.
FileReader fr = null;
String fileName = "C:\\Users\\Darren\\Documents\\Uni Work\\Java\\Assignment\\Task B\\questionFile.txt";
fr = new FileReader (fileName);


Was This Post Helpful? 0
  • +
  • -

#3 Ember   User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 160
  • Joined: 24-April 10

Re: Reading contents of a file [SOLVED]

Posted 10 May 2010 - 01:44 PM

FileReader = fr;


Needs to be:
 FileReader fr;


Also, did you declare the Question and Answer before the code you posted? If yes, this should work perfectly.

This post has been edited by Ember: 10 May 2010 - 01:44 PM

Was This Post Helpful? 1
  • +
  • -

#4 Codey09   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 214
  • Joined: 20-August 09

Re: Reading contents of a file [SOLVED]

Posted 10 May 2010 - 01:51 PM

Thank you Ember that was the problem, I always do something as simple as that and take me ages to spot it.
Was This Post Helpful? 0
  • +
  • -

#5 Ember   User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 160
  • Joined: 24-April 10

Re: Reading contents of a file [SOLVED]

Posted 10 May 2010 - 01:55 PM

View PostCodey09, on 10 May 2010 - 12:51 PM, said:

Thank you Ember that was the problem, I always do something as simple as that and take me ages to spot it.

No problem. That always happened when I started out. Sometimes I would forget to put it in the main method and would wonder why it gave an error when I inserted a perfectly structured for loop. :sweatdrop:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1