1

I am getting errors that the JSON objects and file reader objects cannot be found. However, I have imported the correct libraries and it still does not work. The code I have until now is as follows:

import org.json.*;
import java.sql.*;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Test {
    private static HashSet<String> seenAttributes = new HashSet<>();
    public static void parseBusinessData() {
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        String filePath = "C:/Users/jay/documents/c_______/yelp_business.json";
        try {
            fReader = new FileReader(filePath);
            bufferedReader = new BufferedReader(fileReader);
            String line;
            bufferedReader.readLine();
            int index = 0;
            while ((line = bufferedReader.readLine()) != null) {
                JSONObject obj = new JSONObject(line);
        
                /*
                CREATE TABLE Business_Attributes (
                    Business_Id char(22) NOT NULL,
                    Attribute_Id number(5) NOT NULL,
                
                CREATE TABLE Attributes (
                    Attribute_Id number(5) NOT NULL,
                    Attribute_Name varchar2(50) NOT NULL,
                */

                JSONArray attributeArray = obj.getJSONArray("attributes");

                for(int i = 0; i < attributeArray.length; i++){
                    String attribute = attributeArray.getString(i);
                    System.out.println("Attribute = " + attribute);
                    if(!seenAttributes.contains(attribute)){
                        seenAttributes.add(attribute);
                    }
                }
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                bufferedReader.close();
                fReader.close();
            } catch (IOException ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void main(String[] args){
        parseBusinessData();
    }
}

And I get the following errors:

Test.java:23: error: cannot find symbol
            fReader = new FileReader(filePath);
            ^
  symbol:   variable fReader
  location: class Test
Test.java:29: error: cannot find symbol
                JSONObject obj = new JSONObject(line);
                ^
  symbol:   class JSONObject
  location: class Test
Test.java:29: error: cannot find symbol
                JSONObject obj = new JSONObject(line);
                                     ^
  symbol:   class JSONObject
  location: class Test
Test.java:41: error: cannot find symbol
                JSONArray attributeArray = obj.getJSONArray("attributes");
                ^
  symbol:   class JSONArray
  location: class Test
Test.java:58: error: cannot find symbol
                fReader.close();
                ^
  symbol:   variable fReader
  location: class Test
5 errors

It would be great if you could help me resolve these errors! I've spent a lot of time on this and don't seem to know what's the issue here. From what I've read online, I need to download some jar files but I'm not sure which ones.

jay
  • 31
  • 6

1 Answers1

1

You need to change your fReader to fileReader
For JSONObject and JSONArray make sure you have added the required jars from org.json in your classpath.

Adding jars to classpath

Righ click on project -> Build Path -> Configure build path
Select the libraries tab -> Add external jars.

Add all required jars, you can refer image below buiild path enter image description here

SSK
  • 3,444
  • 6
  • 32
  • 59
  • I've downloaded org.json.jar but I'm unsure how to add it to the classpath. Any tips? – jay Mar 02 '21 at 11:48
  • @jay If you are using eclipse I have added steps to answer – SSK Mar 02 '21 at 13:23
  • Hi @SSK, I have done the same thing and have added a json simple jar file to the java build path in Eclipse. However, it still does not work. Do I need to do something more to make this work? – jay Mar 02 '21 at 16:27
  • I run javac file.java in terminal once I have added the jar file to the build path. Is this correct? Do I need additional command line arguments? – jay Mar 02 '21 at 16:32