在这种情况下,我想取一个long UPC //12 digits,提取数字1-6,并将它们存储为private final MANUFACTURER_ID,并将数字7-11存储为private final ITEM_NUMBER。我有一个代码块,它以一种非常丑陋和迂回的方式执行这个任务,我相信有一种更简洁和正确的方法来完成这个任务。
下面是包含UPC变量的InventoryItem类中的相关代码。对于从UPC中提取数字的部分,是否有一种不同的、更简洁的方法来做到这一点?
public InventoryItem(
long UPC,
Category category,
Unit_Of_Measure unitOfMeasure,
String binLocation,
String aisle,
String name,
String description,
short itemsPerUOM,
int unitsInStock,
int unitsLastReceived){
this.UPC = UPC;
this.category = category;
this.unitOfMeasure = unitOfMeasure;
this.binLocation = binLocation;
this.aisle = aisle;
this.name = name;
this.description = description;
this.itemsPerUOM = itemsPerUOM;
this.unitsInStock = unitsInStock;
this.unitsLastReceived = unitsLastReceived;
/* The code block below parses and extracts digits 1-6 of a UPC which is
* the unique manufacturer ID. This block also repeats the same process for
* digits 7-11 which are the manufacturer-specific Item Number. After these
* steps, the temp Strings are converted to their long integer values and
* used to initialize MANUFACTURER_ID and ITEM_NUMBER. The 12th digit of a
* UPC is used for checksum and thus is ignored for the scope of this program */
String str = Long.toString(UPC);
String tempManufacturerID = "";
String tempItemNumber = "";
char[] chArr = str.toCharArray();
//extract the manufacturer ID
for(int i = 0; i < 6; i++){
tempManufacturerID += String.valueOf(chArr[i]);
}
//extract the manufacturer's item number
for(int i = 6; i < 11; i++){
tempItemNumber += String.valueOf(chArr[i]);
}
// unbox and assign the extracted data to their respective variables
MANUFACTURER_ID = Integer.valueOf(tempManufacturerID);
ITEM_NUMBER = Integer.valueOf(tempItemNumber);
}。
。
解决方案
public InventoryItem(
long UPC,
Category category,
Unit_Of_Measure unitOfMeasure,
String binLocation,
String aisle,
String name,
String description,
short itemsPerUOM,
int unitsInStock,
int unitsLastReceived){
this.UPC = UPC;
this.category = category;
this.unitOfMeasure = unitOfMeasure;
this.binLocation = binLocation;
this.aisle = aisle;
this.name = name;
this.description = description;
this.itemsPerUOM = itemsPerUOM;
this.unitsInStock = unitsInStock;
this.unitsLastReceived = unitsLastReceived;
this.MANUFACTURER_ID = (int)(this.UPC / 1000000); // <--So much nicer!
this.ITEM_NUMBER = (int)((this.UPC % 1000000) / 10); // <--So much nicer!
}发布于 2014-03-09 10:36:18
简单的算术mod / div操作怎么样?
long manufacturerID = (long) UPC / 1000000L;
long itemNumber = (long) (UPC % 100000L) / 10;简单的算法,没有昂贵的字符串操作,在性能上要好得多。它看起来也优雅得多。
发布于 2014-03-09 10:28:19
您可以在整数/longs上这样做:
long longcode = 123456789012L;
long firstpart = longcode / 1000000L;
long secondpart = (longcode - 1000000L * firstpart) / 10L;在字符串上,可以使用substring()获取字符串部件:
String str = Long.toString(UPC);
String tempManufacturerID = str.substring(0,6);
String tempItemNumber = str.substring(6,11);发布于 2014-03-09 10:27:13
Java有一个内置的String方法,名为substring。
String c = "abc".substring(2,3); // Output: bhttps://stackoverflow.com/questions/22281038
复制相似问题