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

asked Jul 28, 2020 at 13:38
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:

answered Jul 28, 2020 at 15:08
1
  • Thanks for help. Incorporated your suggestion. Working fine now. Commented 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();
answered Jul 30, 2020 at 11:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.