Skip to main content
deleted 106 characters in body
Source Link

I was thinking to separate the nested if-statements because I think its a bad OO code. Any ideas to enhance the code also, I tried to run the code in the cmd but I have error: could not find or load main class RecordReader.?

I was thinking to separate the nested if-statements because I think its a bad OO code. Any ideas to enhance the code also, I tried to run the code in the cmd but I have error: could not find or load main class RecordReader.

I was thinking to separate the nested if-statements because I think its a bad OO code. Any ideas to enhance the code?

Source Link

Enhance the performance of the source code in Java

I need help to enhance the performance of java source code. The code take txt file that has name, amount and date in form (name) (amount) (date yyyy-mm-dd) separated by spaces. After reading the file the data will be (name) (amount) (date dd/MM/yyyy).

    public static void main(String[] args) throws IOException {
    
    //we have an lang.ArrayIndexOutOfBoundsException because there is no index 0 in args
    long start = System.currentTimeMillis();
    File inputFile;
    int read;
    char[] charBuffer = new char[1024];
    String token = "";
    int tokenIndex = 0;
    String name = null;
    int year = -1;
    int month = -1;
    int day = -1;
    String amount = null;
    
    try {                       
        inputFile = new File(args[0]);
        FileReader reader = new FileReader(inputFile);
        while((read = reader.read(charBuffer)) != -1) {
            for (int i = 0; i < read; ++i) {
                char c = charBuffer[i];
                if (c == '\r' || c == '\n') {
                    if (token.length() != 0) {
                        // read date
                        String date = token;
                        year = Integer.parseInt(date.substring(0, 4));
                        month = Integer.parseInt(date.substring(5, 7));
                        day = Integer.parseInt(date.substring(8, 10));
                        // flush line
                        System.out.print(name.toUpperCase());
                        System.out.print(" ");
                        System.out.print(amount.indexOf('.') == -1 ? true : false);
                        System.out.print(" ");
                        System.out.print(day < 10 ? "0" + day : day);
                        System.out.print("/");
                        System.out.print(month < 10 ? "0" + month : month);
                        System.out.print("/");
                        System.out.print(year);
                        System.out.println();
                        name = null;
                        year = -1;
                        month = -1;
                        day = -1;
                        amount = null;
                        token = "";
                        tokenIndex = 0;
                    }
                }
                else if (c == ' ') {
                    // flush token
                    if (tokenIndex == 0) {
                        // read name
                        name = token;
                        token = "";
                        tokenIndex++;
                    }
                    else if (tokenIndex == 1) {
                        // read amount
                        amount = token;
                        token = "";
                        tokenIndex++;
                    }
                }
                else token = token + c;
            }
        } 
    } catch (ArrayIndexOutOfBoundsException e) {System.out.println (e);}
    System.out.println ("Time taken for String in if/else if :"+ (System.currentTimeMillis() - start));
}

I was thinking to separate the nested if-statements because I think its a bad OO code. Any ideas to enhance the code also, I tried to run the code in the cmd but I have error: could not find or load main class RecordReader.