Stack Overflow Asked by Chan Myae Tun on January 1, 2022
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
I could see there are few errors in your code:
Profile class includes only Profile constructor, not 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();
//DateTimeFormatter.ofPattern("d MMMM yyyy");
return LocalDate.parse(dateF, df);
}
}
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) +"/"+ month +"/"+ String.valueOf(year);
return dob;
}
}
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());
}
}
Answered by garima garg on January 1, 2022
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP