0

The goal is to fill an ArrayList with custom Country objects made up of information from a separate text file. The while loop gives me the "identifier expected" error, and I'm at my wit's end trying to fix it.

import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
import java.io.FileNotFoundException;

public class Driver {

    public static void main(String[] args) {
        //Instance variables
        Scanner sc;
        Country next = new Country();
        String reader;
        int size;
        ArrayList<Country> ledger = new ArrayList<Country>();

        //Suppressing this exception because I know it's there.
        @SuppressWarnings("unreported exception FileNotFoundException; must be caught or declared to be thrown")
        sc = new Scanner(new File("testLedger.txt"));

        //"<identifier> expected" error
        while (sc.hasNext()) {
            next.setName(sc.nextLine());
            next.setFaith(sc.nextLine());
            next.setInfo(sc.nextLine());
            next.setOrder(sc.nextInt());
            ledger.add(next);
        }

        //Test accessor methods and filling of the ArrayList
        for (int i = 0; i < ledger.size(); i++) {
            System.out.println(ledger.get(i));
        }
    }
}
H. Davis
  • 55
  • 5
  • 1
    I think if you try putting `throws FileNotFoundException` at the top of the method, this might compile. – Dawood ibn Kareem Oct 04 '18 at 19:00
  • 1
    This is may not be what you are looking for, but are you sure that you aren't just overwriting and re-adding the same `Country` object throughout the loop? – cainns98 Oct 04 '18 at 19:02
  • 1
    Also, you REALLY want to look at https://stackoverflow.com/q/13102045 which gives you the answer to the next question you're going to ask after you get this to compile. – Dawood ibn Kareem Oct 04 '18 at 19:02

1 Answers1

0

First, your code will not compile. You need to handle the exceptions. As in this case you are just running a test, you can use a Throw in the main method.

    try {
        sc = new Scanner(new File("testes.txt"));

        while (sc.hasNext()) {

            next.setName(sc.nextLine());
            next.setFaith(sc.nextLine());
            next.setInfo(sc.nextLine());
            next.setOrder(sc.nextInt());
            ledger.add(next);
        }
    } catch (FileNotFoundException e) {
        System.out.println(e);
    }

Second, look at the setters of your Country class and see if the method types are compatible with what you are using in the while.

For example:

sc.nextLine () // will return a String
sc.nextInt () // will return an int

your setters should be compatible with this

public void setOrder(int order){
    this.order = order;
}

and Finally, as mentioned by @Dawood in comments, you need do see stackoverflow.com/q/13102045

  • The error moves to sc = new Scanner(new File("testLedger.txt")); when I hide the loop as a comment. – H. Davis Oct 04 '18 at 21:21
  • Move your "sc = new Scanner(new File("testLedger.txt"))" into a try / catch block – Newton Santos Oct 05 '18 at 13:26
  • I did, with the catch block terminating via System.exit(0). I then get "variable sc might not have been initialized" for next.setName(sc.nextLine()). When I suppress that, I instead get the "identifier expected" error in the same place, as well as " ';' expected" on the same line for some reason. Executing tells me it can't find the main class, if that narrows it down any. – H. Davis Oct 05 '18 at 18:14