我处于这样的情况:我想要取long UPC //12 digits
并提取数字1-6并将其存储为private final MANUFACTURER_ID
,并将数字7-11存储为private final ITEM_NUMBER
。我有一个代码块以相当丑陋和迂回的方式执行此任务,我确信有一个更简洁和正确的方法。
这是InventoryItem类中的相关代码,它具有UPC变量。关于从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!
}
答案 0 :(得分:1)
你可以在整数/长整数上做到:
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);
答案 1 :(得分:1)
简单的算术mod / div动作怎么样?
long manufacturerID = (long) UPC / 1000000L;
long itemNumber = (long) (UPC % 100000L) / 10;
简单的算术,没有昂贵的字符串操作,性能要好得多。它看起来也更优雅。
答案 2 :(得分:0)
Java
有一个名为String
的内置substring
方法。
String c = "abc".substring(2,3); // Output: b
答案 3 :(得分:0)
使用substring
类的String
方法。像
`String tempManufacturerID = str.substring(0,7);` //First 7 digits from index pos - 0 to pos - 6