Home > OS >  JAVA - Two Classes - How To Pass User Input From One Class To Another?
JAVA - Two Classes - How To Pass User Input From One Class To Another?

Time:04-10

Very new to programming, only a few weeks into my first Java course. I have looked at similar queries here but the programs in question are a lot more complicated than my simple one so it's hard to make out the solution! I basically have two classes, Dates and DatesTest. I want to input the values in DatesTest and pass them to Dates for printing using Set & Get. Everything is compiling fine but my values are all being printed as zero, can anyone point out what I've missed please?

public class Dates
{
    
    private int dateMonth;
    private int dateDay;
    private int dateYear;
    
    public Dates( int month , int day , int year)
    {
        dateMonth = month;
        dateDay = day;
        dateYear = year;
    }
    
    
    public void setDateMonth( int month )
   {
      dateMonth = month; 
   } 

   
   public int getDateMonth()
   {
      return dateMonth;
   } 
   
   public void setDateDay( int day )
   {
      dateDay = day; 
   } 

   
   public int getDateDay()
   {
      return dateDay;
   } 
   
   public void setDateYear( int year )
   {
      dateYear = year; 
   } 

   
   public int getDateYear()
   {
      return dateYear;
   } 
    
    
    
    public void displayDate(int month , int day , int year)
    {
        System.out.printf("\nToday's date is: %d/%d/%d" , getDateMonth() , getDateDay() , getDateYear() );
    }


}//end of class Dates



import java.util.Scanner;

public class DatesTest
{
    public static void main(String[] args)
    {
        
        
        Scanner input = new Scanner(System.in);
        int newMonth = 0;
        int newDay = 0;
        int newYear = 0;
        
        Dates date1 = new Dates( newMonth , newDay , newYear);
        
        System.out.println("Please enter the current month: ");
        newMonth = input.nextInt();
        
        System.out.println("Please enter the current day: ");
        newDay = input.nextInt();
        
        System.out.println("Please enter the current year: ");
        newYear = input.nextInt();
        
        date1.displayDate(newMonth , newDay , newYear);
        
        
    }
}//end of class DatesTest

CodePudding user response:

Move the constructor, Dates date1 = new Dates( newMonth , newDay , newYear); to the end of your main method, after the user input is complete, and then call displayDate(...). This will work on its own, but you should also remove the parameters of displayDate(...) as they are not being used.

CodePudding user response:

At first get the user input values: newMonth , newDay , newYear then you can use these input variables in the date1 object.

Initialize the class object Dates date1 = new Dates( newMonth , newDay , newYear); after that.

CodePudding user response:

At the moment you call the constructor, the variables are all set to 0, thats why the object is created with those values.

You have to ask for user input before calling the constructor, so the variables are already assigned and not 0.

  •  Tags:  
  • java
  • Related