2013-10-06 20 views
0

,所以我正在编写使用封装(我是个TAFE学生)一些已经工作的代码和方法是在第一梅索德越来越但不是在第二Java的getter方法第一个GET后获得,但不是第二

首先了Methode:

public void getDetails() throws IOException 
{ 
    do 
    { 
     String fullName = JOptionPane.showInputDialog(null, "Enter your full name", "Input Request", JOptionPane.QUESTION_MESSAGE); 
     f = true; 

     if (fullName == null) 
     { 
      System.out.println("Program has been aborted!"); 
      System.exit(0); 
     } else 
     { 
      f = true; 
     } 


     Pattern numbers = Pattern.compile("[1-9,[email protected]#$%^&*()_+=]"); 
     Matcher match = numbers.matcher(fullName); 

     if (match.find()) 
     { 
      JOptionPane.showMessageDialog(null, "Incorrect!\nLetters Only", "Error", JOptionPane.ERROR_MESSAGE); 
      f = false; 
     } else if (fullName.isEmpty()) 
     { 
      JOptionPane.showMessageDialog(null, "pelase enter a name!", "Error", JOptionPane.ERROR_MESSAGE); 
      f = false; 
     } else 
     { 
      f = true; 
      FullName fn = new FullName(); 
      fn.setFullName(fullName); 
      System.out.println(fn.getFullName()); 
     } 
    } while (f == false); 

} 

第二种方法:

public void print() 
{ 
    DecimalFormat df, df1; 
    df = new DecimalFormat("0.00"); 


    FullName fn = new FullName(); 
    System.out.println(fn.getFullName()); 
    JOptionPane.showMessageDialog(null, fn.getFullName()+ "\nYour tax outcome is: " + "\nIncome Tax $" + incomeTax + "\nMedicare Levy $" + df.format(medicareLevy) 
      + "\nTax Paid $" + taxWitheld + sReturn + "\nActual Tax Rate " + df.format(actualTaxRate) + "%"); 



} 

setter和getter类:

public class FullName 
{ 

    private String fullName; 

    public FullName() 
    { 
     fullName = null; 
    } 

    public FullName(String n1) 
    { 
     fullName = n1; 
    } 

    public String getFullName() 
    { 
     return fullName; 
    } 

    public void setFullName(String name) 
    { 
     this.fullName = name; 
    } 
} 

你可能会看到的任何东西将是惊人的

+0

是为了显示'getDetails()'中给出的名称(在'print()')中? – kiheru

+0

它只是我的测试方法,我想从获取详细信息中获取名称并在最后显示它,我还有其他4种方法,但我还没有将其更改为getter setter方法 – Callum

+1

当我不知道什么时候很难提供帮助正是你想要实现的。在print()中''fn.getFullName()''返回'null',因为对于那个FullName实例*没有调用setFullName()。你可能会想'getDetails()'返回'FullName',否则那个方法的本地实例。 – kiheru

回答

0

我认为你的问题是在第二个方法,你的“新”的全名类,但你永远不setFullName(“等等等等”);所以fn.getFullName()返回null。

+0

但我在getDetails()方法setFullName? – Callum

+0

是的,你是在最后的“其他”你setFullName –

相关问题