简体   繁体   中英

how can i call a variable from another function/method?

I have an assignment to do a CV that users will input on and display it. However, I don't know how I can call a variable to another to function to print/display.

Here is the code:

import java.util.Scanner;
public class curriculumVitae1{
    public static String firstName;
    public static String middleName, lastName, birthDate, maritalStatus, homeAddress, provincialAddress, mobileNumber, anotherMobile, landlineNumber, anotherLandline, primaryYears; 
    private static void main (String args[]){
        Scanner input = new Scanner(System.in); 
            System.out.print("\nCurriculum Vitae");
            System.out.print("\nInput your  last name: ");
            String lastName; 
            lastName = input.nextLine();
            System.out.print("\nInput your  first name: ");
            String firstName; 
            firstName = input.nextLine();

            System.out.print("\nInput your  middle name: ");
            String middleName; 
            middleName = input.nextLine();


            System.out.print("\nInput your  birthdate: ");
            String birthDate; 
            birthDate = input.nextLine();


            System.out.print("\nInput your  marital status (Married, Widowed, Separated, Divorced, Single) : ");
            String maritalStatus; 
            maritalStatus = input.nextLine();

            System.out.print("\nInput your  home address: ");
            String homeAddress; 
            homeAddress = input.nextLine();

            curriculumVitae1.cv();
    }
    private static void provincial(String args[]){
        Scanner input = new Scanner(System.in); 
            System.out.print("\nDo you have a provincial address? Enter Y if yes, and N if no:  ");
            char provincialQuestion; 
            provincialQuestion = input.nextLine().charAt(0);
            if (provincialQuestion=='Y'){
                System.out.print("\nInput your  provincial address: ");
                String provincialAddress; 
                provincialAddress = input.nextLine();
            }
            else if(provincialQuestion=='N'){
            }
    }
    private static  void mobile(String args[]){
        Scanner input = new Scanner(System.in); 
            System.out.print("\nContact Details ");
                System.out.print("\nInput your mobile number: ");
                String mobileNumber; 
                mobileNumber = input.nextLine();
            System.out.print("\nDo you have another mobile number? Enter Y if yes, and N if no:  ");
            char mobileQuestion; 
            mobileQuestion = input.nextLine().charAt(0);
            if (mobileQuestion=='Y'){
                System.out.print("\nInput another mobile number: ");
                String anotherMobile; 
                anotherMobile = input.nextLine();

            }
            else if(mobileQuestion=='N'){   

            }
    }
    private static void landline(String args[]){
        Scanner input = new Scanner(System.in); 
            System.out.print("\nInput your landline number: ");
                String landlineNumber; 
                landlineNumber = input.nextLine();
                System.out.print("\nDo you have another landline number? Enter Y if yes, and N if no:  ");
            char landlineQuestion; 
            landlineQuestion = input.nextLine().charAt(0);
            if (landlineQuestion=='Y'){
                System.out.print("\nInput another mobile number: ");
                String anotherLandline; 
                anotherLandline = input.nextLine();

            }
            else if (landlineQuestion=='N'){

            }
    }
    private static String email(){
        Scanner input = new Scanner(System.in);
            System.out.print("\nInput your email address: ");
                String emailAddress; 
                emailAddress = input.nextLine();
                return emailAddress;
    }
    private static String tertiary(){
        Scanner input = new Scanner(System.in);
            System.out.print("\nEducation History ");
            System.out.print("\nTertiary Education ");

            System.out.print("\nInput your tertiary education course: ");
                String tertiaryCourse; 
                tertiaryCourse = input.nextLine();

            System.out.print("\nInput your tertiary education school: ");
                String tertiarySchool; 
                tertiarySchool = input.nextLine();

            System.out.print("\nInput your tertiary education inclusive years (xxxx-xxxx): ");
                String tertiaryYears; 
                tertiaryYears = input.nextLine();

            System.out.print("\nDo you have any honors/achivements received during your tertiary education? Enter Y if yes, and N if no:  ");
            char tertiaryQuestion; 
            tertiaryQuestion = input.nextLine().charAt(0);
            if (tertiaryQuestion=='Y'){
                System.out.print("\nInput your honor/s or achivement/s:");
                String tertiaryAchievements; 
                tertiaryAchievements = input.nextLine();
                return tertiaryAchievements;
            }
            else if (tertiaryQuestion=='N'){
                return "------";
            }
    }
    private static void secondary(String args[]){
        Scanner input = new Scanner(System.in);
            System.out.print("\nSecondary Education ");
            System.out.print("\nInput your secondary education school: ");
                String secondarySchool; 
                secondarySchool = input.nextLine();

            System.out.print("\nInput your secondary education inclusive years (xxxx-xxxx): ");
                String secondaryYears; 
                secondaryYears = input.nextLine();

            System.out.print("\nDo you have any honors/achivements received during your secondary education? Enter Y if yes, and N if no:  ");
            char secondaryQuestion; 
            secondaryQuestion = input.nextLine().charAt(0);
            if (secondaryQuestion=='Y'){
                System.out.print("\nInput your honor/s or achivement/s:");
                String secondaryAchievements; 
                secondaryAchievements = input.nextLine();
            }
            else if (secondaryQuestion=='N'){
            }
    }
        public static void primary(String args[]){
            Scanner input = new Scanner(System.in);
            System.out.print("\nPrimary Education ");
            System.out.print("\nInput your primary education school: ");
                String primarySchool; 
                primarySchool = input.nextLine();


            System.out.print("\nInput your primary education inclusive years (xxxx-xxxx): ");
                String primaryYears; 
                primaryYears = input.nextLine();


            System.out.print("\nDo you have any honors/achivements received during your primary education? Enter Y if yes, and N if no:  ");
            char primaryQuestion; 
            primaryQuestion = input.nextLine().charAt(0);
            if (primaryQuestion=='Y'){
                System.out.print("\nInput your honor/s or achivement/s:");
                String primaryAchievements; 
                primaryAchievements = input.nextLine();
            }   
            else{
                System.out.print("------");
            }
        }
        public static void cv(String args[]){
            System.out.println("                                               Curriculum Vitae");
            System.out.print("\nName:" + firstName + " " + middleName + " "+ lastName);
            System.out.print("\nBirthdate:" + birthDate);
            System.out.print("\nMarital Status:" + maritalStatus);
            System.out.print("\nHome Address:" + homeAddress);
            System.out.print("\nProvincial Address:" + provincialAddress);
            System.out.print("\nMobile Number:" + mobileNumber );
            System.out.print("\nAnother Mobile Number:" + anotherMobile);
            System.out.print("\nLandline:" + landlineNumber);
            System.out.print("\nYear: " + primaryYears);


}
}

However, I always get the error that

 C:\Users\BEST\Desktop\wew>javac curriculumVitae1.java
curriculumVitae1.java:33: error: method cv in class curriculumVitae1 cannot be applied to given types;
                        curriculumVitae1.cv();
                                        ^
  required: String[]
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error

Please help me on how can I print out another variable from other function. Or some alternatives that I can do.

Your methods expect an (String[] args) however since you don't use them I would remove them. Try

public static void cv() {

The error details highlight that you're calling the method without providing the required parameters in the method signature public static void cv(String args[]) :

  • The required part tells you what types of arguments are expected, here String[] and the found part tells you what it saw instead, here it saw you passed no arguments.

  • The reason tells you that the actual (what you provided) number of arguments differs from the formal (what the method signature defines) number of arguments expected, ie not enough or too many arguments were provided.

You can also get this from the original error message:

error: method cv in class curriculumVitae1 cannot be applied to given types;
                        curriculumVitae1.cv();

It doesn't explicitly state it, but from the line of code shown below you can see that the "given types" are nothing because the method was called with no arguments—nothing inside the parentheses.

Like Peter Lawrey said, you can just remove the String args[] from your method signature since you don't use it.

Hope this helps you understand error messages and what they're telling you a little better!

Make the following changes to your program:

  • Change the access specifier of main() method from private to public. Otherwise the code will throw the error - Does not contain a main method

  • Since you have firstName, middleName, lastName, birthDate etc. declared as static variables do not declare them as local variables in main method . Assign the values to the already declared static variables in the main() method as shown below:

     public static void main (String args[]){ Scanner input = new Scanner(System.in); System.out.print("\\nCurriculum Vitae"); System.out.print("\\nInput your last name: "); //String lastName; lastName = input.nextLine(); System.out.print("\\nInput your first name: "); //String firstName; firstName = input.nextLine(); System.out.print("\\nInput your middle name: "); //String middleName; middleName = input.nextLine(); System.out.print("\\nInput your birthdate: "); //String birthDate; birthDate = input.nextLine(); System.out.print("\\nInput your marital status (Married, Widowed, Separated, Divorced, Single) : "); //String maritalStatus; maritalStatus = input.nextLine(); System.out.print("\\nInput your home address: "); //String homeAddress; homeAddress = input.nextLine(); //System.out.println(); cv(); } 
    • Remove the String[] args argument from cv() method as it is not being used.

    • Since cv() is a static method , it can be called directly from the main() .

    • Return the string type varaible from tertiary() method as it is giving a compile error. You can do so by declaring tertiaryAchievements variable outside the if-else block and then returning it as shown below:

    String tertiaryAchievements=""; if (tertiaryQuestion=='Y')

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