Home > Software design >  How to call data's ex. STRINGS,INT,DOUBLE from method to another method
How to call data's ex. STRINGS,INT,DOUBLE from method to another method

Time:10-02

I am stuck in this code for like an hour because i dont know how to call the LN,FN,MD,StudentNo to another method.

import java.util.*;

public class Fvk {
    
    public static void main(String[] args) {
        PersonalInfo();
        //AcademicInfo();
        la();
        }
    
    static void PersonalInfo() {
            try {
                Scanner bruh = new Scanner (System.in);
                
                System.out.println("~PERSONAL INFROMATION~");
                System.out.println();
                System.out.print("\tStudent No.: ");
                int Sno = bruh.nextInt();
                String w = bruh.nextLine();
                System.out.print("\tLast Name:   ");
                String LN = bruh.nextLine();
                System.out.print("\tFirst Name:  ");
                String FN = bruh.nextLine();
                System.out.print("\tMiddle Name: ");
                String MN = bruh.nextLine();
                }
            catch (Exception e) {
                System.out.println();
                System.out.println("            INVALID INPUT           ");
                System.out.println();
                PersonalInfo();
        }
    }
    
    static void la() {
        System.out.println(LN);
        System.out.println(FN);
    }
    
}

CodePudding user response:

Just like that:

import java.util.*;

public class Fvk {
    static String LN, FN; // just these (!?)
    public static void main(String[] args) {
        PersonalInfo();
        //AcademicInfo();
        la();
        }
    
    static void PersonalInfo() {
            try {
                Scanner bruh = new Scanner (System.in);
                
                System.out.println("~PERSONAL INFROMATION~");
                System.out.println();
                System.out.print("\tStudent No.: ");
                int Sno = bruh.nextInt();
                String w = bruh.nextLine();
                System.out.print("\tLast Name:   ");
                // attention: no declaration, just assignment!
                LN = bruh.nextLine();
                System.out.print("\tFirst Name:  ");
                // as here:
                FN = bruh.nextLine();
                System.out.print("\tMiddle Name: ");
                String MN = bruh.nextLine();
                }
            catch (Exception e) {
                System.out.println();
                System.out.println("            INVALID INPUT           ");
                System.out.println();
                PersonalInfo();
        }
    }
    
    static void la() {
        System.out.println(LN);
        System.out.println(FN);
        // should work, sorry: untested!
    }
    
}
  • Related