简体   繁体   中英

How can i access the variable from different class method

This is the main file, im trying to print the result of the Age, Name, Birthdate, etc.

LabExercise1

package labexercise1;
/**
 *
 * @author user
 */
public class LabExercise1{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        NewClass newClass = new NewClass();
        
        NewClass.getFirstName();
        NewClass.getLastName();
        NewClass.getAge();
        NewClass.getBirthDate();
        NewClass.getGWA();
        // TODO code application logic here
        
        System.out.println("My name is " + FirstName + "." + " I am "  + Age + " years old and the day that i was born is on " + BirthDate + ". My general weighted average this semester is " + GWA + ".");
    }

NewClass

package labexercise1;

import java.util.Scanner;

/**
 *
 * @author user
 */
public class NewClass {
    
     public static void getFirstName(){
          Scanner myObj = new Scanner(System.in);  // Create a Scanner object
    System.out.println("Enter First Name: ");

     String FirstName = myObj.nextLine();  // Read user input
         
     }
     
     public static void getLastName(){
         Scanner myObj = new Scanner(System.in);  // Create a Scanner object
    System.out.println("Enter Last Name: ");

    String LastName = myObj.nextLine();  // Read user input
         
     }
     
     public static void getAge(){
         
         Scanner myObj = new Scanner(System.in);
         System.out.println("Enter your age: ");
         
         int Age = myObj.nextInt();
                
     }
     
     public static void getBirthDate(){
         
         Scanner myObj = new Scanner(System.in);
         System.out.println("Enter your BirthDate: ");
         
         String BirthDate = myObj.nextLine();              
     }
     
     public static void getGWA(){
         
         Scanner myObj = new Scanner(System.in);
         System.out.println("Enter your GWA: ");
         
         float GWA = myObj.nextFloat();
                
     }
     
     
}

A lot of this is basic Java 101, concepts and ideas which you should be building around "what is an object".

I would, highly, recommend taking the time to go through things like...

For example...

import java.text.ParseException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws ParseException {
        new Main();
    }

    public Main() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter First Name: ");
        String firstName = scanner.nextLine();
        System.out.print("Enter Last Name: ");
        String lastName = scanner.nextLine();
        System.out.print("Enter Age: ");
        int age = scanner.nextInt();
        scanner.nextLine();
        System.out.print("Enter Birthdate: ");
        String dateOfBirth = scanner.nextLine();
        System.out.print("Enter GWA: ");
        int gwa = scanner.nextInt();
        scanner.nextLine();

        Person person = new Person(firstName, lastName, age, dateOfBirth, gwa);

        System.out.println("First name = " + person.getFirstName());
        System.out.println("Last name = " + person.getLastName());
        System.out.println("Age = " + person.getAge());
        System.out.println("Date of birth = " + person.getDateOfBirth());
        System.out.println("GWA = " + person.getGwa());
    }

    public class Person {
        private String firstName;
        private String lastName;
        private int age;
        private String dateOfBirth;
        private int gwa;

        public Person(String firstName, String lastName, int age, String dateOfBirth, int gwa) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.age = age;
            this.dateOfBirth = dateOfBirth;
            this.gwa = gwa;
        }

        public String getFirstName() {
            return firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public int getAge() {
            return age;
        }

        public String getDateOfBirth() {
            return dateOfBirth;
        }

        public int getGwa() {
            return gwa;
        }
    }
}

Please keep in mind, Stack overflow is NOT a tutorial site/forum and you will, ultimately, be expected to make the effort to learn these basic concepts.

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