#Help with using JOptionPane class
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
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.
A bit vague, does it want you to enter it in the console or a text field?
is a text field like a pop up window?
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.
yea I think so
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.
sorry, what is a constructor?
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.
okay
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
im sorry I am just having trouble following
It seems that they want you to use multi classes so you should already be familiar with the ideas of constructors and methods.
yea
the class is moving so fast I really dont know what is going on at this point
so i just feel screwed
What part is confusing you.
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.
okay thank you, I appreciate the help, I have to go now
Closed the thread.