0
// default packages
import java. util.*;
import java. lang.*;
import java.io.*;
import java.net.*;
import java. awt.*;
import java. applet.*;

class msg {
    void data() {
        String name, dep, age, bdg, hob;
        System.out.println("\nStudent data .....:)");
        System.out.print("\nEnter your name:");
        Scanner a = new Scanner(System.in);
        name = a.nextLine();
        System.out.print("\nEnter your department:");
        Scanner b = new Scanner(System.in);
        dep = b.nextLine();
        System.out.print("\nEnter your age:");
        Scanner c = new Scanner(System.in);
        age = c.nextLine();
        System.out.print("\nEnter your blood-group:");
        Scanner d = new Scanner(System.in);
        bdg = d.nextLine();
        System.out.print("\nEnter your hobbies:");
        Scanner e = new Scanner(System.in);
        hob = e.nextLine();
    }

    void showdata() {
        System.out.println("\nStudent data inserted successfully .....:)");
        System.out.println("Name :" + name);
        System.out.println("Department :"+dep);
        System.out.println("Age :"+age);
        System.out.println("Blood-Group :"+bdg);
        System.out.println("Hobbies :"+hob);
    }
}

public class display {
    public static void main(String[] args) {
        msg d=new msg();
        d.data();
        d.showdata();
        System.out.println("Welcome to java development !!!");
    }
}

How do I call these variables – > name, dep, age, bdg, hob – > inside the method showdata() and print that details, is there any option to call another variable of another method in a different method but in the same class



Output:
defaultjava.java:32: error: cannot find symbol
     System.out.println("Name:"+name);
     ^
  symbol:   variable name
  location: class msg
0

1 Answer 1

1

Posting OPs solution since they're unable to do it themselves.

The solution is to make the variables in question members of the class by defining them outside of any methods:

// default packages
import java.util.*;
import java.lang.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.applet.*;

class msg {
    String name, dep, age, bdg, hob;
    void data() {
        System.out.println("\nStudent data .....:)");
        System.out.print("\nEnter your name:");
        Scanner a = new Scanner(System.in);
        name = a.nextLine();
        System.out.print("\nEnter your department:");
        Scanner b = new Scanner(System.in);
        dep = b.nextLine();
        System.out.print("\nEnter your age:");
        Scanner c = new Scanner(System.in);
        age = c.nextLine();
        System.out.print("\nEnter your blood-group:");
        Scanner d = new Scanner(System.in);
        bdg = d.nextLine();
        System.out.print("\nEnter your hobbies:");
        Scanner e = new Scanner(System.in);
        hob = e.nextLine();
    }

    void showdata() {
        System.out.println("\nStudent data inserted successfully .....:)");
        System.out.println("Name :" + name);
        System.out.println("Department :"+dep);
        System.out.println("Age :"+age);
        System.out.println("Blood-Group :"+bdg);
        System.out.println("Hobbies :"+hob);
    }
}

public class display {
    public static void main(String[] args) {
        msg d=new msg();
        d.data();
        d.showdata();
        System.out.println("Welcome to java development !!!");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.