-1

I think I've almost figured out my java program. It is designed to read a text file and find the largest integer by using 10 different threads. I'm getting this error though:

Error:(1, 8) java: class Worker is public, should be declared in a file named Worker.java

I feel my code may be more complex than it needs to be so I'm trying to figure out how to shrink it down in size while also fixing the error above. Any assistance in this matter would be greatly appreciated and please let me know if I can clarify anything. Also, does the "worker" class have to be a seperate file? I added it to the same file but getting the error above.

import java.io.BufferedReader;
import java.io.FileReader;

public class datafile {

    public static void main(String[] args) {
        int[] array = new int[100000];
        int count;
        int index = 0;
        String datafile = "dataset529.txt"; //string which contains datafile
        String line; //current line of text file

        try (BufferedReader br = new BufferedReader(new FileReader(datafile))) { //reads in the datafile
            while ((line = br.readLine()) != null) { //reads through each line
                array[index++] = Integer.parseInt(line); //pulls out the number of each line and puts it in numbers[]
            }
        }



            Thread[] threads = new Thread[10];
            worker[] workers = new worker[10];


            int range = array.length / 10;
            for (count = 0; count < 10; count++) {
                int startAt = count * range;
                int endAt = startAt + range;
                workers[count] = new worker(startAt, endAt, array);

            }

            for (count = 0; count < 10; count++) {
                threads[count] = new Thread(workers[count]);
                threads[count].start();
            }

            boolean isProcessing = false;
            do {
                isProcessing = false;
                for (Thread t : threads) {
                    if (t.isAlive()) {
                        isProcessing = true;
                        break;
                    }
                }
            } while (isProcessing);

            for (worker worker : workers) {
                System.out.println("Max = " + worker.getMax());
            }
        }


    }


     public  class worker implements Runnable {

        private int startAt;
        private int endAt;
        private int randomNumbers[];

        int max = Integer.MIN_VALUE;

        public worker(int startAt, int endAt, int[] randomNumbers) {
            this.startAt = startAt;
            this.endAt = endAt;
            this.randomNumbers = randomNumbers;
        }

        @Override
        public void run() {
            for (int index = startAt; index < endAt; index++) {

                if (randomNumbers != null && randomNumbers[index] > max)
                    max = randomNumbers[index];
            }
        }

        public int getMax() {
            return max;
        }

    }
Vortex11
  • 171
  • 1
  • 3
  • 11
  • The 'EOF while parsing' issue is caused by the stray cloiseng braces `}` at the end of your source code file. – Knells Oct 29 '15 at 22:23
  • I'm getting this error too. I fixed the closing brace in my worker class. Error:(58, 1) java: class, interface, or enum expected – Vortex11 Oct 29 '15 at 22:25
  • The closing braces in the worker class were fine. The end of the `readTextFile` classs has too many closing braces – Knells Oct 29 '15 at 22:27
  • As for the other part of your question. There is a SE site specifically for [codereview.se] so you'd probably be better off asking there – Knells Oct 29 '15 at 22:28
  • Oh wait, I fixed a silly variable error I had and fixed the closing braces. I''m getting this new error now. See above please. – Vortex11 Oct 29 '15 at 22:34
  • That's probably going to be an easy one: Is your source for `Worker` saved in a file called `Worker.java` ? – Knells Oct 29 '15 at 22:37
  • Yes, i have worker.java and datafile.java – Vortex11 Oct 29 '15 at 22:39

1 Answers1

1

I've written a few comments but I'm going to gather them all in an answer so anyone in future can see the aggregate info:

At the end of your source for the readtextfile class (which should be ReadTextile per java naming conventions) you have too many closing braces,

    } while (isProcessing);

    for (Worker worker : workers) {
        System.out.println("Max = " + worker.getMax());
    }
}

}
}
        }

The above should end on the first brace that hits the leftmost column. This is a good rule of thumb when making any Java class, if you have more than one far-left brace or your last brace isn't far-left you've probably made a mistake somewhere and should go through checking your braces.

As for your file issues You should have all your classes named following Java conventions and each class should be stored in a file called ClassName.java (case sensitive). EG:

public class ReadTextFileshould be stored in ReadTextFile.java

You can also have Worker be an inner class. To do this you could pretty much just copy the source code into the ReadTextFile class (make sure it's outside of the main method). See this tutorial on inner classes for a quick overview.

As for the rest of your question Code Review SE is the proper place to ask that, and the smart folks over there probably will provide better answers than I could. However I'd also suggest using 10 threads is probably not the most efficient way in to find the largest int in a text file (both in development and execution times).

Community
  • 1
  • 1
Knells
  • 827
  • 2
  • 12
  • 24
  • Thank you for your help. Sometimes I overlook the simplest of things. Right now the 10 threads are required, so I can't change the program, only codense the code. – Vortex11 Oct 29 '15 at 23:04
  • Do you mean you added the `Worker` class as an inner to the other class? What software/commands are you using to compile these classes? – Knells Oct 29 '15 at 23:53
  • I just want it all in one .java file instead of 2. Then will work on condensing. I'm using IntelliJ – Vortex11 Oct 29 '15 at 23:53
  • You shouldn't edit your question to ask further questions. This site is used by many more people than just yourself, and making these edits makes it difficult for anyone else who comes along to get useful information out of the Q&A. Questions need to be specific (i.e. limited to one question/problem per post) – Knells Oct 30 '15 at 00:02
  • Take [this code](http://pastebin.com/cJqjn4cb), which is a slightly altered version of your edit. Save it as DataFile.java, delete everything else and then compile, then come back here and revert your edits such that they don't remove information for other users. Finally take a read of the naming conventions linked in my answer, otherwise you will keep having similar problems. – Knells Oct 30 '15 at 00:06