0

I have created a java program to count occurrences from a csv file and working fine in eclipse. Below is the code.

    import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import com.opencsv.CSVWriter;

public class ReadExcelDemo {
    public ReadExcelDemo(FileWriter fileWriter) {
        // TODO Auto-generated constructor stub
    }
    static void countEachWords(String fileName, Map < String, Integer > words) throws FileNotFoundException
    {
    Scanner file = new Scanner(new File(fileName));
    
    while (file.hasNext())
    {
        //String[] w=file.split(" ");
        String word=file.next();
        String key = word.split(",")[1];
        Integer count = words.get(key);
        if(count !=null)
            count++;
        else
            count =1;
        words.put(key, count);
    }
    file.close();
}
    public static void main(String args[]) throws IOException
    {
        Map<String, Integer> words= new HashMap<String, Integer>();
        
        countEachWords("C:\\apache-jmeter-5.1.1\\Results\\Login.csv", words);
        FileWriter csvWriter = new FileWriter("C:\\apache-jmeter-5.1.1\\Results\\output.csv");
        
        for(String keyset : words.keySet()) {
            
            String data = keyset +","+ words.get(keyset);
            String data1 = keyset;
            Integer data2= words.get(keyset);
            
            csvWriter.append(data);
            csvWriter.append("\n");         
        }
        csvWriter.close();
    }
}

While running in jmeter beanshell throwing below error.

Response message: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval  In file: inline evaluation of: ``import java.io.File; import java.io.FileNotFoundException; import java.io.FileWr . . . '' Encountered "<" at line 16, column 57.

Can anyone suggest how to fix this.enter image description here

0

2 Answers 2

2

Map<String, Integer> is the problematic statement, Beanshell lis not Java and it doesn't support modern Java features including diamond operator

If you want to proceed with Beanshell you need to remove these type declarations and leave just raw types like:

Map words= new HashMap();

Also be aware that starting from JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting as:

1
  • Thanks for help. Incorporated your suggestion. Working fine now. Jul 30, 2020 at 11:21
0

Written code in JSR223 Sampler. Below is the code.

import java.io.File;
import java.io.FileNotFoundException;    
import java.io.FileWriter;    
import java.io.IOException;    
import java.io.PrintStream;    
import java.util.HashMap;    
import java.util.Map;    
import java.util.Scanner;

System.out.println("Program started");

static void countEachWords(String fileName, Map words) throws FileNotFoundException

{

Scanner file = new Scanner(new File(fileName));
while (file.hasNext())   
{   
//String[] w=file.split(" ");    
String word=file.next();
String key = word.split(",")[0];
Integer count = (Integer) words.get(key);
if(count !=null)
count++;
else
count =1;
words.put(key, count);
}
file.close();
}
System.out.println("Starting main method");
Map words= new HashMap();
countEachWords("${inputRawFileToCountOccurrences}", words);
FileWriter csvWriter = new FileWriter("${inputFileForDBUpdate}");

for(Object keyset : words.keySet()) {
//String key = keyset.split(",")[1];
String data = keyset +","+ words.get(keyset);  
//String data1 = keyset; 
Integer data2= (Integer) words.get(keyset);    
csvWriter.flush();    
System.out.println(data);    
System.out.println(data2);   
csvWriter.append(data);   
csvWriter.append("\n");    
}   
csvWriter.close();

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.