You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java含String参数的重载构造器问题及对象输入校验咨询

Hey there! Let's tackle your two Java issues one by one, with clear fixes and explanations.

1. 正确获取用户输入并校验,创建对象打印

Your main issues here are undeclared variables in Object2 and an input flow that doesn't properly create and print the Object1 instance. The good news is your Object1 class already has built-in validation logic—we just need to hook up the input correctly.

2. 修复重载构造器的符号缺失错误

The "symbol missing" error comes from three key mistakes:

  • Using variables like fileName in Object2 without declaring them first
  • Misusing the Object1(String f) constructor (it shouldn't handle user input—constructors are for initializing objects, not interacting with users)
  • Not properly initializing all Object1 fields when using the string constructor

修正后的完整代码

Updated Object1 Class

We'll clean up the string constructor and fix a minor bug in the name validation:

public class Object1 { 
    // Attributes 
    private double yyy; 
    private int zzz; 
    private String xxx; 
    private String fileName; 

    // Symbolic constants 
    private final double MINDIAMETER = 1; 
    private final int MINTEMP = 300; 
    private final String NA = "N/A"; 

    // Getter/Setter for name (fixed null/empty check)
    public void setName(String n) { 
        if (n != null && !n.trim().isEmpty()) {
            xxx = n; 
        } else { 
            xxx = NA; 
        } 
    } 
    public String getName() { return xxx; } 

    // Getter/Setter for diameter (already handles validation)
    public void setDiameter(double d) { 
        yyy = (d >= MINDIAMETER) ? d : MINDIAMETER; 
    } 
    public double getDiameter() { return yyy; } 

    // Getter/Setter for temperature (already handles validation)
    public void setTemp(int t) { 
        zzz = (t >= MINTEMP) ? t : MINTEMP; 
    } 
    public int getTemp() { return zzz; } 

    // Default constructor
    public Object1() { 
        xxx = NA; 
        yyy = MINDIAMETER; 
        zzz = MINTEMP; 
    } 

    // Constructor for object properties
    public Object1(String n, double d, int t) { 
        setName(n); 
        setDiameter(d); 
        setTemp(t); 
    } 

    // Constructor for filename (now initializes all fields properly)
    public Object1(String f) { 
        this(); // Call default constructor to set name/diameter/temp to valid defaults
        this.fileName = f; 
    } 

    // String representation
    public String toString() { 
        return "A object, named " + xxx + ", with a diameter of " + yyy + " miles, and a temperature of " + zzz + " Kelvin."; 
    } 

    // Getter for filename (if you need to access it later)
    public String getFileName() {
        return fileName;
    }
}

Updated Object2 Class

Fix variable declarations, input flow, and constructor usage:

import java.util.Scanner; 
import java.io.File; 
import java.io.PrintWriter; 
import java.io.IOException; 

public class Object2 { 
    public static void main(String[] args) throws IOException { 
        Scanner kb = new Scanner(System.in); 

        // Step 1: Get filename and create Object1 with the string constructor
        System.out.print("Enter the filename: ");
        String fileName = kb.nextLine(); 
        Object1 z = new Object1(fileName); // Correctly call the overloaded constructor

        // Step 2: Collect object properties from user
        System.out.println("Please enter name: "); 
        String name = kb.nextLine(); 

        System.out.println("Please enter the diameter: "); 
        double diameter = kb.nextDouble(); 

        System.out.println("Please enter the temperature: "); 
        int temp = kb.nextInt(); 

        // Step 3: Update the Object1 instance (validation happens automatically!)
        z.setName(name);
        z.setDiameter(diameter);
        z.setTemp(temp);

        // Step 4: Write data to file
        File file = new File(fileName); 
        PrintWriter outputFile = new PrintWriter(file); 
        outputFile.println(z.getName()); 
        outputFile.println(z.getDiameter()); 
        outputFile.println(z.getTemp()); 
        outputFile.close(); 

        // Step 5: Print the final object
        System.out.println(z); 

        kb.close();
    } 
}

Key Fixes & Explanations

  1. Input Validation: Your original Object1 setters already handle validation (e.g., a diameter of 0.5 gets auto-set to 1). We just fixed the setName method to properly handle null values and whitespace-only strings.
  2. Overloaded Constructor: The Object1(String f) constructor now calls the default constructor (this()) to ensure all fields (name, diameter, temp) are initialized to valid defaults, instead of leaving them unassigned. We moved the filename input prompt to Object2—constructors should focus on initialization, not user interaction.
  3. Variable Declarations: All used variables (fileName, name, diameter, temp) are now declared with correct types in Object2, eliminating the "symbol missing" error.
  4. Input Flow: The flow is simplified to collect one set of values, update the Object1 instance, write to file, and print the final validated object.

内容的提问来源于stack exchange,提问作者VGSO15

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 21:42:47