从Excel读取整数值并将其写入.txt文件中的Java代码部分。

huangapple 未分类评论47阅读模式
英文:

Reading Copied Integer Values from Excel to .txt file in Java

问题

我想从文本文件中读取整数值到一个数组中。如果在.txt文件中键入数字,代码运行正常。然后,我尝试从Excel复制整数值到文本文件中,并尝试读取它。在这种情况下,我收到以下错误代码。

[989 1042 1059 1067 1087 1098 1103 1110 1114 1131]
Exception in thread "main" java.lang.NumberFormatException: For input string: "989 1042 1059 1067 1087 1098 1103 1110 1114 1131"
	at java.lang.NumberFormatException.forInputString(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at readparetoapprox.readapproxset(readparetoapprox.java:83)
	at DomSortepsilon.main(DomSortepsilon.java:11)

File fileText = new File(FileName);
Scanner sc = new Scanner(new BufferedReader(new FileReader(fileText)));

Scanner sc1 = new Scanner(new BufferedReader(new FileReader(fileText)));
String[] line1 = sc1.nextLine().trim().split(" ");

int rows = 2;
int columns = line1.length;
int[][] intarray = new int[rows][columns];

int x = 0;
while (x < 1) {
    for (int i = 0; i < intarray.length; i++) {
        String[] line = sc.nextLine().trim().split(" ");
        System.out.println(Arrays.toString(line));
        for (int j = 0; j < line.length; j++) {
            paretofront[i][j] = Integer.parseInt(line[j]);
        }
    }
    x++;
}
System.out.println(Arrays.deepToString(intarray));

我真的不知道为什么我收到NumberFormatException错误。

非常感谢您的帮助。

问候,
Martin

英文:

I want to read an integer value from a text file to an array. The code works fine if type the numbers in the .txt file. Afterwards i tried to copy integer values from excel to the text file and tried to read it. In this case i get the following error code.

[989	1042	1059	1067	1087	1098	1103	1110	1114	1131]
Exception in thread &quot;main&quot; java.lang.NumberFormatException: For input string: 
&quot;989	1042	1059	1067	1087	1098	1103	1110	1114	1131&quot;
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at readparetoapprox.readapproxset(readparetoapprox.java:83)
at DomSortepsilon.main(DomSortepsilon.java:11)

File fileText = new File(FileName);
Scanner sc = new Scanner(new BufferedReader(new FileReader(fileText)));
  
Scanner sc1 = new Scanner(new BufferedReader(new FileReader(fileText)));
String[] line1 = sc1.nextLine().trim().split(&quot; &quot;);

int rows = 2;
int columns = line1.length;
int [][] intarray= new int[rows][columns];

int x=0;
while(x&lt;1) {
   for (int i=0; i&lt;intarray.length; i++) {
      String[] line = sc.nextLine().trim().split(&quot; &quot;);
      System.out.println(Arrays.toString(line));
      for (int j=0; j&lt;line.length; j++) {
    	  paretofront[i][j] = Integer.parseInt(line[j]);
      }
   }
x++;
}
System.out.println(Arrays.deepToString(intarray));

I really have no idea why I receive the NumberFormatException error.

I´m really thankful for your help.

Greetings,
Martin

huangapple
  • 本文由 发表于 2020年5月3日 19:52:09
  • 转载请务必保留本文链接:https://java.coder-hub.com/61573973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定