[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

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
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.");				
	
	}
}

Open in new window

0
sobeservices2
Asked:
sobeservices2
  • 11
  • 9
  • 4
4 Solutions
 
sobeservices2Author Commented:
I also tried the below
import javax.swing.JOptionPane;
import java.util.Scanner;
public class time2
{
	public static void main(String[] args)
	{

		String minutesString;
		int minutes;
		JOptionPane.showInputDialog(null, "How many minutes did you work?" , "You worked" , 
		minutes = Integer.parseInt(minutesString);
		JOptionPane.showMessageDialog
                  (null, "You have worked " + minutes/60 + " hours and " + minutes%60 + " minutes.");				
	
	}
}

Open in new window

0
 
for_yanCommented:

Try this:
import javax.swing.JOptionPane;
import java.util.Scanner;
public class time
{
	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.");

	}
}

Open in new window

0
 
for_yanCommented:
In the code above I assumed that the use will type
minutes after the questioon on the same line
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
for_yanCommented:

This code assumes that the user
will type imeediately replcaing "You worked"
It also checks if the user enters non-numeric input

import javax.swing.JOptionPane;
import java.util.Scanner;
public class time2
{
	public static void main(String[] args)
	{

		String minutesString = null;
		int minutes = -1;
		minutesString = JOptionPane.showInputDialog(null, "How many minutes did you work?" , "You worked " );
        String mString = minutesString;
        System.out.println(mString);


        try{
        minutes = Integer.parseInt(mString);
        }
            catch(Exception ex){
                        System.out.println("Illegal number " + ex.toString());
        }
        if(minutes > -1)
        JOptionPane.showMessageDialog
                  (null, "You have worked " + minutes/60 + " hours and " + minutes%60 + " minutes.");
        else
               JOptionPane.showMessageDialog
                  (null, "Please eneter valid number");


    }
}

Open in new window

0
 
sobeservices2Author Commented:
ok will check shortly.

taking break will be back on in about 1hr
0
 
sobeservices2Author Commented:
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

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.");

        }
}

Open in new window

0
 
for_yanCommented:
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.
0
 
sobeservices2Author Commented:
I also got this

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -8
      at java.lang.String.substring(String.java:1937)
      at java.lang.String.substring(String.java:1904)
      at time2.main(time2.java:11)

0
 
for_yanCommented:
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).

0
 
for_yanCommented:
0
 
sobeservices2Author Commented:
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
0
 
for_yanCommented:
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
0
 
sobeservices2Author Commented:
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?
0
 
for_yanCommented:


If you replace
String mString = minutesString.substring(11);
with
String mString = minutesString;
in the first code

Then you'll need to have only number in the box

0
 
sobeservices2Author Commented:
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
0
 
objectsCommented:
the code in your original question was almost there
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.");

	}
}

Open in new window

0
 
for_yanCommented:

If you replace
String mString = minutesString.substring(11);
with
String mString = minutesString;
in the first code which I sent

Then you'll need to have only number in the box

0
 
sobeservices2Author Commented:
Checking that now sorry posted after you had already responded back ...

Checking that now
0
 
objectsCommented:
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.");

	}
}

Open in new window

0
 
sobeservices2Author Commented:
Thanks that worked pefectly

Could you explain a bit about the meaning behind the

String mString = minutesString.substring(11);
0
 
objectsCommented:
> String mString = minutesString.substring(11);

takes a substring of the original string start at 11th character
why did you add that?
0
 
for_yanCommented:
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.

0
 
objectsCommented:
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

0
 
for_yanCommented:
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.


0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 11
  • 9
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now