【问题标题】:Error whilst attempting to generate Objects - Constructor classes尝试生成对象时出错 - 构造函数类
【发布时间】:2016-09-30 12:21:53
【问题描述】:

我在编译代码时遇到问题。

问题似乎是当我尝试从主类中的客户类生成对象时。主类 CareHire 中的代码行是:

newCust[i]=new Customer();

我正在输入一个字符串和整数,并对数组中的每个条目进行编号。这是我计划为每个实例使用的行,尽管它显然不正确,我不确定如何进行。

当我编辑这条线时,我要建造。单击“Enter”按钮似乎只显示字符串而不显示 int 值。我应该在这里做什么?

主要课程:CareHire

package CareHire;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Scanner;


public class CareHire extends JFrame implements ActionListener
{
private JLabel nameLabel=new JLabel("Name");
private JLabel daysLabel=new JLabel("Days of Hire");
private JTextField nameField=new JTextField(16);
    private JTextField daysField=new JTextField(11);
    private JButton enterButton=new JButton("Enter");
private JButton displayButton=new JButton("Display");
    private JButton searchButton= new JButton("Search");
    private JButton displayStatButton=new JButton("Statistics");
    private JButton exitButton=new JButton("Exit");
    private JTextArea textArea=new JTextArea(16,35);
    private Customer newCust[]=new Customer[20]; 
    int numProcessed=0;
    private static final int FRAME_WIDTH = 480;
    private static final int FRAME_HEIGHT = 430;

//This is the problematic section

public CareHire() {

       super("  Care Hire System   ");
       setLayout(new FlowLayout());  

//Issues appears to be with this For Loop

       for(int i=0;i<20;i++)  {
       newCust[i]=new Customer();
    }

//End there. Remainder of code is provided for context.

       add(nameLabel);              
       add(nameField);
       add(daysLabel);
       add(daysField);

       add(enterButton);
       add(displayButton);
       add(searchButton);
       add(displayStatButton);
       add(exitButton);

       add(textArea);
       enterButton.addActionListener(this);  
       displayButton.addActionListener(this);
       searchButton.addActionListener(this);
       displayStatButton.addActionListener(this);
       exitButton.addActionListener(this);
    }
}

    public void processInput()
    {
    textArea.setText(nameField.getText());
    newCust[numProcessed].setDoH(Integer.parseInt(daysField.getText()));               
    String title="Customer   Days of Hire ($)\n\n";
    textArea.setText(title+newCust[numProcessed].getName()+ "\t $"+newCust[numProcessed].getDoH()+"\t "+" ");               
    nameField.setText("");                
    daysField.setText("");                
    numProcessed++;
}

    public void display()       {
                if(numProcessed==0) {    //no data entered             
        JOptionPane.showMessageDialog(null,"Need Customers","Display All",JOptionPane.ERROR_MESSAGE);         
        return;       
        }    
        String allItems="";    
        for(int i=0;i<numProcessed;i++)    {     
            allItems=allItems+(i+1)+".\t"+newCust[i].getName()+ "\t\t $"+newCust[i].getDoH()+"\t "+"   "+"\n";}    

        textArea.setText("No.\t Customer \t Days of Hire \n\n"+allItems); 
    }

    public void search()
    {

    }



       public void statistics()     {

if(numProcessed==0){ //no data entered                
JOptionPane.showMessageDialog(null,"Need Customers", "Find Max",JOptionPane.ERROR_MESSAGE);          
return;
    }

String maxName=null;
int maxAmount=-10000;


for(int i=0;i<numProcessed;i++) {
if(newCust[i].getDoH()>maxAmount){
maxAmount=newCust[i].getDoH();
maxName=newCust[i].getName();
}
}
}

    public static void main(String[] args)
    {
       JFrame frame = new CareHire();
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
       frame.setVisible(true);
       frame.setResizable(false);
    }
}

容器类:客户

 package CareHire;

public class Customer {

private String name;   
private int doh;

public Customer(String custName, int totalDays)    {       
name = custName;
doh = totalDays;
}

public void setName(String custName )  {
name = custName;
}

public String getName()   {
return name;
}

public void setDoH(int totalDays)    {
doh = totalDays;
}

public int getDoH() {
return doh;
}
}

【问题讨论】:

  • s[i]=int new custName (); 也不会编译。
  • 请改进你的标题并添加一个问题,因为目前还不清楚你在问什么。
  • 抱歉,我在自己调查该问题时一直在努力使这一点清晰易读。我认为问题可能是我不确定我应该问什么问题。这让我很难过。我已经用一段挥之不去的问题更新了正文。

标签: java string exception int containers


【解决方案1】:

这个语法错误s[i]=String new custName ();

应该是s[i] = new Customer();

“s”被声明为一个客户数组private Customer s[]=new Customer[20]

我假设 Cusotmer 有一个构造函数或一些方法来添加名称和雇用日期数据。您需要为我分享更多代码才能准确了解如何填充对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 2017-08-28
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多