0

I want to ask you if is it possible to change/decompose my code to 2-3 classes, add constructors (if possible not empty) and/or add more methods. If need program can have more functions.

 public class Testing {


            public static void main(String args[]) throws Exception {
                Scanner input = new Scanner(System.in);
                System.out.println("Select word from list:");
                System.out.println();

                try {
                    FileReader fr = new FileReader("src/lt/kvk/i3_2/test/List.txt"); // this is list of words, everything all right here
                    BufferedReader br = new BufferedReader(fr);
                    String s;
                    while((s = br.readLine()) != null) {
                        System.out.println(s);
                    }
                    fr.close();
                    String stilius = input.nextLine();   // eneter word which I want to count in File.txt
                    BufferedReader bf = new BufferedReader(new FileReader("src/lt/kvk/i3_2/test/File.txt")); // from this file I need to count word which I entered before

                    int counter = 0;                
                    String line;

                    System.out.println("Looking for information");
                    ArrayList<String> resultList = new ArrayList<String>();
                    String name = null;
            while (( line = bf.readLine()) != null){
                    if (line.trim().length() == 0) name = null;
                    else if (name == null) name = line;
                    int indexfound = line.indexOf(stilius);
                          if (indexfound > -1) {
                   counter++;
                   resultList.add(name);
   }
                    }
                    if (counter > 0) {
                        System.out.println("Word are repeated "+ counter + "times");}
                        else {
                        System.out.println("Error...");
                    }
                    bf.close(); 

                }
                catch (IOException e) {
                    System.out.println("Error:" + e.toString());
                    }
                }
            }

Program counting words (entered by keyboard) from file.txt and elect who repeated this word for ex.: if I enter word: One It shows:

Word One repeated 3 times by John, Elisa, Albert

file.txt looks like:

John //first line - name
One
Three
Four

Peter //first line - name
Two
Three

Elisa //first line - name
One
Three

Albert //first line - name
One
Three
Four

Nicole //first line - name
Two
Four

I don't know really if is possible to decompose this code to 2-3 classes. If someone could help me, thank you very much.

4
  • Have you tried anything yet? What specific problem is stopping you?
    – jbowes
    Nov 12, 2012 at 11:51
  • There is no big trick to decomposite a class into it's smaller independent functionalities, you just do exactly that, "decomposite a class into it's smaller independent functionalities". --- Steps: 1) Identify parts of your code that can work independently and that serve a specific functionality. 2) Modify it so that it performs its role in an flexible and easily-maintainable manner. 3) Done. (FileReader fr ... to fr.close(); - can be made into a public static String readFile(File file){...} method - You can then put that to a public static class FileUtils{...} class, for example.) Nov 12, 2012 at 11:58
  • I think you'd do better asking this question on codereview
    – Stephen C
    Nov 12, 2012 at 12:18
  • Everything is possible, you are a programmer after all, isn't it?
    – aaronps
    Nov 12, 2012 at 12:48

1 Answer 1

0

I would start by defining two classes:

  • WordFile
  • WordFileEntry

A WordFile-object should consist of a list of WordFileEntry-objects. A WordFileEntry consists of String name and List<String> words.

The counting of repetitions could be done by a WordFile-object itself. The logic of reading a file could be written in the WordFile-class or a separate class.

5
  • Could you write me how must look one of classes? And whats about constructors or methods?
    – user1768236
    Nov 12, 2012 at 12:50
  • This is just one possible (data-oriented) approach. There are no "musts". It seems to me that you need to learn some basics about classes and objects first.
    – COME FROM
    Nov 12, 2012 at 13:06
  • I know, I need, I'm just started learning, I wrote all in one class, but my task to decompose it to 2-3 classes and I dont know how and I need to use some constructors :| thats why I'm asking your help.
    – user1768236
    Nov 12, 2012 at 13:10
  • Sorry, I can't help you if you do not know how to start by writing a WordFileEntry class like I've described. I would just end up writing the whole solution for you and/or confusing you by introducing lots of unfamiliar concepts.
    – COME FROM
    Nov 12, 2012 at 13:59
  • As I know I need to use .get command when use 2 classes to connect them together, could you help me with me?
    – user1768236
    Nov 12, 2012 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.