调用不同类中的字符串方法到主类。

huangapple 未分类评论45阅读模式
英文:

calling String method from different class to main class

问题

以下是您的代码的翻译部分:

class Date {
    // 声明实例变量
    private int day;
    private String month;
    private int year;

    // 构造函数
    public Date(int day, String month, int year) {
        this.day = day;
        this.month = month;
        this.year = year;
    }

    // 获取-设置方法在此
    public String getDateString() {
        String dob = String.valueOf(day) + month + String.valueOf(year);
        return dob;
    }
}

// 这是我的 Profile 类
class Profile {
    private Date dob;
    private String name;

    // 构造函数
    public Profile(Date dob, String name) {
        this.name = name;
        this.dob = dob;
    }

    // 获取-设置方法在此

    public void printProfile() {
        System.out.printf("Name: %s%n", name);
        System.out.printf("Date of birth: %s%n", dob.getDateString());
    }
}

// 这是我的主类。如何使用 Date 类中的 getDateString 方法读取 txt 文件中的名称和出生日期(2019 年 10 月 3 日)?
class Healthprofile {
    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(new File("info.txt"));
        
        // 声明变量
        String name;
        Date dob;
        
        // 这里需要提前定义 day、month 和 year,然后创建 Date 对象
        int day = ...; // 您需要指定实际的 day 值
        String month = ...; // 您需要指定实际的 month 值
        int year = ...; // 您需要指定实际的 year 值
        Date dob1 = new Date(day, month, year);
        
        name = input.nextLine();
        dob = dob1; // 在这里如何调用它
        
        Profile h1 = new Profile(dob, name);
        h1.printProfile();
    }
}

请注意,代码中的 ... 部分需要您填入实际的值。至于如何从 "8 October 2000" 这样的输入中获取 day、month 和 year 值,您可能需要使用字符串解析和转换来从这种格式的输入中提取正确的值。

英文:

I have 3 classes; Date class, profile class and main class. I want to call a getDateString method from Date class and bypass profile class and read a scanner input file as date of birth (format example: 3 October 2019) in main class. How can I do that? Below is my code;

class Date
{
	//Declare instant variables
	private int day;
	private String month;
	private int year;
	
	//constructor
	public Date (int day, String month, int year)
	{
		this.day = day;
		this.month = month;
		this.year = year;
	}
	
	// get-set methods here
	public String getDateString()
	{
		String dob = String.valueOf(day) + month + String.valueOf(year);
		return dob;
	}

} 

[This is my profile class]

class Profile {   private Date dob;
   private string name;

   //constructor
   public HealthProfile (Date dob, String name) 
                            
   {
	   this.name = name;
	   this.dob = dob;
   }
   
   // get-set methods here
   
    public void printProfile()
	{
		System.out.printf ("Name: %s%n", name );
		System.out.printf ("Date of birth: %s%n", dob);
	}			
		  } 

[This is my main class. How can I read txt file input name and dob (3 October 2019) using the getDateString method from Date class.]

class Healthprofile { public static void main (String [] args) throws IOException
{
    Scanner input = new Scanner (new File ("info.txt"));
	// Declare variables
	String name;
	Date dob;
	
	Date dob1 = new Date (day, month, year);
    name = input.nextLine ();
    dob = //How can I call it here
	
    Profile h1 = new Profile (name, dob);
	h1.printProfile();
	
}

}

[This is data in my txt file. Other data are all working but I am having trouble reading for 8 October 2000 line because I want to read it as all 3 data types as one]

Jonathan
8 OCtober 2000

答案1

得分: 0

我可以看到你的代码中有一些错误:

  1. Profile 类只包括 Profile 构造函数,没有 HealthProfile
public class Test3{
    public static void main(String[] args){
        Map<String, MyDate> map = new HashMap<>();
        try(Scanner input = new Scanner(new File("info.txt"))){
            int count = 0;
            String name = "";

            while(input.hasNextLine()) {
                String dateF = "";
                if (count == 0) {
                    name = input.nextLine();
                    count++;
                }
                if (count == 1) {
                    dateF = input.nextLine();
                }
                count = 0;

                LocalDate random = getDate(dateF);
                int dat = random.getDayOfMonth();
                String month = String.valueOf(random.getMonth().ordinal());
                int year = random.getYear();
                // Set MyDate class object for DOB
                MyDate myD = new MyDate(dat, month, year);
                //map.put(name, myD);
                // Call profile methods
                Profile h1 = new Profile(myD, name);
                h1.printProfile();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    // This method will return the formatted LocalDate instance for String Date fetched from File
    private static LocalDate getDate(String dateF){
        LocalDate random = null;
        DateTimeFormatter df = new DateTimeFormatterBuilder()
                .appendOptional(DateTimeFormatter.ofPattern("dd MMMM yyyy"))
                .appendOptional(DateTimeFormatter.ofPattern(("d MMMM yyyy"))).toFormatter();
        return LocalDate.parse(dateF, df);
    }
}
  1. MyDate 类将显示日期值:
class MyDate {
    private int day;
    private String month;
    private int year;

    //constructor
    public MyDate(int day, String month, int year){
        this.day = day;
        this.month = month;
        this.year = year;
    }

    // get-set methods here
    public String getDateString() {
        String dob = String.valueOf(day) + "/" + month + "/" + String.valueOf(year);
        return dob;
    }
}
  1. Profile 类:
class Profile {
    private MyDate dob;
    private String name;

    //constructor
    public Profile(MyDate dob, String name) {
        this.name = name;
        this.dob = dob;
    }

    // get-set methods here
    public void printProfile() {
        System.out.printf("Name: %s%n", name);
        System.out.printf("Date of birth: %s%n", dob.getDateString());
    }
}
英文:

I could see there are few errors in your code:

  1. Profile class includes only Profile constructor, not HealthProfile.

     public class Test3{
     public static void main(String[] args){
      Map&lt;String,MyDate&gt; map=new HashMap&lt;&gt;();
     try( Scanner input = new Scanner(new File(&quot;info.txt&quot;))){
     int count=0;String name=&quot;&quot;;
    
     while(input.hasNextLine()) {
                String dateF = &quot;&quot;;
                if (count == 0) {
                    name = input.nextLine();
                    count++;
                }
                if (count == 1) {
                    dateF = input.nextLine();
                }
                count = 0;
     LocalDate random = getDate(dateF);
     int dat = random.getDayOfMonth();
     String month = String.valueOf(random.getMonth().ordinal());
     int year = random.getYear();
     // Set MyDate class object for DOB
     MyDate myD = new MyDate(dat, month, year);
     //map.put(name, myD);
     // Call profile methods
     Profile h1 = new Profile(myD, name);
     h1.printProfile();
     }
      }
     } catch (FileNotFoundException e) {
     e.printStackTrace();
     }
     }
    

// This method will return the formatted LocalDate instance for String Date fetched from File

    private static LocalDate getDate(String dateF){
        LocalDate random=null;
    DateTimeFormatter df =new DateTimeFormatterBuilder().appendOptional(DateTimeFormatter.ofPattern(&quot;dd MMMM yyyy&quot;)).appendOptional(DateTimeFormatter.ofPattern((&quot;d MMMM yyyy&quot;))).toFormatter();
    //DateTimeFormatter.ofPattern(&quot;d MMMM yyyy&quot;);
    return LocalDate.parse(dateF, df);
    }
    }
  1. MyDate class will display the date value:

     class MyDate{ //Declare instant variables
      private int day;
     private String month;
     private int year;
         //constructor
         public MyDate (int day, String month, int year){
     this.day = day;
     this.month = month;
     this.year = year;
         }
    // get-set methods here
     public String getDateString()
     {
     String dob = String.valueOf(day) +&quot;/&quot;+ month +&quot;/&quot;+ String.valueOf(year);
     return dob;
     }
     }
    

3.

class Profile {
    private MyDate dob;
    private String name;
        //constructor
    public Profile (MyDate dob, String name)
        {
    this.name = name;
    this.dob = dob;
     }
    // get-set methods here
    public void printProfile() {
    System.out.printf(&quot;Name: %s%n&quot;, name);
    System.out.printf(&quot;Date of birth: %s%n&quot;, dob.getDateString());
    }
    }

huangapple
  • 本文由 发表于 2020年7月25日 01:02:16
  • 转载请务必保留本文链接:https://java.coder-hub.com/63078219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定