CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2022
    Posts
    2

    Compile Time Error in Online Java Compiler

    Hello Everyone, I am new in this community and I am facing an issue while running the below program. This code is working fine on eclipse. but if I submit it for online compilation here. it shows compile time error. Can anyone suggest to me, what the exact error is this?

    error message
    *database.java 163 cannotfindsymbolsymbol methodcompare int int location class java.lang.Integer return Integer.compare left right ;^1error*

    Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Scanner;
    import java.io.FileNotFoundException;
    
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Iterator;
    
    import java.util.List;
    import java.util.ArrayList;
    import java.util.StringTokenizer;
    
    public class database {
        String fileName;
        Scanner input;
        String[][] data;
        List<String> useful_list;
        List<String> records;
        ArrayList<Object> handles;
    
        public database(String fileName) {
            this.fileName = fileName;
        }
    
        public void openFile() {
            try {
                input = new Scanner(new File(fileName));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                return;
            }
        }
    
        public void readRecords() {
            // Read all lines (records) from the file into an ArrayList
            records = new ArrayList<String>();
            try {
                while (input.hasNext())
                    records.add(input.nextLine());
    
            } catch (Exception e) {
                // TODO: handle exception
            }
    
        }
    
        public void parseFields() {
            String delimiter = ",\n";
    
            // Create two-dimensional array to hold data (see Deitel, p 313-315)
            int rows = records.size(); // #rows for array = #lines in file
            data = new String[rows][]; // create the rows for the array
            int row = 0;
    
            for (String record : records) {
                StringTokenizer tokens = new StringTokenizer(record, delimiter);
                int cols = tokens.countTokens();
                data[row] = new String[cols]; // create columns for current row
                int col = 0;
                while (tokens.hasMoreTokens()) {
                    data[row][col] = tokens.nextToken().trim();
    
                    col++;
    
                }
    
                row++;
    
            }
    
        }
    
        public static void main(String[] args) {
            String filename = null;
            String[] values = new String[4];
            String input = null;
    
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    System.in));
    
            try {
                filename = reader.readLine();
                input = reader.readLine();
                values = input.split(",");
    
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println("Invalid Input");
                return;
            }
    
            int[] input1;
            input1 = new int[4];
    
            try {
                for (int j = 0; j < values.length; j++) {
                    input1[j] = Integer.parseInt(values[j]);
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                System.out.println("Invalid Input");
                return;
            }
    
            if (input1[0] >= 4 || input1[0] <= 0) {
                System.out.println("Invalid Input");
                return;
            }
    
            database file1 = new database(filename);
            file1.openFile();
            file1.readRecords();
            file1.parseFields();
            file1.search(input1[1]);
            if (file1.useful_list.size() == 0) {
                System.out.println("Data Unavailable");
                return;
            }
    
            file1.sortarray(input1[0] - 1);
    
            int width = input1[2];
            int skip = (input1[3] - 1) * width;
            Iterator<Object> it = file1.handles.iterator();
            for (int i = 1; i <= skip; i++) {
                if (it.hasNext()) {
                    it.next();
                } else {
                    System.out.println("Data Unavailable");
                    return;
                }
    
            }
    
            for (int j = 1; j <= width && it.hasNext(); j++) {
                String[] a = (String[]) it.next();
                for (int i = 0; i < a.length; i++)
                    if(i<a.length-1)
                    System.out.print(a[i] + ",");
                    else
                        System.out.print(a[i]);
                System.out.println();
            }
    
        }
    
        void sortarray(final int index) {
            handles = new ArrayList<Object>();
            for (int i = 0; i < data.length; i++)
                handles.add(data[i]);
    
            Collections.sort(handles, new Comparator<Object>() {
                public int compare(Object o1, Object o2) {
                    String[] a = (String[]) o1;
                    String[] b = (String[]) o2;
                    if (index == 1 || index == 0) {
                        int left = Integer.parseInt(a[index]);
                        int right = Integer.parseInt(b[index]);
                        return Integer.compare(left, right); //Line 165 
                    } else {
    
                        if (a.length == 0 && b.length == 0)
                            return 0;
                        if (a.length == 0 && b.length != 0)
                            return 1;
                        if (a.length != 0 && b.length == 0)
                            return -1;
                        return a[index].compareTo(b[index]);
                    }
                }
    
                public boolean equals(Object o) {
                    return this == o;
                }
    
            });
    
        }
    
        void search(int searchs) {
            useful_list = new ArrayList<String>();
    
            for (int row = 0; row < data.length; row++) {
    
                if (Integer.parseInt(data[row][0]) == searchs) {
                    // store in array list
                    useful_list.add(data[row][0] + "," + data[row][1] + ","
                            + data[row][2] + "," + data[row][3]);
    
                }
            }
            if (useful_list.size() == 0) {
                return;
            }
            String delimiter = ",\n";
    
            // Create two-dimensional array to hold data (see Deitel, p 313-315)
            int rows = useful_list.size(); // #rows for array = #lines in file
            data = new String[rows][]; // create the rows for the array
            int row1 = 0;
    
            for (String record : useful_list) {
                StringTokenizer tokens = new StringTokenizer(record, delimiter);
                int cols = tokens.countTokens();
                data[row1] = new String[cols]; // create columns for current row
                int col1 = 0;
                while (tokens.hasMoreTokens()) {
                    data[row1][col1] = tokens.nextToken().trim();
    
                    col1++;
    
                }
    
                row1++;
    
            }
    
        }
    
    }

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Compile Time Error in Online Java Compiler

    What version of java does that site use? See the API doc for the compare method to see what version of java it was released in.
    Norm

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured