[英]Using for-loop, iterate through 2D array in order to pass those values into parameter
I am opening a text file that looks like this: 我正在打开一个文本文件,如下所示:
1;Rani;Hockey;BMW;1 Series;2011;WAUGFAFR4CA952133;2003;
2;Ranice;Dodge;Hyundai;Sonata;2005;WAUMF78PX6A683500;2013;
(has 90 more lines similar to the above) (还有90条与上述相似的行)
I have already split each words into a 2D array such that positions 我已经将每个单词拆分成2D数组,这样位置
[0][0] = 1
[0][1] = Rani
[0][2] = Hockey
[1][0] = 2
[1][1] = Ranice
[1][7] = 2013
I setup a for-loop to iterate through my 2D Array where (eventually) each index will be passed as a parameter to a constructor that I have. 我设置了一个for循环来遍历我的2D数组,最终(每个)索引将作为参数传递给我拥有的构造函数。 During the loop I keep getting:
在循环中,我不断得到:
Exception in thread "main" java.lang.NumberFormatException:
For input string: "2"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Test4.main(Test4.java:53)
Below are my code for my CarData class: 以下是我的CarData类的代码:
public CarData() {}
public CarData(int i, String fn, String ln, String cMake, String cMdl,
int cYear, String vin, int p)
{
this.customerID = i;
this.ownerFirstName = fn;
this.ownerLastName = ln;
this.carMake = cMake;
this.carModel = cMdl;
this.carYear = cYear;
this.carVin = vin;
this.yearCarPurchased = p;
}
public class CarDataDriver {
public static void main(String [] args)
{
final int col = 8;
final int row = 100;
int id = 0;
String fn = null;
String ln = null;
String cMake = null;
String cMdl = null;
int cYear = 0;
String vin = null;
int p = 0;
CarData [] data = new CarData[row];
// Open file
File file = new File("CarData.txt");
// Use scanner to scan new File
Scanner scanner;
try {
scanner = new Scanner(file);
// useDelimiter to separate String
String text = scanner.useDelimiter("\\A").next();
// Use split method to split String
String [] array = text.split(";");
String[][] array2D = new String[row][col];
//iterate through array to create 2D array
for (int i = 0, k=0; i < array2D.length; i++)
{
for (int j = 0; j < col; j++)
{
array2D[i][j] = array[k++];
}
}
for (int i = 0, k=0; i < array2D.length; i++)
{
for (int j = 0; j < col; j++)
{
if(j==0){
id = Integer.parseInt(array2D[i][j]); //first iteration where i = 0 runs fine, when i=1 and j=0 I get the error
continue;
}
if(j==1){
fn = array2D[i][j];
continue;
}
if(j==2) {
ln = array2D[i][j];
continue;
}
if(j==3){
cMake = array2D[i][j];
continue;
}
if(j==4){
cMdl = array2D[i][j];
continue;
}
if(j==5){
cYear = Integer.parseInt(array2D[i][j]);
continue;
}
if(j==6){
vin = array2D[i][j];
continue;
}
if(j==7){
p = Integer.parseInt(array2D[i][j]);
}
data[k] = new CarData(id, fn, ln, cMake, cMdl, cYear, vin, p);
k++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
} }
Your looping alone is fine - what is causing an error is an invalid character (a whitespace) parsed together with proper integer. 单独执行循环就可以了-导致错误的是将无效字符(空格)与正确的整数一起解析。 You may simply remove all whitespaces from your string with:
您可以使用以下命令从字符串中删除所有空格:
array2D[i][j].replaceAll("\\s+","")
(note, that this not modifies a string in array, but returns new one with removed spaces) (请注意,这不会修改数组中的字符串,而是返回带有空格的新字符串)
Where strange "\\\\s+"
string just means "any space character" (it's so called regular expression, but you don't need to understand it fully for now) 奇怪的
"\\\\s+"
字符串仅表示“任何空格字符”(即所谓的正则表达式,但是您现在不需要完全理解它)
However, note, that you have some more issues in your code, for example absolutely unnecessary inner loop with so numerous if
s, where every of them would execute just once - instead of this you can simply access array2D[i][1], array2D[i][2] and so on. 但是,请注意,您的代码中还有更多问题,例如,绝对不必要的内部循环,
if
有那么多的if
s,它们中的每一个都只会执行一次-代替此操作,您可以直接访问array2D [i] [1], array2D [i] [2]等。 Also keeping additional index k and incrementing it by hand seems to be redundant. 同样,保持附加索引k并手动增加它似乎是多余的。
Anyway, keep coding and good luck! 无论如何,请继续编码并祝您好运!
PS Using BufferedReader
and its readLine()
seems to be a much better (and simpler!) way to read a file. PS使用
BufferedReader
及其readLine()
似乎是一种更好(且更简单!)的方式来读取文件。 Take a glimpse on https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html 瞥一眼https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.