-1

I'm learning command-line in JAVA now. I'm a beginner. I did a hard code for my project but I don't know how to apply command-line.

public class SalesRep 

private String firstName;
private String lastName;
private String employeeID;
private double grossSales;
private double commissionRate;

public SalesRep(String[] args)
{
    if (args.length != 5)
        System.out.printf("Error");
    else
    {
        firstName = args[0];
        lastName = args[1];
        employeeID = args[2];
        grossSales = Double.parseDouble(args[3]);
        commissionRate = Double.parseDouble(args[4]);
    }
    if (grossSales < 0.0)
        throw new IllegalArgumentException
        ("Gross sale must be greater than or equal 0.0");
    if (commissionRate <= 0.0 || commissionRate >= 1.0)
        throw new IllegalArgumentException
        ("Comission rate must be in between 0.0 and 1.0");
}

public String getFirstName()
{
    return firstName;
}
public String getLastName()
{
    return lastName;
}
public String getEmployeeID()
{
    return employeeID;
}
public void setGrossSales(double grossSales)
{
    if (grossSales < 0.0)
        throw new IllegalArgumentException
        ("Gross sales must be greater than or equal 0.0");
    this.grossSales = grossSales;
}
public double getGrossSales()
{
    return grossSales;
}
public void setCommissionRate(double commissionRate)
{
    if (commissionRate <= 0.0 || commissionRate >= 1.0)
        throw new IllegalArgumentException
        ("Comission rate must be in between 0.0 and 1.0");
    this.commissionRate = commissionRate;
}
public double getCommissionRate()
{
    return commissionRate;
}
public double earnings()
{
    return getCommissionRate() * getGrossSales();
}
@Override
public String toString()
{
    return String.format("%s: %s %s%n%s: %s%n%s: %.2f%n%s: %.2f", 
            "Sale Representatives", firstName, lastName,
            "Employee ID", employeeID,
            "Gross Sales", grossSales,
            "Commission Rate", commissionRate);
}        

I know something wrong with command line argument. But I don't know how to fix it. Can anyone help me please?

Jin Nguyen
  • 45
  • 10
  • 3
    Did you get any errors? Do you have a `main` method? – Thilo Dec 09 '14 at 05:54
  • 1
    what is the error you are getting? you can always print out args and see in what order you are getting command line arguments – Adi Dec 09 '14 at 05:54
  • `public class SalesRepTest { public static void main(String[] args) { SalesRep Rep = new SalesRep(args); System.out.println("\nUpdated employee information obtained by toString"); System.out.printf("%n%s%n%s: $%, .2f%n%n", Rep, "Earnings", Rep.earnings()); } }` I forgot to add the test class of this. – Jin Nguyen Dec 09 '14 at 05:54
  • I got this answer, which I don't want to. Error Updated employee information obtained by toString Sale Representatives: null null Employee ID: null Gross Sales: 0.00 Commission Rate: 0.00 Earnings: $ 0.00 Sorry! I'm new to computer world – Jin Nguyen Dec 09 '14 at 05:55
  • What was the exact command line you entered? Also, are you running on Windows, Linux, Mac, or something else? – ajb Dec 09 '14 at 05:59
  • By the way, if you find you need to give us more code, please edit your question--**don't** try to give us a bunch of code in the comments. – ajb Dec 09 '14 at 06:02
  • And your code is missing the actual class definition of the SalesRep. All you printed here is the constructor + the class methods. – JohnDoe90 Dec 09 '14 at 06:04
  • @johndoe90 no, the class definition is there. The OP has a couple things to learn about the proper way to put code in a SO question. – ajb Dec 09 '14 at 06:05
  • **edit your question and put ALL the relevant code in it!** –  Dec 09 '14 at 06:05
  • You need to post all your relevant environment details in your question. You forgot to mention about NetBeans. Your problem is how to pass command line arguments in NetBeans. This is now entirely a duplicate question of - http://stackoverflow.com/questions/9168759/netbeans-how-to-set-command-line-arguments-in-java Voted to close. – Kazekage Gaara Dec 09 '14 at 06:16

2 Answers2

0

Your program is going into this condition :

if (args.length != 5)
        System.out.printf("Error");

Hence, the "Error" printed in the output. To run your program with command line arguments, use:

java SalesRepTest arg0 arg1 arg2 arg3 arg4

where arg0,...,arg4 represent your arguments.

Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
  • Thank you for your help!I'm trying it. But, somehow I get this: Sale Representatives: null null Employee ID: null Gross Sales: 0.00 Commission Rate: 0.00 Earnings: $ 0.00 Can you explain what's wrong with this? – Jin Nguyen Dec 09 '14 at 06:04
  • I used this "java SalesRep arg0 arg1 arg2 arg3 arg4" for NetBeans. Is it right? – Jin Nguyen Dec 09 '14 at 06:10
  • The other answer already pointed you to the correct direction for netbeans - http://stackoverflow.com/questions/9168759/netbeans-how-to-set-command-line-arguments-in-java – Kazekage Gaara Dec 09 '14 at 06:14
-1

Well the right Code is:

public class SalesRep{

private String firstName;
private String lastName;
private String employeeID;
private double grossSales;
private double commissionRate;

public SalesRep(String[] args)
{
    if (args.length != 5)
        System.out.printf("Error");
    else
    {
        firstName = args[0];
        lastName = args[1];
        employeeID = args[2];
        grossSales = Double.parseDouble(args[3]);
        commissionRate = Double.parseDouble(args[4]);
    }
    if (grossSales < 0.0)
        throw new IllegalArgumentException
        ("Gross sale must be greater than or equal 0.0");
    if (commissionRate <= 0.0 || commissionRate >= 1.0)
        throw new IllegalArgumentException
        ("Comission rate must be in between 0.0 and 1.0");
}

public String getFirstName()
{
    return firstName;
}
public String getLastName()
{
    return lastName;
}
public String getEmployeeID()
{
    return employeeID;
}
public void setGrossSales(double grossSales)
{
    if (grossSales < 0.0)
        throw new IllegalArgumentException
        ("Gross sales must be greater than or equal 0.0");
    this.grossSales = grossSales;
}
public double getGrossSales()
{
    return grossSales;
}
public void setCommissionRate(double commissionRate)
{
    if (commissionRate <= 0.0 || commissionRate >= 1.0)
        throw new IllegalArgumentException
        ("Comission rate must be in between 0.0 and 1.0");
    this.commissionRate = commissionRate;
}
public double getCommissionRate()
{
    return commissionRate;
}
public double earnings()
{
    return getCommissionRate() * getGrossSales();
}
@Override
public String toString()
{
    return String.format("%s: %s %s%n%s: %s%n%s: %.2f%n%s: %.2f", 
            "Sale Representatives", firstName, lastName,
            "Employee ID", employeeID,
            "Gross Sales", grossSales,
            "Commission Rate", commissionRate);
}        

public static void main(String [] args){
SalesRep s =  new SalesRep(args);
}

}

then you need to complie your code like

javac SalesRep.java

then your can run and supply commandline arguments like this:

java SalesRep arg1 arg2 arg3 arg4 arg4

Ok this will Work. you cannot supply commandline argument to constructor, it can be only for main method, but you can pass commandline argument as parameter to constructor.

Manoj Sharma
  • 596
  • 1
  • 6
  • 23
  • Yes, you can supply the command line arguments to a constructor or any other method. `args` is just a `String[]`. You can pass it to a method or do anything else that you can do with a `String[]`. And the questioner's code did that. – ajb Dec 09 '14 at 06:00
  • I'm using NetBean! Do you know how to run command line on NetBeans?BTW, thank you for your help – Jin Nguyen Dec 09 '14 at 06:03
  • 1
    Ok just go to this link : http://stackoverflow.com/questions/9168759/netbeans-how-to-set-command-line-arguments-in-java Or you can download and install Run With Arguments - plugin detail to run your project with arguments. – Manoj Sharma Dec 09 '14 at 06:06
  • You can also set commandline arguments by just right click on your project then go to properties you will find run in left pane, click on it. then supply your parameter into arguments there. Just argument nothing else. – Manoj Sharma Dec 09 '14 at 06:15
  • Jin did you got your answer ? – Manoj Sharma Dec 09 '14 at 06:37
  • I'm working on this. Can't figure out what did I do wrong. It worked with hard code but not with the command line argument. I will let you know what I'm getting. Thank you for your help. Very much!!!!!! – Jin Nguyen Dec 09 '14 at 07:05
  • It my pleasure. whenever you stuck in java just let me know. I will try my best to resolve your problem. – Manoj Sharma Dec 11 '14 at 05:40