Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从较大的数字中提取较小的数字_Java - Fatal编程技术网

Java 从较大的数字中提取较小的数字

Java 从较大的数字中提取较小的数字,java,Java,我所处的情况是,我想取一个长UPC//12位,提取数字1-6,并将其存储为私人最终制造商ID,以及将数字7-11存储为私人最终项目编号。我有一个代码块,它以一种相当丑陋和迂回的方式完成这项任务,我相信有一种更简洁和正确的方法来完成这项任务 下面是InventoryItem类中的相关代码,该类具有UPC变量。对于从UPC中提取数字的部分,是否有一种不同的、更简洁的方法来实现这一点 public InventoryItem( long UPC, Category c

我所处的情况是,我想取一个
长UPC//12位
,提取数字1-6,并将其存储为
私人最终制造商ID
,以及将数字7-11存储为
私人最终项目编号
。我有一个代码块,它以一种相当丑陋和迂回的方式完成这项任务,我相信有一种更简洁和正确的方法来完成这项任务

下面是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 inventory项目(
长期UPC,
类别,
测量单位测量单位,
字符串位置,
细绳过道,
字符串名,
字符串描述,
短篇文章,
国际单位股票,
int unitsLastReceived){
this.UPC=UPC;
this.category=类别;
this.unitOfMeasure=测量单位;
this.binLocation=binLocation;
这个。过道=过道;
this.name=名称;
this.description=描述;
this.itemsPerUOM=itemsPerUOM;
this.unitsInStock=unitsInStock;
this.unitsLastReceived=unitsLastReceived;
/*下面的代码块解析并提取UPC的数字1-6,它是
*唯一的制造商ID。此块还重复相同的过程
*数字7-11是制造商特定的项目编号。在这些数字之后
*步骤中,临时字符串将转换为其长整数值,然后
*用于初始化制造商ID和项目编号。a的第12位数字
*UPC用于校验和,因此在本程序范围内被忽略*/
字符串str=Long.toString(UPC);
字符串tempManufacturerID=“”;
字符串tempItemNumber=“”;
char[]chArr=str.toCharArray();
//提取制造商ID
对于(int i=0;i<6;i++){
tempManufacturerID+=String.valueOf(chArr[i]);
}
//提取制造商的项目编号
对于(int i=6;i<11;i++){
tempItemNumber+=String.valueOf(chArr[i]);
}
//取消装箱并将提取的数据分配给各自的变量
制造商ID=整数.valueOf(tempManufacturerID);
ITEM_NUMBER=整数.valueOf(tempItemNumber);
}

解决方案
public inventory项目(
长期UPC,
类别,
测量单位测量单位,
字符串位置,
细绳过道,
字符串名,
字符串描述,
短篇文章,
国际单位股票,
int unitsLastReceived){
this.UPC=UPC;
this.category=类别;
this.unitOfMeasure=测量单位;
this.binLocation=binLocation;
这个。过道=过道;
this.name=名称;
this.description=描述;
this.itemsPerUOM=itemsPerUOM;
this.unitsInStock=unitsInStock;
this.unitsLastReceived=unitsLastReceived;

this.MANUFACTURER_ID=(int)(this.UPC/1000000);//
Java
有一个名为
子字符串的
内置方法

String c = "abc".substring(2,3); // Output: b

使用
String
类的
substring
方法

 `String tempManufacturerID = str.substring(0,7);`  //First 7 digits from index pos - 0 to pos - 6

您可以在整数/长数上执行此操作:

long longcode = 123456789012L;
long firstpart = longcode / 1000000L;
long secondpart = (longcode - 1000000L * firstpart) / 10L;
在字符串上,可以使用获取字符串部分:

String str = Long.toString(UPC);
String tempManufacturerID = str.substring(0,6);
String tempItemNumber = str.substring(6,11);

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

long manufacturerID = (long) UPC / 1000000L;
long itemNumber = (long) (UPC % 100000L) / 10;

没有昂贵的字符串操作的简单算法性能更好。它看起来也更优雅。

子字符串
不包括结束索引。字符串索引从0开始。正确的方法是
“abc”。子字符串(1,3)
。这将使用索引1和2,所以第二个和第三个字母没有转换成字符串的方法吗?UPC、制造商ID和项目编号都是整数,所以我想知道转换成字符串只是子字符串并直接转换回整数类型是否不好。应该是
字符串tempManufacturerID=str.substring(0,7);
。substring方法现在返回索引0到5。tempItemNumber是索引6-10,因此它应该是
str.substring(6,11)
@MaxMeijer,我想(0,6)可以,因为其数字1-6(含)表示制造商ID,7-11(含)表示项目编号,所以当从0索引系统引用时,我需要索引0,1,2,3,4,5和6,7,8,9,10respectively@Justin我更新了我的答案,以显示如何使用数字。现在更新后,第二个数字只有5位。@MaxMeijer Sorry、 没注意到第二个数字只有5位。你抢先回答了我自己的问题。呵呵。我刚发完帖子,我就想到了使用模。如此多的压缩和可读性,我甚至不需要在构造函数中使用它!模应该是1000000L才能得到前6位。修复它,这将是一个很好的答案。还有除法。我得到了预期前6位(制造商ID)的ed结果为
UPC/1000000
,7-11位(项目编号)的ed结果为
(UPC%1000000)/10
@AndrzejBobak在进一步检查后,我发现有一个巨大的错误,因为UPC可以从0开始。如果第一个数字是零,那么一切都将毫无希望地被破坏。您是否建议添加最左边的1作为占位符并修改计算,或者是否也会有可能的错误?最小的12位UPC可能是000001000010.到目前为止,我有一个解决办法,我添加最左边的1,然后修改或分割更多,以逐步淘汰制造商ID中的1,但由于UPC是最终的,这迫使我的UPC有13位数字,而不是标准化的12位,数据不再是产品UPC的准确表示。这是7个字符。受您的评论启发,我建议:将UPC作为字符串发送,然后将其裁剪为长值:例如Long.parseLong(“000001000010”)。由于UPC代码中的前导零可以多达5个,因此这个小补丁应该可以帮助您处理常见情况。
long manufacturerID = (long) UPC / 1000000L;
long itemNumber = (long) (UPC % 100000L) / 10;