首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从较大的数字中提取一个较小的数字

从较大的数字中提取一个较小的数字
EN

Stack Overflow用户
提问于 2014-03-09 10:23:34
回答 4查看 77关注 0票数 0

在这种情况下,我想取一个long UPC //12 digits,提取数字1-6,并将它们存储为private final MANUFACTURER_ID,并将数字7-11存储为private final ITEM_NUMBER。我有一个代码块,它以一种非常丑陋和迂回的方式执行这个任务,我相信有一种更简洁和正确的方法来完成这个任务。

下面是包含UPC变量的InventoryItem类中的相关代码。对于从UPC中提取数字的部分,是否有一种不同的、更简洁的方法来做到这一点?

代码语言:javascript
复制
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); 

}

解决方案

代码语言:javascript
复制
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!

}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-03-09 10:36:18

简单的算术mod / div操作怎么样?

代码语言:javascript
复制
long manufacturerID = (long) UPC / 1000000L;
long itemNumber = (long) (UPC % 100000L) / 10;

简单的算法,没有昂贵的字符串操作,在性能上要好得多。它看起来也优雅得多。

票数 1
EN

Stack Overflow用户

发布于 2014-03-09 10:28:19

您可以在整数/longs上这样做:

代码语言:javascript
复制
long longcode = 123456789012L;
long firstpart = longcode / 1000000L;
long secondpart = (longcode - 1000000L * firstpart) / 10L;

在字符串上,可以使用substring()获取字符串部件:

代码语言:javascript
复制
String str = Long.toString(UPC);
String tempManufacturerID = str.substring(0,6);
String tempItemNumber = str.substring(6,11);
票数 1
EN

Stack Overflow用户

发布于 2014-03-09 10:27:13

Java有一个内置的String方法,名为substring

代码语言:javascript
复制
String c = "abc".substring(2,3); // Output: b
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22281038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档