There is a file report.CSV .

The program's task is to count how many times a word from column К and to calculate the sum of numbers from column R for each word from column К

Here is the program:

 public class CSVtestclass { public static void main(String[] args) throws IOException { String FileName = "c:\\TestJava\\report.csv"; File file = new File(FileName); Map<String, WordInfo> wordInfoMap = new HashMap<>(); try(BufferedReader read = new BufferedReader(new FileReader(file))){ String s; String wordName; HashMap<String, int[]> stringIntegerHashMap = new HashMap<>(); read.readLine(); while( (s = read.readLine()) != null) { String[] next = s.split(";"); wordName = next[10]; if (stringIntegerHashMap.containsKey(wordName)) { int[] ints = stringIntegerHashMap.get(wordName); ints[0]++; ints[1] += Integer.parseInt(next[17]); } else { stringIntegerHashMap.put(wordName, new int[]{1, Integer.parseInt(next[17])}); } } for (Map.Entry<String, int[]> pair: stringIntegerHashMap.entrySet()) { int[] value = pair.getValue(); System.out.printf("%s zayavki: %d interakcii: %d%n", pair.getKey(), value[0], value[1]); } } catch (FileNotFoundException e) { e.printStackTrace(); } } } 

During program execution, an error occurs:

 Exception in thread "main" java.lang.NumberFormatException: For input string: ""0"" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.base/java.lang.Integer.parseInt(Integer.java:638) at java.base/java.lang.Integer.parseInt(Integer.java:770) at CSVpack.CSVtestclass.main(CSVtestclass.java:31) 

picture with an error

Where am I wrong?

2 answers 2

public class CSVtestclass {

 public static void main(String[] args) throws IOException { String FileName = "c:\\TestJava\\report.csv"; File file = new File(FileName); Map<String, WordInfo> wordInfoMap = new HashMap<>(); try (BufferedReader read = new BufferedReader(new FileReader(file))) { String s; String wordName; HashMap<String, int[]> stringIntegerHashMap = new HashMap<>(); read.readLine(); while ((s = read.readLine()) != null) { try { String[] next = s.split(";"); wordName = next[10]; next[17] = next[17].replace("\"", ""); if (stringIntegerHashMap.containsKey(wordName)) { int[] ints = stringIntegerHashMap.get(wordName); ints[0]++; ints[1] += Integer.parseInt(next[17]); } else { stringIntegerHashMap.put(wordName, new int[]{1, Integer.parseInt(next[17])}); } } catch (NumberFormatException e) { System.out.println(e); } } for (Map.Entry<String, int[]> pair : stringIntegerHashMap.entrySet()) { int[] value = pair.getValue(); System.out.printf("%s zayavki: %d interakcii: %d%n", pair.getKey(), value[0], value[1]); } } catch (FileNotFoundException e) { e.printStackTrace(); } } 

}

There are several important points ... 1. The exception flies because 0 is recorded not just as 0, but as "0". This can not be parsed. The quotes will have to be removed by hand if you want it to work. 2. The Nth line in the cell you are trying to parse contains just text. Here the task is completely unbearable, decide yourself what to do with this text. 3. Pars - a potentially dangerous place in terms of exceptional situations. Therefore, it is strongly recommended to frame such try-catch blocks.

Total, I added replacement of quotes, the try-catch block too. In the block, the usual output to the console of the message with the exception, respectively, when you start, you will see the line, the text that you cannot turn into numbers, of course, decide for yourself what to write in the try-catch block depending on the business task. Good luck

  • did not understand what this code means? - Vaagn Akopyan
  • This is your code just fixed. replace it and everything will be - Dmitriy
  • I do not see in the file that 0 is written in quotes, where does he find these quotes? - Vaagn Akopyan
  • The fact is that it does not just take 0 in quotes, but it quotes all the data from the file using a semicolon as a separator. You do not forget that in fact behind the document itself is xml. To understand the situation, remember the Excel document, where the cell can be marked as text, or you can mark it as a number. Now imagine that you read only text data and zero in this context is also text, you end up with a quoted zero. everything is actually a bit wrong, but it's easier to understand - Dmitriy
  • but in general, if you want to parse various documents, save data in various formats, look in the direction of apache POI. I have been using it for a long time and do not know grief. for high-quality formatting of documents, of course, you will have to write a lot of code, but at the output this document cannot be distinguished from what is done by hand in principle. And on any formats it is sharpened much better, than standard means java. Am, these same quotes are already part of the context, and do not callous eyes - Dmitriy
 Exception in thread "main" java.lang.NumberFormatException: For input string: ""0"" 

Indicates that you are trying to convert a "0" to a number. But what is the number of "?

before converting a string to a number, remove all unnecessary

 string.replaceAll("\"", ""); - в вашем случае string.replaceAll("\\D", ""); - для удаления всех символов (не чисел) 

You should execute the following commands in the else block before investing in the map (31 line):

 String number = next[17].replaceAll("\"", ""); int value = Integer.parseInt(number); 

or shorter:

 int value = Integer.parseInt(next[17].replaceAll("\"", "")); 

and put it in mapa.

  • Victor, I can ask in my code to specify specifically what you need to enter? Sorry for the tediousness, just do not quite understand - Vaagn Akopyan
  • @VaagnAkopyan added - Victor
  • Victor, does not recognize replaceall - cdn1.savepice.ru/uploads/2017/12/2/… ... - Vaagn Akopyan
  • Yes, after replaceAll, the point is unnecessary, a pancake crept in, my mistake =) Corrected in the post - Victor
  • Now another mistake is cdn1.savepice.ru/uploads/2017/12/2/… - Vaagn Akopyan