-2

我需要为输入运算符名称返回 finalString 值。其中,internalPrestring 是针对特定运营商固定的,internalDigit 将从获取运营商名称中检索。然后所有这些都将添加到 finalString。但它给出了null,我无法理解这个问题

 import java.io.*;
 import java.lang.*;

 class CallManager
 {


   public static final String postString = "#";

   StringBuilder stringBuilder;

   String internalPreString;
   String preString;
   String middleString;
   String finalString;
   String operatorName;


   int internalDigit;



   //needs to set oprator name
   public void setOperatorName( String getMeFromPreferences)
   {

    operatorName = getMeFromPreferences;
        System.out.println("I got it " + operatorName);
   }


//afeter having operator name need to set inrernal digit for each operator
public void setOperatorBasedInternalDigit(int getIntegerForOperator)
    {
        internalDigit = getIntegerForOperator;
        System.out.println("I got it too " + internalDigit);
    }

//it needs to get string from ocr  
public void setString( String getMeFromOCR )
    {
        middleString = getMeFromOCR;
    }


//preString creator for differnet operator
public String getOperatorBasedPreString(String operatorName)
{
        if(operatorName.equals("Airtel"))
        internalPreString = "787";

        else if(operatorName.equals("Banglalink"))
        internalPreString = "123";

        else if(operatorName.equals("Grameen"))
        internalPreString = "555";

        else if(operatorName.equals("Robi"))
        internalPreString = "111";

        else if(operatorName.equals("TeleTalk"))
        internalPreString = "151";


        stringBuilder.append("*").append(internalPreString).append("*");
        preString = stringBuilder.toString();

        return preString;

}

//get operator name and retrive midlle string's digit size from it
public int getOperatorBasedInternalDigit( String operatorName)
{

        if(operatorName.matches("^Airtel | Grameen | Robi$"))
        internalDigit = 16; 

        else if(operatorName.matches("^Banglalink$"))
        internalDigit = 14;

        else if(operatorName.matches("^TeleTalk$"))
        internalDigit = 13;


        return internalDigit;
}

//check operator-based digit number with input middle string as a number then retrive final string
public String getString( String toBeInserted, int inetrnalDigit)
    {

        if(toBeInserted.length() == internalDigit)
        {

            int counter = 0;
            char [] insertHere  = new char[internalDigit];

            for(int verifier = 0; verifier < internalDigit; verifier ++)
            {
                insertHere[verifier] = toBeInserted.charAt(verifier);
                    if(!Character.isDigit(insertHere[verifier]))
                    break;

            counter ++;
            }

            if(counter == internalDigit)
            {

                    stringBuilder.append(preString).append(toBeInserted).append(postString);
                    finalString = stringBuilder.toString();
                    //to see what i've got finally as input for using this call manager method.it would be removed too
                    System.out.println(finalString);
                    return finalString;
            }

            else
            {
                //this printing could be used in main program
                System.out.println("number is less or more than desired ..... INVALID SCAN");
                System.out.println(middleString);
                //here i will call the method for scan the card again
                //
                //
                   return middleString;
             }
          }


          else
              {
                //this printing could be used in main program
                System.out.println("number is less or more than desired ..... INVALID SCAN");
                System.out.println(middleString);
                //here i will call the method for scan the card again
                //
                //
                return middleString;
              }



    }


}


//tester class that CallManager works rightly or not
class CallManagerDemo
{
    public static void main(String args[]) throws IOException
    {
            BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));



          System.out.println("Enter name of Operator");
          CallManager clm = new CallManager();

          clm.setOperatorName("Banglalink");
          System.out.println(clm.internalPreString);
          System.out.println(clm.preString);

    }
}
4

3 回答 3

3

您只有四行处理您的CallManager课程:

CallManager clm = new CallManager();
clm.setOperatorName("Banglalink");
System.out.println(clm.internalPreString);
System.out.println(clm.preString);

你得到 null 的原因:

  1. 您正在使用默认构造函数,并且在其中完成了处理。所以这不是问题
  2. 现在在下一行调用setOperator具有以下代码的方法:

    public void setOperatorName( String getMeFromPreferences)  
    {  
       operatorName = getMeFromPreferences;  
       System.out.println("I got it " + operatorName);  
    }
    

    现在在这里你只设置 thw 变量operatorName,没有别的。因此,所有其他变量都为空,因为您没有进行任何处理或将它们初始化为某些东西的东西。

  3. 因此,当您打印clm.internalPreStringclm.preString获得 null 时,因为它们未初始化。但是尝试打印clm.operatorName,它将打印您传递并在您的方法中初始化的操作员名称setOperatorName

因此,当您在类中定义了这么多方法时,请使用它们以便根据您的逻辑设置所有变量

更新

public void setOperatorName( String getMeFromPreferences)  
{  
     operatorName = getMeFromPreferences;  

     //call any methods for example and use the values returned from the method by storing it inside a variable 
     String mystring = getOperatorBasedPreString(String operatorName) 
}
于 2012-11-22T05:22:59.600 回答
0

你不认为你应该调用任何函数而不是使用对象的字符串变量。

你只是调用一个函数

public void setOperatorName(String getMeFromPreferences) {

        operatorName = getMeFromPreferences;
        System.out.println("I got it " + operatorName);
    }

您正在调用没有任何变量设置的默认构造函数,您没有初始化任何正在调用表单对象的字符串。

我认为您应该调用任何函数,例如

public int getOperatorBasedInternalDigit(String operatorName)

或者

public String getString(String toBeInserted, int inetrnalDigit) 

然后你会得到一些你所期望的字符串......希望这会对你有所帮助。

于 2012-11-22T05:23:11.940 回答
0

您从未为获得 NULL 值的变量设置值。术语 get/set 必须在直接访问属性的情况下使用。请阅读Java Programming Style GuideLines以获得更清晰的信息。使用适当的 getter 和 setter 来获取和像您为 operatorName 所做的那样设置值。

于 2012-11-22T05:24:35.853 回答