需要使用get()和set()方法拆分字符串

时间:2013-12-10 23:47:19

标签: java string

我必须为此代码使用getter和setter 实际上我正在使用两个类来获得结果

这里是Ndc类:

package java4u.com;

public class Ndc {


    private String ndcQUAL;
    private String ndcCODE;
    private String ndcUNIT;
    private String ndcQTY;
     String str;
    public String getStr() {
        return str;
    }
    public void setStr(String str) {
        this.str = str;
    }
    public String getndcQUAL() {

        if(str.contains("N4"))
        {
            return "N4";
        }
        else
        {

        return "";
        }
    }
    public void setndcQUAL(String getndcQUAL) {

        this.ndcQUAL = getndcQUAL;
    }
    public String getndcCODE() {
        if(str.contains("N4")){
            int i=str.indexOf("N4");
            str=str.substring(i+2,i+13);
            return str;
        }
        else
        {
            return "";
        }

    }
    public void setndcCODE(String getndcCODE) {
        this.ndcCODE = getndcCODE;
    }
    public String getndcUNIT() {
        if(str.contains("N4")) {
            str=str.substring(i+13,i+15);
            return str;
        }else
        {
            return "";
        }

    }
    public void setndcUNIT(String getndcUNIT) {
        this.ndcUNIT = getndcUNIT;
    }
    public String getndcQTY() {
        if(str.contains("N4")) {
            do {
                int i=str.indexOf(getndcUNIT());
                str=str.substring(i,i++);
                return str;

            } while(str.length()<=35 || str.contains("N4") || str.contains("TPL"));

        else
        {
            return "";
        }


    }
    public void setndcQTY(String getndcQTY) {
        this.ndcQTY = getndcQTY;
    }




}

这里我使用str变量,字符串将在运行时输入,条件是如果字符串包含“N4”值,则循环应该继续,否则返回空格。 我在这个程序中有四种方法  如果string包含“N4”值,则getNdcQUAL()方法应返回“N4” 对于这种情况,getNdcCODE()方法应该显示“N4”之后的下一个11位数我不应该提到str.substring(2,13)..我应该找到NdcQUAL的位置并从那里到下一个11位数将被打印.. 和getNdcUNIT()方法应该在这个案例的11位数之后显示接下来的两个字节限定符我也应该找到NdcCODE的位置并从那里到2位数 最后getNdcQTY()方法应该在NdcUNIT之后返回数据,对于这种情况我也应该找到NdcUNIT的位置并从那里直到满足其中一个条件

这是我的主要课程

package java4u.com;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor.GetterSetterReflection;

public class Test {
    public static String getStr(String str)
    {
        return str;
    }
    public static void main(String args[]) {
        Ndc ndc=new Ndc();
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        try {
            System.out.println("enter a string:");
            br.readLine();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } 


}

无法理解如何将字符串值从Ndc.java传递给Test.java也无法获取如何将其他方法从Ndc.java传递给Test.java

这是示例输出 海峡= N412345678923UN2345.677 它应该回来 N4 12345678923 联合国 2345.67

请帮帮我!!!!!!

3 个答案:

答案 0 :(得分:0)

因为你没有构造函数。您需要手动设置str

如果此N412345678923UN2345.677br.readLine()。然后,您需要在NDC对象中设置它

String str = br.readLine();
ndc.setStr(str);               // now the str is set in your ndc object.

System.out.println(ndc.getndcCODE());
System.out.println(ndc.getndcUNIT());
System.out.println(ndc.getndcCQTY());

答案 1 :(得分:0)

你应该首先传递这样的字符串:

ndc.setndcQUAL(yourString);

然后获得所需的值:

System.out.print(ndc.getndcQUAL());

答案 2 :(得分:0)

您的方法有一个主要缺陷 - 您需要以预定义的顺序执行方法,否则它将提取错误的数据。但是,您可以使用setStr(String str)方法初始化所有正确的字段,然后使用getter方法返回您在setStr(...)方法中设置的值:

public class Ndc 
{
    private String ndcQUAL;
    private String ndcCODE;
    private String ndcUNIT;
    private String ndcQTY;

    public void setStr(String str) 
    {
        int pos = 0;

        if (str.contains("N4"))
        {
            pos = str.indexOf("N4");
            this.ndcQUAL = str.substring(pos, pos+=2);
            this.ndcCODE = str.substring(pos, pos+=11);
            this.ndcUNIT = str.substring(pos, pos+=2);
            String data = str.substring(pos);

            // trim the data at the end corresponding to the provided class logic
            int p = data.length();
            if (data.contains("N4"))
            {
                p = data.indexOf("N4");
            }
            else if (data.contains("TLP"))
            {
                p = data.indexOf("TLP");
            }

            if (p > 35)
                p = 35;

            this.ndcQTY = data.substring(0, p);
        }
        else
            this.ndcQUAL = "";
    }

    public String getndcQUAL() 
    {
        return this.ndcQUAL;
    }

    public String getndcCODE() 
    {
        return this.ndcCODE;
    }

    public String getndcUNIT() 
    {
        return this.ndcUNIT;
    }

    public String getndcQTY() 
    {
        return this.ndcQTY;
    }
}

如果没有输入有效的N4字符串,要打破循环,首先必须首先定义一种循环,然后检查getndcQUAL()返回值是否等于N4之后的'{1}} ve将输入字符串分配给setStr(...)

public class Test 
{
    public static void main(String args[]) 
    {
        Ndc ndc=new Ndc();
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        try 
        {
            do
            {
                System.out.println("enter a string:");
                ndc.setStr(br.readLine());

                System.out.println("QUAL: "+ndc.getndcQUAL());
                System.out.println("CODE: "+ndc.getndcCODE());
                System.out.println("UNIT: "+ndc.getndcUNIT());
                System.out.println("QTY: "+ndc.getndcQTY());
            }
            while("N4".equals(ndc.getndcQUAL()));
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    } 
}