so I am working on a java project that will order elements. However, in my code, which is supposed to iterate over some tokenized terms and assign them to values ββin the custom Items class, it doesn't seem to work.
The code:
public void tokenizeTerms(String content) {
String[] tokenizedTerms = content.split(" ");
Item[] itemArray = new Item[tokenizedTerms.length/3];
Item fillItem = new Item();
fillItem.setName("fillItem");
fillItem.setPrice(0.00);
fillItem.setQuantity(1);
Arrays.fill(itemArray, fillItem);
int currToken = 0;
for(int i = 0; i < itemArray.length; i++) {
itemArray[i].setName(tokenizedTerms[currToken]);
currToken++;
try {
int foo = Integer.parseInt(tokenizedTerms[currToken]);
itemArray[i].setQuantity(foo);
currToken++;
double moo = Double.parseDouble(tokenizedTerms[currToken]);
itemArray[i].setPrice(moo);
currToken++;
} catch (Exception e) {
System.out.println("Error parsing data.");
}
}
this.items = itemArray;
}
Item Class:
public class Item {
private String name;
private int quantity;
private double price;
public void setName (String name) {
this.name = name;
}
public String getName () {
return this.name;
}
public void setQuantity (int quantity) {
this.quantity = quantity;
}
public int getQuantity () {
return this.quantity;
}
public void setPrice (double price) {
this.price = price;
}
public double getPrice () {
return this.price;
}
}
When I launch the terminology tokenization method and print the values ββof each item in itemArray, I get a set of items that look like this. Name: Book Number: 14 Price: 856.89
Name: Book Number: 14 Price: 856.89
Name: Book Number: 14 Price: 856.89
However, I know that this should not happen, as it String[] tokenizedTermslooks like this:
[CD, 32, 459.2, T-Shirt, 22, 650.8, Book, 14, 856.89]