954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How do I run this code using Netbeans?

File, new project, Java+Java application, Project name just a random name, finish.

Then.. new file, Java+Java class, Class name payroll, finish..

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication5;

/**
 *
 * @author computer

public class Payroll
{
   public static void main(String[] args)
   {
      int hours = 40;
      double grossPay, payRate = 25.0;

      grossPay = hours * payRate;
      System.out.println("Your gross pay is $" + grossPay);
   }
}


Then I build it, then I run it. It does not run. I must have chosen the wrong settings or something. Asks me if I want to run it anyway, which is what I did...

then nothing happens and it just says:

run:
BUILD SUCCESSFUL (total time: 1 second)


What did I do wrong?

Torf
Light Poster
39 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

the whole program is commented out...
put */ at the line after the author

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

I still get the same result, but now it says..

"class Payroll is public, should be declared in a file named Payroll.java
public class Payroll".

But, as you can see here, the file is called payroll.

[IMG]http://img19.imageshack.us/img19/7411/javae.jpg[/IMG]

(zoom in)

Torf
Light Poster
39 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Java is case sensitive
so that should be "Public class payroll" not Payroll

either change the file name's first letter to uppercase or the public class name's first letter to lowercase

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

Alright, I should have noticed that! Thanks.

Shouldn't there be any output? It should display the System.out.println , but it dosen't. I still only get the:

"run:
BUILD SUCCESSFUL (total time: 0 seconds)"

Torf
Light Poster
39 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

I think your running javaapplication5 class instead of payroll class
...judging from the image

Edit: If your figuring this out...Left click on payroll.java on the Projects tab at the left side of netbeans then click run file
... or simply Shift + F6 :)

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

[IMG]http://img853.imageshack.us/img853/290/java1.jpg[/IMG]


Everything is closed now. I am running Payroll.java Did not think it was this difficult to get this up and running..

Edit:

Oh, you mean at the bottom in the output window.. "Output - JavaApplication5". No idea why that's there or how I change that.

Torf
Light Poster
39 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Try what I suggested a while ago
Left click on payroll.java on the Projects tab at the left side of netbeans then click run file

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

I didn't notice you added this to your post. Sorry. But I actually just did that myself. Thanks! Shouldn't it work the way I tried it? I don't understand why the F6 runs the other file.

Torf
Light Poster
39 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

F6 runs your main project
Try using Shift + F6 to run the java file on the current screen

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

or just right click in it and select 'run as'

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

Thank you! I have another problem and will just put it in here...

package javaapplication8;




public class RecursiveMultiplier
{
    public static void main(String[] args)
    {
        double num1, num2;    
        
        
        // Get the first number.
        input = JOptionPane.showInputDialog("Enter a number.");
        num1 = Double.parseDouble(input);
        
        // Get the second number.
        input = JOptionPane.showInputDialog("Okay, enter another number.");
        num2 = Double.parseDouble(input);
        
        // Show their product.
        JOptionPane.showMessageDialog(null, num1 + " times " + num2 +
                                      " equals " + multiply(num1, num2));
                                                
        System.exit(0);
    }
    
    

    public static double multiply(double x, double y)
    {
        if (x == 1)
            return y;
        else
            return y + multiply(x - 1, y);
    }
}


[IMG]http://i42.tinypic.com/6htjxs.jpg[/IMG]

The code should be fine. Is it just the way I run it or is the code not ok? It dosen't seem to run the correct file, either... Although I did do it correctly this time.

Torf
Light Poster
39 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

right click on your source file and click on run file option.

rushikesh jadha
Junior Poster in Training
89 posts since Dec 2011
Reputation Points: 4
Solved Threads: 11
 
right click on your source file and click on run file option.


brilliant thought. which is why I suggested him to do so several days ago.

@Torf: what exactly do you mean? does it do what you want/expect it to do? if not: what does it do and what do you expect it to do.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 
The code should be fine.


Those red lines found at the rightmost side of netbeans says otherwise
try to fix those issues before running theright file

PS. next time just attach the image on your post so we can view it at the thumbnail

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

you don't declare input as a String, there's your first error seems to me.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

If you want to use classes like JOptionPane from the javax.swing package then either
1. Use its fully qualified name javax.swing.JOptionPane or
2. import the class (or package) at the beginning of your program so you can use the class name without qualification

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
If you want to use classes like JOptionPane from the javax.swing package then either 1. Use its fully qualified name javax.swing.JOptionPane or 2. import the class (or package) at the beginning of your program so you can use the class name without qualification


should've seen that one ...

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

It's called teamwork. :)

ps Torf: While we're at it, if (x == 1)
It's always dangerous to rely on an exact equals with a floating-point value. You can never be certain that x isn't 0.99999999999 after a calculation whose exact result should be 1.0. It's particularly dangerous in this case because you are relying on it to terminate a recursive algorithm. Far far safer to code that as if (x <= 1)

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You