Member_2_4942450
asked on
Can not compile after doing math cal to display output in dialog box
I am trying to get the user to input for example 197 mins and have the results displayed at
3 hr and 17 mins in a dialog box
Can't seem to figure out what my problem is
3 hr and 17 mins in a dialog box
Can't seem to figure out what my problem is
import javax.swing.JOptionPane;
import java.util.Scanner;
public class time2
{
public static void main(String[] args)
{
String minutesString, hoursString;
double minutes, hours;
int dependents;
final double MINUTES_IN_HOUR = 60;
minutesString = JOptionPane.showInputDialog(null, "How many minutes did you work?" , "You worked" ,
JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog
(null, "You have worked " + timeInMinutes/60 + " hours and " + timeInMinutes%60 + " minutes.");
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In the code above I assumed that the use will type
minutes after the questioon on the same line
minutes after the questioon on the same line
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok will check shortly.
taking break will be back on in about 1hr
taking break will be back on in about 1hr
ASKER
Ok I used this one below. Worked ran and compiled without issues.
The only problem is
A dialog box came up asking me to put the mins worked.
I input the mins for example 197
And nothing else happened program just ended.
I need the next dialog box to display this
You worked 3 hr and 17 mins
Right now it just ends after I enter the mins in
The code didn't do any of the calculation of the mins and did not display them.
Everthing else looks cool now just need to display the results
The only problem is
A dialog box came up asking me to put the mins worked.
I input the mins for example 197
And nothing else happened program just ended.
I need the next dialog box to display this
You worked 3 hr and 17 mins
Right now it just ends after I enter the mins in
The code didn't do any of the calculation of the mins and did not display them.
Everthing else looks cool now just need to display the results
import javax.swing.JOptionPane;
import java.util.Scanner;
public class time2
{
public static void main(String[] args)
{
String minutesString = null;
int minutes;
minutesString = JOptionPane.showInputDialog(null, "How many minutes did you work?" , "You worked" );
String mString = minutesString.substring(11);
System.out.println(mString);
minutes = Integer.parseInt(mString);
JOptionPane.showMessageDialog
(null, "You have worked " + minutes/60 + " hours and " + minutes%60 + " minutes.");
}
}
You need to click OK, then the next dialog will then pop-up
and show hours and minutes.
It waits for you to click OK.
and show hours and minutes.
It waits for you to click OK.
ASKER
I also got this
Exception in thread "main" java.lang.StringIndexOutOf BoundsExce ption: String index out of range: -8
at java.lang.String.substring (String.ja va:1937)
at java.lang.String.substring (String.ja va:1904)
at time2.main(time2.java:11)
Exception in thread "main" java.lang.StringIndexOutOf
at java.lang.String.substring
at java.lang.String.substring
at time2.main(time2.java:11)
It works for me.
Should work for you.
Take my seond posting.
Compile and run it.
It will popup the widow.
In the textline it syas "You worked"
You type 170 (it will replace "You worked")
There should be only 170 left in the box.
Then you click OK
You see next box which says "You have worked for 2 hours and 50 min".
Please, try again exactly in this way and let me know
if it works or describe in detail,
if it fails (it should not because java is everuywhere the same).
Should work for you.
Take my seond posting.
Compile and run it.
It will popup the widow.
In the textline it syas "You worked"
You type 170 (it will replace "You worked")
There should be only 170 left in the box.
Then you click OK
You see next box which says "You have worked for 2 hours and 50 min".
Please, try again exactly in this way and let me know
if it works or describe in detail,
if it fails (it should not because java is everuywhere the same).
ASKER
I will try the 2nd one but the first one was simpler for me to understand. The other went into stuff that I hadn't taught myself yet
The first one also works - but in the fiorst one you should not remove the words "You worked"
Just add a space and enter the number.
Before you click OK, you should see in the line:
You worked 170
Then you click OK and it will also work
Just add a space and enter the number.
Before you click OK, you should see in the line:
You worked 170
Then you click OK and it will also work
ASKER
oh ok maybe that was my problem trying it now.
Only thing when I put the mins my output should do the calculations into hours mins.
Will check now
could I remove that part so all I have to do is enter the number?
Only thing when I put the mins my output should do the calculations into hours mins.
Will check now
could I remove that part so all I have to do is enter the number?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I did go back and redo it and it did work when I put in the number after "You worked"
But I want just to be able to input the number in the dialog box not have "You worked" inside the box followed by a number.
How would I get to that.
Otherwise it's perfect
But I want just to be able to input the number in the dialog box not have "You worked" inside the box followed by a number.
How would I get to that.
Otherwise it's perfect
the code in your original question was almost there
you just need to declare the timeInMinutes variable
see below:
you just need to declare the timeInMinutes variable
see below:
import javax.swing.JOptionPane;
public class time2 {
public static void main(String[] args) {
String minutesString;
final double MINUTES_IN_HOUR = 60;
minutesString = JOptionPane.showInputDialog(null,
"How many minutes did you work?", "You worked",
JOptionPane.INFORMATION_MESSAGE);
int timeInMinutes = Integer.parseInt(minutesString);
JOptionPane.showMessageDialog(null, "You have worked " + timeInMinutes
/ MINUTES_IN_HOUR + " hours and " + timeInMinutes
% MINUTES_IN_HOUR + " minutes.");
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Checking that now sorry posted after you had already responded back ...
Checking that now
Checking that now
And your constant (its a good idea to use that) should be an int not a double
import javax.swing.JOptionPane;
public class time2 {
public static void main(String[] args) {
String minutesString;
final int MINUTES_IN_HOUR = 60;
minutesString = JOptionPane.showInputDialog(null,
"How many minutes did you work?", "You worked",
JOptionPane.INFORMATION_MESSAGE);
int timeInMinutes = Integer.parseInt(minutesString);
JOptionPane.showMessageDialog(null, "You have worked " +
timeInMinutes / MINUTES_IN_HOUR + " hours and " +
timeInMinutes % MINUTES_IN_HOUR + " minutes.");
}
}
ASKER
Thanks that worked pefectly
Could you explain a bit about the meaning behind the
String mString = minutesString.substring(11 );
Could you explain a bit about the meaning behind the
String mString = minutesString.substring(11
> String mString = minutesString.substring(11 );
takes a substring of the original string start at 11th character
why did you add that?
takes a substring of the original string start at 11th character
why did you add that?
I first thought that you expected the user to type after the suugestion not instead of it.
Then if we actually read string:
You worked 170
then to get to the number we need to strip away the bgiining part of the string - the first 11 chracters.
Once you expect the user to overwrite with the number, you don't need it.
Still look at my second code.
On interaction with (usually unpredicatable) user you have to
take into account cases that user responds
with some weird answer and try in you program to
analyze it and make amn attempt of graceful treatment oof the situation.
Then if we actually read string:
You worked 170
then to get to the number we need to strip away the bgiining part of the string - the first 11 chracters.
Once you expect the user to overwrite with the number, you don't need it.
Still look at my second code.
On interaction with (usually unpredicatable) user you have to
take into account cases that user responds
with some weird answer and try in you program to
analyze it and make amn attempt of graceful treatment oof the situation.
the accepted comment won't actually work
anyone interested in whats needed can check my last comment
basically just a minor change to the code that was originally posted in the question, just needed a variable declaration added
anyone interested in whats needed can check my last comment
basically just a minor change to the code that was originally posted in the question, just needed a variable declaration added
objects,
Let me at least once try to convince you to drop this hostile attitude.
You know pretty well that accepted solution does work, I explained how it works,
author understood it, there is no reason to post these hostile attacks.
In general your constant attacks, numerous
objections in many cases without merit, I really don't undertstand, why are
you doing all that.
We can have some different attitudes and different java experiences - that is true, we
can each one metion and promote our attitudes, but why should it all be so hostile?
In my mind there is absolutely no reason for it; it just spoils the pleasure
of looking for intersting solutions and sometimes even helping some good people.
Please, rethink your attitude.
Let me at least once try to convince you to drop this hostile attitude.
You know pretty well that accepted solution does work, I explained how it works,
author understood it, there is no reason to post these hostile attacks.
In general your constant attacks, numerous
objections in many cases without merit, I really don't undertstand, why are
you doing all that.
We can have some different attitudes and different java experiences - that is true, we
can each one metion and promote our attitudes, but why should it all be so hostile?
In my mind there is absolutely no reason for it; it just spoils the pleasure
of looking for intersting solutions and sometimes even helping some good people.
Please, rethink your attitude.
ASKER
Open in new window