Hi all. I have a really confusing problem. I'm using Swing and I'm trying to do a confirmation box : -
int reply;
-
String message = "Do you want to input another number?";
-
String title = "Input Another Number?";
-
reply = (JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
-
if (reply == JOptionPane.NO_OPTION) {
-
System.exit(0); }
-
This part works great, now down to business:
I'm trying to get the program to basically reload it, or go back to the beginning
if the reply is YES.
I have googled until my fingers hurt, but I haven't found anything except for JS.
(The window.reload function)
Here's the complete code for my program. If there's anything I can do to accomplish this, please let me know. Otherwise I'll be forced to take it out of the program. By the way, I'm using JCreator. -
// TimeConverter.java
-
// Author: Adam Martin
-
// Assignment #2, Time Converter
-
// This program lets the user input seconds and converts into hours, minutes and seconds (HH:MM:SS).
-
-
// Imports the GUI interface used by Swing. JOptionPane is used for producing special windows called dialog
-
// windows, dialog boxes, or just dialogs.
-
import javax.swing.JOptionPane;
-
-
public class TimeConverter
-
{
-
public static void main(String[] args)
-
{
-
-
-
// Input dialog box to input number of seconds. Takes the input from the user and assigns it to a string
-
// and converts the string into an integer
-
-
String secondsString = JOptionPane.showInputDialog("Enter number of seconds:");
-
int secondsAmount = Integer.parseInt(secondsString);
-
-
int hours, minutes, seconds, remainder; // whole number for hours, divides seconds by 3600 (number of seconds in one hour)
-
hours = secondsAmount / 3600; // the remainder operator (%) will get the remainder of secondsAmount / 3600
-
remainder = secondsAmount % 3600; // whole number for minutes, divides the remainder by 60 (number of seconds in one minute)
-
minutes = remainder / 60; // remainder operator gets the remainder of secondsAmount / 60, it will be used as the left
-
seconds = remainder % 60; // over seconds that were not converted
-
-
JOptionPane.showMessageDialog(null,"The time is " + (hours < 10 ? "0" : "") + hours + ":" +
-
(minutes < 10 ? "0" : "") + minutes + ":" + (seconds <10 ? "0" : "") + seconds + " (HH:MM:SS)");
-
-
int reply;
-
String message = "Do you want to input another number?";
-
String title = "Input Another Number?";
-
reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
-
if (reply == JOptionPane.NO_OPTION) {
-
System.exit(0); }
-
if (reply == JOptionPane.YES_OPTION) {
-
}
-
}
-
}
-
-
Thanks,
Adam
8 5655
Just a while loop will do: -
while (true) {
-
// all your original code here ...
-
// ...
-
if (JOptionPane.showConfirmDialog(null, message, title,
-
JOptionPane.YES_NO_OPTION == JOptionPane.NO_OPTION)
-
System.exit(0);
-
}
-
kind regards,
Jos
That's all there is to it? What determines the true/false value of the code?
Read the code that has been posted and see what that if is doing there.
The if statement is if the JOptionPane.NO_OPTION is selected, the system will exit. BUT, there is no statement for the boolean value of true/false for the option selected.
@risk32
You don't need it, it is directly put in the if clause. Anything that can be true or false can be used for an if clause;
You did this: -
boolean result= ... something complicated ...
-
if (result) ...
-
and I did this: -
if (... something complicated ...) ...
-
kind regards,
Jos
@JosAH
I dont know where to post the new thread
iam login after a long time ,iam ashame to post here
Go to answers - bytes
There is a button for "New Thread" near the top left corner.
Thanks for the help Jos, I had to tweak my code a little since I had 2 showConfirmDialog screens. I also had to add an extra ')' that was missing. I found that out from my Java instructor. I must say, it's conforting that there are individuals like yourselves willing to help others when learning, or when they're stuck. I really appreciate it.
Adam
Sign in to post your reply or Sign up for a free account.
Similar topics
by: SubbaRao Karanam |
last post by:
What does this error for the Code below
java.io.StreamCorruptedException: invalid stream header
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at...
|
by: RobertMaas |
last post by:
After many years of using LISP, I'm taking a class in Java and finding
the two roughly comparable in some ways and very different in other
ways. Each has a decent size library of useful utilities...
|
by: DrUg13 |
last post by:
In java, this seems so easy. You need a new object
Object test = new Object() gives me exactly what I want.
could someone please help me understand the different ways to do the
same thing in...
|
by: David Van D |
last post by:
Hi there,
A few weeks until I begin my journey towards a degree in Computer
Science at Canterbury University in New Zealand,
Anyway the course tutors are going to be teaching us JAVA wth bluej...
|
by: erekose666 |
last post by:
I have this program right now, but I need to modify it to output to the JTextPane.
What I mean is, when someone clicks on the 1 button, it needs to output to the TextArea a 1. None of the add,...
|
by: oll3i |
last post by:
package library.common;
import java.sql.ResultSet;
public interface LibraryInterface {
public ResultSet getBookByAuthor(String author);
public ResultSet getBookByName(String name);
|
by: helpwithcode |
last post by:
Hi people,
I am just learning java.I have been creating a project which involves JDBC Connectivity.I find that the statements,
String string_dob=text_dob.getText();
//Converting string to...
|
by: twin2003 |
last post by:
need help with inventory part 5 here is what I have to do
Modify the Inventory Program by adding a button to the GUI that allows the user to move to the first item, the previous item, the next...
|
by: xirowei |
last post by:
public class Result
{
private int countA = 0;
private int countB = 0;
private int statement;
private boolean statusA = false;
private boolean statusB = false;
private int arrayA = new...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |