#Help with using JOptionPane class

1 messages · Page 1 of 1 (latest)

dawn lynxBOT
#

<@&987246399047479336> please have a look, thanks.

dawn lynxBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

silk forge
#

A bit vague, does it want you to enter it in the console or a text field?

sonic hamlet
silk forge
#

JTextfield, it's like a text box for java guis

#

I'm assuming from console because it is asking you to use a Scanner to enter this.

sonic hamlet
#

yea I think so

silk forge
#
import java.util.Scanner;

public class Input {

    private String name;
    private int salary;

    public Input() {
        Scanner scn = new Scanner(System.in);

        System.out.println("What is your name? ");

        name = scn.nextLine();

        System.out.println("What is your salary? ");

        salary = scn.nextInt();

    }


    public String getName() {
        return name;
    }

    public int getSalary() {
        return salary;
    }
}
#

here is the input class.

#
import javax.swing.JOptionPane;

public class Display {
    
    public static void main(String[] args) {

        Input input = new Input();


        JOptionPane.showMessageDialog(null, input.getName() + "'s salary is: $" + input.getSalary());


    }
}
#

So basically, you create a support class that uses a Scanner in the default constructor to set the private datafields of the class to chosen values.

sonic hamlet
#

sorry, what is a constructor?

silk forge
#

A default constructor is like the default method of a class, when you create an instance of that class, that default constructor is "executed" if you may.

sonic hamlet
#

okay

silk forge
#

Input input = new Input(); <--- this line we are creating an instance object of that class called "input"

#

so it runs this:

    public Input() {
        Scanner scn = new Scanner(System.in);

        System.out.println("What is your name? ");

        name = scn.nextLine();

        System.out.println("What is your salary? ");

        salary = scn.nextInt();

    }

asks the user to enter these details

sonic hamlet
#

im sorry I am just having trouble following

silk forge
#

It seems that they want you to use multi classes so you should already be familiar with the ideas of constructors and methods.

sonic hamlet
#

yea

#

the class is moving so fast I really dont know what is going on at this point

#

so i just feel screwed

silk forge
#

What part is confusing you.

sonic hamlet
#

like

#

i dont know where to start

#

sorry

silk forge
#

You have to start somewhere.

#

Basically, java follows this convention called encapsulation meaning that the access to data in classes should be controlled.

#

If you look in the Input class, we created two datafields, name and salary

#

but they are private so we can't access them outside of the class.

#

So we have to write 2 methods, getName() and getSalary() to be able to retrieve these values.

#

as you can see, the two methods specified have the "public" prefix so we can use them outside of the class.

#

Our Input class doesn't have a main method so it is referred to as a "support" class because it is not running the program but providing code for the class Display which has the main method.

sonic hamlet
#

okay thank you, I appreciate the help, I have to go now

dawn lynxBOT
#

Closed the thread.