简体   繁体   中英

Calling functions from other files “Can't find symbol”

Trying to call some functions from various classes for an exercise.

We are told to use the following function calls,

MonthName.month_name();
WeekdayName.weekday_name();
MonthOffset.month_offset();
WeekdayCalculator.is_leap();

When I tried MonthName.month_name(); It says it is a non static method, it is infact a static method as seen in below, I sorted that by calling it like,

MonthName monthObject = new MonthName();

Which sorted that problem out. But every other function I try to call i get the msg it can't find symbol.

The class I am trying to call the functions in

public class callingfunctionsfromotherfiles



public static void main(String[] args)
{
    // TODO code application logic here

    Scanner scan = new Scanner(System.in);
    String finalDate="";
    System.out.println("Pleaase enter your date of birth in this format dd mm yyyy");
    String dob=scan.nextLine(); // date of birth entered by the user


    String d = dob.substring(0,2); //split the dob into 3 parts
    String m = dob.substring(3, 5);
    String y = dob.substring(6, 10);

    int day = Integer.parseInt(d); //convert dob strings to int
    int month = Integer.parseInt(m);
    int year = Integer.parseInt(y);
    MonthName monthObject = new MonthName();
 // MonthName.month_name(month); non static???

 System.out.println(finalDate= "You where born on " + week_day(day, month, year) +", " + monthObject.month_name(month) + ", "+ day + ", " + year );

}



public static String week_day(int day, int month, int year)  // returns a string value for the week day with integer parameters of day,month,year
   {

       MonthOffset offsetObject = new MonthOffset(); //"Can't find symbol"
       WeekDayCalculator leapObject = new WeekDayCalculator(); //"Can't find symbol"
       WeekDayName wkdayObject = new WeekDayName(); //"Can't find symbol"

   int yy = year - 1900;
   int total = yy / 4 + yy + day + offsetObject.month_offset(month);
  // weekDayCalculator.month_offset();

   //WeekDayCalculator.is_leap();
    boolean leap = leapObject.is_leap(year); //pass "year" into the is_leap method which returns true or false
    if(leap == true && month == 1 || month == 2) //if it is a leap year and the month == 1(january) 2(february) subtract 1 from the total
    {
        total-= -1;
    }


   total = total %7; //total is remainder of (total / 7)
   String finalDay = wkdayObject.weekday_name(total); // call weekday_name and pass in total, giving finalDay a string value from weekday_name

    return finalDay;




}

This is the class & function that works when I create an object, but also tells me the function is non static when really it is.

public class monthname
{
public static String month_name(int num)

{
    String month ="";

    if( num == 1)
    {
        month ="January";
    }

    else if(num == 2)
    {
        month ="Feburary";
    }

    else if(num == 3)
    {
        month ="March";
    }

    else if(num == 4)
    {
        month ="April";
    }

    else if(num == 5)
    {
        month ="May";
    }

    else if(num == 6)
    {
        month ="June";
    }

    else if(num == 7)
    {
        month ="July";
    }

    else if(num == 8)
    {
        month ="August";
    }

    else if(num == 9)
    {
        month ="September";
    }

    else if(num == 10)
    {
        month ="October";
    }

    else if(num == 11)
    {
        month ="November";
    }

    else if(num == 12)
    {
        month ="December";
    }

    else
    {
        month ="error";
    }
    return month;

}
}

And this is one of the classes & functions where it can't find symbol. Will post more if needed.

public class monthoffset
{
 public static int month_offset(int month)
{
    int offset =0;

    if (month == 1)
    {
        offset=1;
    }
    else if (month == 2)
    {
        offset = 4;
    }
    else if (month == 3)
    {
        offset = 4;
    }
    else if (month == 4)
    {
        offset = 0;
    }
    else if (month == 5)
    {
        offset = 2;
    }
    else if (month == 6)
    {
        offset = 5;
    }
    else if (month == 7)
    {
        offset = 0;
    }
    else if (month == 8)
    {
        offset = 3;
    }
    else if (month == 9)
    {
        offset = 6;
    }
    else if (month == 10)
    {
        offset = 1;
    }
    else if (month == 11)
    {
        offset = 4;
    }
    else if(month ==12)
    {
        offset=6;
    }

    else
    {
       offset = -1;
    }


    return offset;
}

}

MonthOffset is not monthoffset. You have syntax erros, Try to use Eclipse or IntelliJ to have some help from an IDE.

It's syntax error. You don't have class MonthOffset etc. Change name of your class from monthoffset to Monthoffset.

Convention is here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM