r/learnjava • u/[deleted] • Oct 08 '23
Lack of understanding in Java (JOptionPane) is causing severe depression, please help?
The title says it all. I am super depressed about not understanding the most basic of Java. You think I am joking but I've spent upwards of 12 hours on basic questions. I need to complete an introduction course to continue with my degree. I am using JOptionPane to create basic input dialog boxes but can't grasp the concept of writing a code and it providing me visual information.
I can't even begin to explain what I know as it is negligent. Any programs, discords, anything at all where people are willing to help.
This is what I need to learn:
Adding the required import statements to the project
o import javax.swing.JOptionPane;
Adding the required comments at the top of the code
Declaring constants of different data types (int, double, boolean, String) and initializing them appropriately using the correct naming conventions discussed in class
Declaring variables of different data types (int, double, boolean, String) and initializing them appropriately using the correct naming conventions discussed in class
Getting user input using a JOptionPane.showInputDialog() function call
Converting user input from String to int using the Integer.parseInt() function
Converting user input from String to double using the Double.parseDouble() function
Converting user input from String to boolean using the Boolean.parseBoolean() function
Assigning values to variables using assignment statements
Doing basic calculations using assignment statements, arithmetic operators (+, –, *, /, %), constants, variables, etc.
Knowing how to check if a given number is perfectly divisible by another number using the appropriate arithmetic operator
Using an if-else()decision structure, relational operators (>, >=, <, <=, ==, !=), logical operators (&&, ||, and !) simple expressions, and compound boolean expressions to make decisions
Setting String literals (anything with double-quotes around it) to String variables using assignment statements
Concatenating strings to add String literals to values in variables and storing the result in String variables
Knowing how to use the String.format() function with the appropriate format specifier (“$%,.2f”, “%02d”, “%.2f”, “%.2f%%”etc.)
Displaying output back to user using a JOptionPane.showMessageDialog() method call
TLDR: Depressed an hopeless but don't want to throw in the towel just yet. I need any resources at all to help learn JOptionPane.
5
u/Lumethys Oct 08 '23 edited Oct 08 '23
I have more than 2 YoE in various languages and i dont even know what JOptionPane is
Also, spend 12 hours on a foreign concept is not something bad, wait until you stuck on a bug for 2 months
Those are normal.
With that said, what exactly you wanna learn about, and what exactly have you know?
1
Oct 08 '23
This is my example of code to determine batting average by dividing # of hits by # of at bats.
import javax.swing.JOptionPane;
/**
* Application purpose: To calculate batting average
* Application inputs: #1. Number of hits
* #2. Number of at bats
* Processing tasks: #1. Calculate batting average by dividing number of hits by number of at bats
* #2. Concatenate an appropriate message to the stored batting average
* and store the results in an output variable
* #3. Display the value in the output variable back to the user
* #4. Exit the application
* Application outputs: #1. Batting average
*/
/**
*
*/
public class practice {
/\*\* \* u/param args \*/ public static void main(String\[\] args) { //Declare the program's variables (2 input, 1 processing, 1 output) int inputNumberOfHitsInteger; int inputNumberOfAtBatsInteger; double battingAverageDouble; String outputMessageString; //Input 1 inputNumberOfHitsInteger = Integer.parseInt(JOptionPane.showInputDialog("Enter " + "number of hits: ")); //Input 2 inputNumberOfAtBatsInteger = Integer.parseInt(JOptionPane.showInputDialog("Enter " + "Number of At Bats: ")); //Calculate batting average by dividing number of hits by number of at bats battingAverageDouble = inputNumberOfHitsInteger / inputNumberOfAtBatsInteger; //Concatenate appropriate message to the value in battingAverageDouble variable //Store result in output variable outputMessageString = "Batting average is " + battingAverageDouble; //Display the value back to user in message box JOptionPane.showMessageDialog(null, outputMessageString);For some reason it isn't doing the calculation properly. On top of that I need to know how to utilize decimals and where I place that code.
2
u/desrtfx Oct 08 '23
In Java, when you divide an integer by an integer, you get an integer as result, hence, no decmals, only primary school mathematics.
As soon as at least one of the operands is a double (type cast, or original data type), floating point division is used.
Your problem is completely unrelated to JOptionPane.
1
1
u/desrtfx Oct 08 '23
1
Oct 08 '23
This is the basic of what code I have to determine batting average. It isn't calculating properly nor is it the decimal amount I am needing.
import javax.swing.JOptionPane;
/**
* Application purpose: To calculate batting average
* Application inputs: #1. Number of hits
* #2. Number of at bats
* Processing tasks: #1. Calculate batting average by dividing number of hits by number of at bats
* #2. Concatenate an appropriate message to the stored batting average
* and store the results in an output variable
* #3. Display the value in the output variable back to the user
* #4. Exit the application
* Application outputs: #1. Batting average
*/
/**
*
*/
public class practice {
/\*\* \* u/param args \*/ public static void main(String\[\] args) { //Declare the program's variables (2 input, 1 processing, 1 output) int inputNumberOfHitsInteger; int inputNumberOfAtBatsInteger; double battingAverageDouble; String outputMessageString; //Input 1 inputNumberOfHitsInteger = Integer.parseInt(JOptionPane.showInputDialog("Enter " + "number of hits: ")); //Input 2 inputNumberOfAtBatsInteger = Integer.parseInt(JOptionPane.showInputDialog("Enter " + "Number of At Bats: ")); //Calculate batting average by dividing number of hits by number of at bats battingAverageDouble = inputNumberOfHitsInteger / inputNumberOfAtBatsInteger; //Concatenate appropriate message to the value in battingAverageDouble variable //Store result in output variable outputMessageString = "Batting average is " + battingAverageDouble; //Display the value back to user in message box JOptionPane.showMessageDialog(null, outputMessageString);
3
u/FakiB Oct 09 '23
Most of this stuff is Java core, you should try learning it from Java Helsinki MOOC. String format is quite tricky so you will need some time. Once you completed the first part of mooc you should be able to approach swing
1
u/MOCannasseur Oct 09 '23
Thank you for your reply! I have attempted to learn from MOOC I am just having a hard time coordinating it with my actual course so I feel I’m being directed in a way that my professor isn’t looking for.
2
Oct 09 '23
If a lack of understanding causes severe depression you should seek help.
1
u/MOCannasseur Oct 09 '23
I know. I just can’t give up. People invested so much to get me where I am. I have to do it!
1
u/Outside-Ad2721 Oct 13 '23
The core Java documentation is something you should learn how to read and use. It provides a lot of useful information.
You didn't say what Java version you're using, so here's the docs for JDK 21:
JOptionPane: https://docs.oracle.com/en/java/javase/21/docs/api/java.desktop/javax/swing/JOptionPane.html
String.format(String, Object ...) uses a standard set of formatters, which are discussed here: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Formatter.html#syntax
I hope this might help.
1
u/lumpynose Oct 08 '23
Swing is so much work. Probably because when you do it manually, instead of using some gui drag and drop thing, it's a lot of work. I wrote a simple thing to manage some stuff for my reddit account using their api. I've decided to redo it as a web app using Jakarta Faces. Out of the frying pan and into the fire!
3
u/desrtfx Oct 08 '23
Just because
JOptionPaneis part of Swing, it doesn't automatically mean work. It is, in fact on the complexity level of a basicScanner(System.in).Calling it to print a message - single liner. Calling it to obtain input - single liner.
0
Oct 09 '23
[deleted]
1
u/MOCannasseur Oct 09 '23
Thank you for your reply! I will look into what you recommended. I am still not familiar with the last sentence of the first paragraph in your reply.
I will look into chatGPT. Tbh I’ve never attempted to even open it. I will learn more about it.
1
u/chet714 Oct 10 '23
The modulo part of that last sentence is explained in this short video. It is a reference to a Java operator, the remainder operator.
https://www.youtube.com/watch?v=pZHJU9T0vwE&list=PLZPZq0r_RZOMhCAyywfnYLlrjiVOkdAI1&index=5
Here is the complete Java playlist: https://www.youtube.com/playlist?list=PLZPZq0r_RZOMhCAyywfnYLlrjiVOkdAI1
•
u/AutoModerator Oct 08 '23
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full - best also formatted as code block
You ask clear questions
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.