Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.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 单独类中的方法-UPC值_Java_Class_Methods_Barcode - Fatal编程技术网

Java 单独类中的方法-UPC值

Java 单独类中的方法-UPC值,java,class,methods,barcode,Java,Class,Methods,Barcode,所以我遇到了一个问题。我正在设计一个UPC程序,它返回关于产品类型/manufactureretc的条形码编号的几个部分,并使用一个等式来检查最后一个数字。但我的问题是: 我有一个处理输入和输出的驱动程序类,还有一个我的所有方法都存储在其中的UPC类。我刚刚了解了调用方法和toString是如何工作的,对此我非常激动。但现在我的问题是,用户的UPC是如何根据分配要求输入的,我必须收集数据作为字符串的一部分,然后使用parseInt运行一段代码,将字符串分隔为多个整数位数,这样我就可以通过等式运行

所以我遇到了一个问题。我正在设计一个UPC程序,它返回关于产品类型/manufactureretc的条形码编号的几个部分,并使用一个等式来检查最后一个数字。但我的问题是:

我有一个处理输入和输出的驱动程序类,还有一个我的所有方法都存储在其中的UPC类。我刚刚了解了调用方法和toString是如何工作的,对此我非常激动。但现在我的问题是,用户的UPC是如何根据分配要求输入的,我必须收集数据作为字符串的一部分,然后使用parseInt运行一段代码,将字符串分隔为多个整数位数,这样我就可以通过等式运行它们。问题是,我是在driver类中这样做的,我不知道如何在不破坏代码的情况下继续进行

我曾尝试将使用parseInt的部分放在UPC类中,但它似乎不起作用。有没有办法把分离出来的变量从driver类带到UPC类,然后把它们输入到我要建立的方程中

以下是驾驶员等级: 导入java.util.Scanner

public class Driver  
{  

   public static void main (String[] args)  
   {  

   // Create a Scanner object  
      Scanner keyboard = new Scanner(System.in);  



//------------------------------Variables----------------------------------------------  

   // Declare variables for UPC code (Digits 1-12)  
      int d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12;  


   // Declare character variables for string to individual digit conversion (d2-d11)  
      char c2, c3, c4, c5, c6, c7, c8, c9, c10, c11;  

   // User's entered UPC (Input is d1, manufacturer, product and d12)  
      UPC userUPC;  



//-------------------------------User Input-------------------------------------------  

   // Prompt and get user input for all 12 digits of UPC code  
      System.out.println("Enter all twelve digits of your UPC code.");  
      System.out.println("Please enter in this format: * ***** ***** *.");  

      d1 = keyboard.nextInt();  

      String manuString = keyboard.next();  
      String prodString = keyboard.next();  

      d12 = keyboard.nextInt();  


//-------------------------------String to Int conversion-----------------------------  

   // Convert user input strings to digits (Int)  
      int manuInt = Integer.parseInt(manuString);  
      int prodInt = Integer.parseInt(prodString);  

      c2 = manuString.charAt(0);  
           d2 = Character.getNumericValue(c2);  
      c3 = manuString.charAt(1);  
           d3 = Character.getNumericValue(c3);  
      c4 = manuString.charAt(2);  
           d4 = Character.getNumericValue(c4);  
      c5 = manuString.charAt(3);  
           d5 = Character.getNumericValue(c5);  
      c6 = manuString.charAt(4);  
           d6 = Character.getNumericValue(c6);  

      c7 = prodString.charAt(0);  
           d7 = Character.getNumericValue(c7);  
      c8 = prodString.charAt(1);  
           d8 = Character.getNumericValue(c8);  
      c9 = prodString.charAt(2);  
           d9 = Character.getNumericValue(c9);  
      c10 = prodString.charAt(3);  
           d10 = Character.getNumericValue(c10);  
      c11 = prodString.charAt(4);  
           d11 = Character.getNumericValue(c11);  



//-----------------------------------UPC----------------------------------------------  

   // Create user UPC  
      userUPC = new UPC(d1, manuInt, prodInt, d12);  

      resultUPC = new UPC  


//----------------------------Program Output------------------------------------------  



   // Output data  
      System.out.println("The item's UPC value is: " + userUPC.toString());  
      System.out.println();  

      System.out.println("Product Type: " + userUPC.getItemType());  
      System.out.println("Manufacturer: " + userUPC.getManufacturer());  
      System.out.println("Product: " + userUPC.getProduct());  
      System.out.println();  

      System.out.println("Calculated Check Digit: " /*+ userUPC.getCalculatedCheck()*/);  
      System.out.println("UPC Check Digit: " + userUPC.getCheckDigit());  


   }  

}
这是我的UPC课程:

 public class UPC  
{  

// Instance variables  
   private int itemType;      // digit 1  

   private int manufacturer;  // digits 2,3,4,5,6  
   private int product;       // digits 7,8,9,10,11  

   private int checkDigit;    // digit 12  

// Constructer method: Initialize instance variables  
   public UPC(int item, int man, int prod, int check)   
   {  
      itemType = item;  
      manufacturer = man;  
      product = prod;  
      checkDigit = check;  
   }  

// Return the UPC code with "-" inserted between the different secions of the barcode  
   public String toString()   
   {  
      return itemType + "-" + manufacturer + "-" + product + "-" + checkDigit;  
   }  

// Return the UPC's item type (First digit)  
   public int getItemType()   
   {  
      return itemType;  
   }  

// Return the manufacturer code (Digits 2-6)  
   public int getManufacturer()   
   {  
      manufacturer = man  
      return manufacturer;  
   }  

// Return the product code (Digits 7-11)  
   public int getProduct()   
   {  
      return product;  
   }  

// Return the check digit (Digit 12)  
   public int getCheckDigit()   
   {  
      return checkDigit;  
   }  

/* Calculate and return the calculated check digit (Equation) 
   public int getCalculatedCheckDigit()  
   { 

   } */  



}  

在我测试的代码中可能有一些无用的代码,但除此之外,所有的工作都完成了。提前谢谢,我很抱歉有这么多文字

我认为UPC类中应该有2个构造函数。一个接受字符串,另一个接受int item、int man、int prod、int check

接受字符串的构造函数将使用扫描器从中获取数字,并使用这些数字调用另一个构造函数。这样,所有内容都将封装在UPC类中


import java.util.Scanner;


public class Driver {

    public static void main(String[] args) {
        // Create a new scanner to help you get information from the input
        // stream.
        Scanner scanner = new Scanner(System.in);

        // Prompt and get user input for all 12 digits of UPC code
        System.out.println("Enter all twelve digits of your UPC code.");
        System.out.println("Please enter in this format: * ***** ***** *.");

        // get the next input
        String input = scanner.nextLine();

        Upc userUpc = new Upc(input);

        // Output data
        System.out.println("The item's UPC value is: " + userUpc.toString());
        System.out.println();

        System.out.println("Product Type: " + userUpc.getItemType());
        System.out.println("Manufacturer: " + userUpc.getManufacturer());
        System.out.println("Product: " + userUpc.getProduct());
        System.out.println();

        System.out.println("Calculated Check Digit: " //+ userUPC.getCalculatedCheck()
                );
        System.out.println("UPC Check Digit: " + userUpc.getCheckDigit());

        scanner.close();
    }

}

class Upc {

    // Instance variables
    private int itemType; // digit 1

    private int manufacturer; // digits 2,3,4,5,6
    private int product; // digits 7,8,9,10,11

    private int checkDigit; // digit 12

    // constructor that accepts String
    public Upc(String upcString) {
        //Scanner for the given input that will help us get data from it
        Scanner scanner = new Scanner(upcString);

        itemType = scanner.nextInt(); //get item type
        manufacturer = scanner.nextInt(); //get manufactor
        product = scanner.nextInt(); //get product
        checkDigit = scanner.nextInt(); //get chackDigit

        setVariables(itemType, manufacturer, product, checkDigit);

        scanner.close();
    }

    public Upc(int item, int man, int prod, int check)
    {
        setVariables(item, man, prod, check);
    }

    private void setVariables(int item, int man, int prod, int check)
    {
        this.itemType = item;
        this.manufacturer = man;
        this.product = prod;
        this.checkDigit = check;
    }

    // Return the UPC code with "-" inserted between the different secions of
    // the barcode
    public String toString() {
        return itemType + "-" + manufacturer + "-" + product + "-" + checkDigit;
    }

    // Return the UPC's item type (First digit)
    public int getItemType() {
        return itemType;
    }

    // Return the manufacturer code (Digits 2-6)
    public int getManufacturer() {
        return manufacturer;
    }

    // Return the product code (Digits 7-11)
    public int getProduct() {
        return product;
    }

    // Return the check digit (Digit 12)
    public int getCheckDigit() {
        return checkDigit;
    }

    // Calculate and return the calculated check digit (Equation)
    // public int getCalculatedCheckDigit() {
    //
    // }

}




你有什么错误?你试图做的事情的真正问题是什么?它现在的工作方式很好,但我需要在UPC类文件中使用d2-d11整数,当我试图将用户输入从字符串更改为单个int变量的代码部分移到UPC类中时,它只会说,对于每个字符串到int的转换,它只会给我一些小错误。我现在要问的是,当变量d2-d11在驱动程序类中时,是否有办法在UPC类中使用它们?啊,我不知道我可以有多个构造函数。我是否只需要使用不同的名称创建一个构造函数,可能是名为public UPC2或其他名称?对不起,如果这个问题有点愚蠢,我现在正在弄清楚所有的方法是如何工作的,我会试着通读一遍,看看它是否能解决问题,多亏了你们两个。你们可以有多个构造函数,但你们只能在第一条指令中调用另一个构造函数,所以诀窍是创建一个私有方法,让两个构造函数都使用。看代码:明白了!使另一个构造函数工作完美,返回我需要的所有结果。非常感谢你的帮助,我今天学到了很多