解析从输入流读取的 csv 文件中数组的第一项时出现问题


文件
  1. 是从CSV读取的。
  2. 字符串使用分号拆分。
  3. 解析数组的第一个字符串返回 NumberformatException。错误<</li>

我已经尝试过的事情:

  1. Integer.Valueof(str(;
  2. Integer.parseInt(str(;
  3. pp = 新整数(str(;

有人可以帮助解决这个问题吗? 谢谢

public static void main(String[] args) throws FileNotFoundException, IOException {
File inputF = new File("C:\chatse\Estatistica\dados.csv");
InputStream inputFS = new FileInputStream(inputF);
BufferedReader in = new BufferedReader(new InputStreamReader(inputFS));
String line;
String readFromCsv = "";
while ((line = in.readLine()) != null) {
readFromCsv += line + ";";
}
in.close();
String read = readFromCsv.trim();
String[] n = read.split(";");
ArrayList<String> lista = new ArrayList<>();
for (String st : n) {
lista.add(st);
}
ArrayList<Integer> nlistNum = new ArrayList<>();
for (String data : lista) {
int pp Integer.Valueof(data); //tried this,
int pp Integer.parseInt(data); // this,
int pp = new Integer(data); // and this.
nlistNum.add(pp);
}

问题是我的数组的第一个索引有一个"ASCII 65279",所以我 通过将这个索引拆分为字符数组并检索来解决这个问题 其长度

n = readFromCsv.split(";");
for (String st : n) {
char[] charr = st.toCharArray();
String str = "";
StringBuilder sb = new StringBuilder();
for (char c : charr) {
int asci = (int) c;
//this verify if it is in the range of number
if (asci >= 48 && asci <= 57) {
sb.append(c);
}
}
str = sb.toString();
int novoInt = Integer.valueOf(str);
nlistNum.add(novoInt);
}

最新更新