• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 441
  • Last Modified:

How do I declare a variable in java.

Here is what I need to do. I am tying to create my first java program. I need to declare a variable named minutes.
Minutes are the minutes worked on a job.
I then need to display the hours and mins as well.

Any suggestions helps
I also need to make this interactive so that I can put the mins in myself and the have the results apear.
0
sobeservices2
Asked:
sobeservices2
  • 6
  • 5
4 Solutions
 
CEHJCommented:
int for both
0
 
cmalakarCommented:
You can int, long or short data types.

for ex...

int minutes, hours ;

if you want to assign some value, then

minutes = 300;
0
 
sobeservices2Author Commented:
So the start of my code what look like what?

int minutes ='300;
int hours ='2;

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
cmalakarCommented:
no '

int minutes =300;
int hours =2;

Can minutes be > 300 ?

0
 
sobeservices2Author Commented:
Minutes can be anything

so I can start off like this

public class Time
{
      public static void time(String[] args)
      {
               System.out.println("Minutes worked on a job");
0
 
cmalakarCommented:
>>public static void time(String[] args)

replace time with main, thats where a java program starts.

public static void main(String[] args)
0
 
sobeservices2Author Commented:
Hows that look?

public class main
{
     public static void main(String[] args)
      {
               System.out.println("Minutes worked on a job");
               int timeInMinutes = 5;
               System.out.print(timeInMinutes);

Open in new window

0
 
cmalakarCommented:
looks good
0
 
sobeservices2Author Commented:
Now I need to figure out how to convert the mins to hours and display it for example if I were to put in

130 mins. I need to display that in like 2hr and 10 mins
0
 
sobeservices2Author Commented:
How about this ?
The below returned some erroes but can't figure it out.

time.java:9: ';' expected
                  (null, "This is the total time worked" + timeInMinutes + " days")
                                                                                   ^
time.java:11: reached end of file while parsing
            System.out.print(timeInMinutes);
import javax.swing.JOptionPane;
public class main
{
	public static void main(String[] args)
	{
		int timeInMinutes = 5;          
		JOptionPane.showMessageDialog(null, "" + timeInMinutes);
		JOptionPane.showMessageDialog
			(null, "This is the total time worked" + timeInMinutes + " days")
		System.out.println("Minutes worked on a job");
		System.out.print(timeInMinutes);

Open in new window

0
 
cmalakarCommented:
>> time.java:9: ';' expected
     
Very clearing saying you need a ";" at end of line 9

>> time.java:11: reached end of file while parsing
            System.out.print(timeInMinutes);

And when you open a opening brace "{", you must close that with "}" at appropriate place.
Look at "Blocks of Code" section in the following page.

http://www.learn-java-tutorial.com/Java-Basics.cfm

0
 
sobeservices2Author Commented:
got it thanks.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now