Reputation:
I am in a situation where I want to take a long UPC //12 digits
and extract digits 1-6 and store them as private final MANUFACTURER_ID
and also digits 7-11 to store as private final ITEM_NUMBER
. I have a block of code that does this task in a rather ugly and roundabout manner, and I'm sure there's a more concise and correct way to do so.
Here is the relevant code in the InventoryItem class, which has the UPC variable. Is there a different, more concise way to do this, regarding the section which extracts numbers from 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!
}
Upvotes: 0
Views: 124
Reputation: 2112
How about a simple arithmetic mod / div action?
long manufacturerID = (long) UPC / 1000000L;
long itemNumber = (long) (UPC % 100000L) / 10;
Simple arithmetic without costly string operations is much better performance wise. It also looks far more elegant.
Upvotes: 1
Reputation: 43023
You can do it on integers/longs:
long longcode = 123456789012L;
long firstpart = longcode / 1000000L;
long secondpart = (longcode - 1000000L * firstpart) / 10L;
On strings, you can use substring()
to get the string parts:
String str = Long.toString(UPC);
String tempManufacturerID = str.substring(0,6);
String tempItemNumber = str.substring(6,11);
Upvotes: 1
Reputation: 3456
use substring
method of String
class. like
`String tempManufacturerID = str.substring(0,7);` //First 7 digits from index pos - 0 to pos - 6
Upvotes: 0
Reputation: 12196
Java
has a String
built in method called substring
.
String c = "abc".substring(2,3); // Output: b
Upvotes: 0