mardi 17 mars 2015

Java, Reading a file into an array, else If statement not working

I have the code below which reads data from a text file and inputs it into a Product array, however only the price is being inputted into the Product class due to the else if statement not working, what i need to know is how to make the "countLine" +1 once it has run through the if statement (10 times) so that the else if statement will work.



File input = new File("productData.txt");
Scanner sc = new Scanner(input);
Integer arraySize = new Integer(sc.nextLine());
Product[] productList = new Product[arraySize];
String[] productCode = new String[arraySize];
int[] productPrice = new int[arraySize];
int countLine = 0;

while(sc.hasNextLine()){
int count = 0;
String line = new String(sc.nextLine());
for(String retval: line.split("#")){
if(countLine == 0){
productCode[count] = retval;
}else if(countLine == 1){
productPrice[count] = (int) Double.parseDouble(retval);
}
count++;


}
}
for(int i=0; i < arraySize; i++){
productList[i] = new Product(productCode[i],productPrice[i]);
}


Text File



10
PA/1234#PV/5732#Au/9271#DT/9489#HY/7195#ZR/7413#bT/4674#LR/4992#Xk/8536#kD/9767#
153#25#172#95#235#159#725#629#112#559#

Aucun commentaire:

Enregistrer un commentaire