Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.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 尝试生成对象时出错-构造函数类_Java_String_Exception_Int_Containers - Fatal编程技术网

Java 尝试生成对象时出错-构造函数类

Java 尝试生成对象时出错-构造函数类,java,string,exception,int,containers,Java,String,Exception,Int,Containers,我在编译代码时遇到问题 问题似乎是当我尝试从主类中的Customer类生成对象时。CareHire主类中的代码行是: newCust[i]=new Customer(); 我输入一个字符串和Int,并对数组中的每个条目进行编号。这是我计划在每个实例中使用的线路,尽管它显然不正确,我不确定如何继续 当我编辑这一行时,我将构建。单击“回车”按钮仅显示字符串,而不显示int值。我应该在这里做什么 主要类别:凯瑞 package CareHire; import java.awt.*; import

我在编译代码时遇到问题

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

newCust[i]=new Customer();
我输入一个字符串和Int,并对数组中的每个条目进行编号。这是我计划在每个实例中使用的线路,尽管它显然不正确,我不确定如何继续

当我编辑这一行时,我将构建。单击“回车”按钮仅显示字符串,而不显示int值。我应该在这里做什么

主要类别:凯瑞

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);
    }
}

此语法错误
s[i]=String new custName()

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

“s”声明为客户数组
私人客户s[]=新客户[20]


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

s[i]=int new custName()
也不会编译。请改进您的标题并添加一个问题,因为目前还不清楚您在问什么。对不起,我在调查问题时一直在努力使其易读。我想问题可能是我不确定我应该问什么问题。这把我难住了。我用一段连环提问更新了正文。
 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;
}
}