Java - 线性搜索时出现 NumberFormatException

标签 java csv exception search

我的程序中出现 NumberFormatException 问题。基本上,我被要求读取由 ; 分隔的 .csv 文件,它看起来像这样:

//列说明(不在 .csv 文件中)

编号;概括;数字;雇员1;雇员2; .......员工7;

"1";"Sony";"1600";"Markos";"Nikos";"Antonis";"Nikolas";"Vaggelis";"Markos";"Thanasis";
"2";"HP";"1000";"Marios";"Dimitra";"Nikolia";"Spiros";"Thomas";"Kostas";"Manolis";
"3";"Dell";"1100";"Antonis";"Aggelos";"Baggelis";"Nikos";"Kuriakos";"Panagiotis";"Rafail";
"4";"Acer";"2000";"Marina";"Aggelos";"Spiros";"Marinos";"Xristos";"Antreas";"Basilis";

我已经做的是创建一个字符串二维数组或名为 temp_arr.csv 文件,我被要求编写一个方法来运行线性按 id 搜索并返回该公司。事情是这样的。

起初,我认为我应该将输入键从 int -> String 转换,因为我的 temp_arr 是一个 String 并比较字符串(当时它们是 int 但读作 String)使用 temp_arr[value][value2].equals(string_key)。但是我有一个 NullPointerException

然后我认为我应该更好地将我的 ID 从 temp_arr 转换为 String -> Int,然后使用 == 操作数与整数键进行比较。此操作返回了一个 NumberFormatException

过程是这样的:

System.out.println("Enter id :");
Scanner input = new Scanner(System.in); 
int item = input.nextInt();  // read the key which is an Integer
int id_int; // temp_arr is String and item is int, must convert ids from String -> int
for (int i = 0; i < temp_arr.length; i++)
{
   id_int = Integer.parseInt(temp_arr[i][0]); // Convert from String to int
    if (id_int == item) // If the Array's Id's are == item
    {
      System.out.println(item+" is present at location " + (i+1) );
      break;
    } 
     if (i == temp_arr.length) 
        System.out.println(item + " does not exist");
    }

我的错误出现在第 7 行,我不知道为什么。

读文件过程:

String csvFile = "sam.csv"; // .csv file to be placed in the project file!
BufferedReader br = null; // ini
String line = "",cvsSplitBy = ";"; // columns asked to be split by ";"
String[] arr = null;
String[][] temp_arr = new String[1000][10];
int temp = 0;
try
{
     br = new BufferedReader(new FileReader(csvFile)); //start reading the file
     while ((line = br.readLine()) != null) // while the line has words     
     { 
         arr = line.split(cvsSplitBy); // creating the array
         System.out.println(arr[0] + "\t" + arr[1] + "\t" + arr[2] + "\t" + arr[3] + "\t" + arr[4] + "\t" + arr[5] + "\t" + arr[6] + "\t" + arr[7] + "\t" + arr[8] + "\t" + arr[9] );
      for (int i = 0; i<=9; i++)
      {
           temp_arr[temp][i] = arr[i]; // temp_arr represents (is a copy of) the .csv file
      }
          temp++;
         }
    } catch (FileNotFoundException e) 
    {
       e.printStackTrace();
    } catch (IOException e)
    {
       e.printStackTrace();
    } finally 
    {
      if (br != null)
       {
                try 
                {
                    br.close();
                } catch (IOException e)
                {
                     e.printStackTrace();
                }
                 }
            }
   System.out.println("Done!\n");

输出(图像):Output

导致问题的第 106 行是: id_int = Integer.parseInt(temp_arr[i][0]);//字符串转整数

最佳答案

您的问题是您的 Integer.parseInt() 正在尝试解析带引号的“2”。这就是问题所在。

一个快速的解决方案是替换这一行:

temp_arr[temp][i] = arr[i];

对此:

temp_arr[temp][i] = arr[i].replaceAll("\"", "");

无论如何,我想建议您为您的案例使用不同的数据结构,因为我之前为客户做过类似的事情。你听说过 HashMap 吗?您可以使用 int 键和 String[] 值来执行类似 HashMap 的操作来存储数据,键可以是您的 id_int。也许下次你可以试试这个实现。它更加优雅。

希望我能帮上忙!

干杯, 贾斯汀

关于Java - 线性搜索时出现 NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29521069/

相关文章:

java - 未报告的异常IOException…必须被捕获或声明为引发

Java:获取对象的唯一哈希值

java - 在浏览器中显示传入的 JMS 消息

python - csv writer没有关闭文件

mysql - 是否可以使用 VB.Net 将 CSV 文件导入 MySQL?

php - 使用 PHPExcel lib 导入带制表符分隔的 CSV 文件

python - 修改异常消息而不丢失引发堆栈

java - spring 框架中 Service 和 DAO 接口(interface)的主要目的是什么?

java - 为每个请求分配新 session ID 的良好做法?

java - Java中什么时候需要添加 throws?