Jake
Jake

Reputation: 59

How can I call a variable into another method?

I'm not sure what exactly I'm doing wrong, but I want to get the variables from the get methods into a print statetment in the testClass. For instance, the user enters all the information, and then outputs the statement.

import java.util.Calendar;
import java.util.Scanner;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class testClass {

public testClass() {
    System.out.println("ALARM SET TO: " + getHour ":" + getMinutes + amOrPm);

}

public int getHour() {
    Scanner input = new Scanner(System.in);
    int getHour;
    System.out.println("ENTER HOUR FOR YOUR ALARM: ");
    getHour = input.nextInt();
    return getHour; 
}

public int getMinutes() {
    Scanner input = new Scanner(System.in);
    int getMinutes;
    System.out.println("ENTER MINUTES FOR YOUR ALARM: ");
    getMinutes = input.nextInt();
    return getMinutes;      
}

public int getAMOrPM() {
    Scanner input = new Scanner(System.in);
    int amOrPm;
    System.out.println("ENTER AM or PM FOR YOUR ALARM: ");
    amOrPm = input.nextInt();
    return amOrPm;

}

public void getSystemTime() {
    DateFormat df = new SimpleDateFormat("HH:mm");
    Calendar calobj = Calendar.getInstance();
       System.out.println(df.format(calobj.getTime()));
}

} // end of testClass

Upvotes: 0

Views: 60

Answers (1)

John Joe
John Joe

Reputation: 12803

Define a main method to call the constructor class

 public static void main(String[] args)
    {
        testClass r = new testClass();
    }

To call a function, you need to add a bracket. So change to this

 public testClass() {
    System.out.println("ALARM SET TO: " + getHour()+":" + getMinutes() + getAMOrPM());
   }

Upvotes: 3

Related Questions