I am new to Java and using the JIRA MISC Custom Fields add-on and require some logic assistance to solve math functions between two drop down fields.
Field one is "User Cost"
This field contains four string selections with the user price posted at the end of the string.
sam costs .21
mitch costs .419
Lance costs 2.66
xmen costs 13.338
Field two is "Usage"
This field contains two string selections:
24 hours (unless maintenance)
12 Hours (7a-7p)
The argument should be invoked into a new field called "Total User Cost." This field would automatically display the correct price for user and usage amount.
The equation blueprint would be as follows:
Cost*31(calendar days)*usage(12 || 24)
I would want my form to update based on user input selection of these two fields and other variables in my equation.
This is what I have so far:
[
Thank you in advance for any feedback!
If I understand correctly, you first need to initialize issue to something. (It looks red in your images like that variable doesn't exist)
Then, you can do something like this
double costSam = 0.21;
String userSam = issue.get("customfield_10620");
Then, if you are needing to convert or otherwise do some math on userSam, then you need this
double samTotal = costSam * Double.parseDouble(userSam);
Some flaws with your code
You have to define types for your values like String or double.
If you have String x = "hello" on one line, then x = 0.4 on the next, that won't work because of incompatible types
If you did have compatible types on consecutive lines, then the first line is pointless unless using the value from the first, as the second line overwrites the value of the first one
Related
I am writing a program for calculating pay for overtime hours and non-overtime hours. This is for my Java course, so the requirements are that I must have two classes and three functions. The first class does not have a main and contains three functions. It's basically where the calculation is done.The second class is for I/O, where you input your values through the Scanner and display the result.
I think I am almost done, however, I cannot get my total pay amount to return in my I/O class, it comes out as 0.0. How can I get it to properly return my function getPayAmount() value?
Here is the image of my first class, the three functions: constructor, payCalculation, and getPayAmount()
Here is the image of my second class, where the I/O is done.
Again, I'd like to know how I can return the value from my getPayAmount() function to print at my last sys.out statement.
Thanks so much in advance for your help, I'm so anxious to be done with this!
try creating paycheck calculation after getting the input. move line 11 to 25
PayCheckCalculation myPay= new PayCheckCalculation (hours,rate);
myPay.payCalculation(rate,hours);
When you are creating the object mypay, the parameters are rate and hours have value as 0. Instead you want to instantiante the object with the values you get as input and you should get the right answer. Also you should use this in your class methods to refer to current object.
You did not call the method paycalculation method in your code as well.
In line 11 of your main function, you're creating the mPay object with (0,0) as initial values, you could initiate the object, you get the required information from the console or user, then the variables in the object will have updated values in them.
You could create or initiate the object after line 25 so that, you will have all the required data to calculate the payroll information from the user.
To understand an error, always check the flow of your code, this way you can find most of your errors.
Is it possible to multiply subtotal with specified number in Jasper/Dynamic reports? I have sum in subtotal and I would like to have a possibility to enter discount before start report and I would like to see it in my report. Is there any way to create variable and multiply or divide subtotal?
For example:
report.subtotalsAtSummary(sbt.sum(colUnits)*myCreatedNumber);
How can I create "myCreatedNumber"? Because sbt.sum(colUnits.multiply(column)) works only with columns and I want to multiply with number.
I see that there are many people who are smart enough, to not understand what I am asking without a code sample and giving me minuses, but I will try to explain a little bit more. Lets say that I have a invoice with sum of money in subtotal. But I want to give a discount which I set manually, the discount is not stored in DataSource. How can I create variable in Dynamic/Jasper reports in Java?
ADDITION
I have tried this:
int cc = 15;
report.addParameter("par1", cc);
Integer xxx = (Integer) report.getJasperParameters().get("par1");
But when I uncomment last line report shows empty. If I add JOption pane to show xxx it shows 15, but report is still empty.
Finally I have figured it out.
I couldn't find how to insert my own nuber to the report, but there is possibility to create variables in report using custom variables, created with class methods. Good example, which helped me a lot, of creating custom variables is at dynamic reports page.
This question has been answered so please close it...
Thanks for the clarifications!!
I looked at the question above but there is an use case which we should consider before closing the issue:
I have a situation where I raise an order and the system generates a reference number as: 0000002443
I store that number as a string.
When the system sends the order out, it sends two documents. One as a requisition with the above reference number and the other as a Purchase order with a reference: 0000002444
I need to be able to store the first reference number (i.e. 0000002443) as an Integer keeping the preceding zeroes and add +1 and store as a PO reference number (i.e.0000002444) to verify the orders later.
If I keep the first reference number as a String then I won't be able to add 1 to the reference number to get the PO reference Number.
It's a Follow up question:
https://stackoverflow.com/questions/15025136/converting-string-to-integer-but-preceding-zero-is-being-removed
Integers do not have leading zeros (as it says in that other question)
You'd need to convert it to an int, add one, and then pad it back into a String:
def ref = '0000002443'
def refPlusOne = "${ref.toInteger() + 1}".padLeft( ref.length(), '0' )
Simply put, an integer doesn't have a number of leading zeroes. It doesn't even have information about whether it's decimal, hex, or anything like that. It's just an integer.
If you really need to follow your existing design, I suggest you parse it as an integer, add one, and then repad with as many zeroes as you need to get back to the original length.
To be honest, if it's really just meant to be a number, it would be better if you stored it as a number instead of using a string at all.
I've been reading up on it, but every question I've found has asked for slightly different things, such as only wanting a single letter for their array, or in a different language (I'm new and only learning java at the moment), so here I am.
I want to set up an array that uses the user's input for their names.
What I have so far is this, I'm assuming this is the declaration line, where later I use an input line to define a value within the array (which I also am unsure how to do)
String[] array = {"name"};
But I don't know how to for example print.out the object or keep up with which name will be what value. I appreciate your time taken to teach me!
EDIT for further clarification. I'm trying to write up a small app that asks the user for numerous names, addresses, and phone numbers (Type name -> Type name's address -> type name's phone number, ask if they want to add another person, if yes then go back to asking for another name)
I am unsure how to set up a String array or how to use it throughout. However, thanks to your input and coming back after some fresh air, I have a better idea how to word it for google. Thank you guys for your help, even if it was just to gesture a better articulated question.
An array is a sequence of values. You have created an array of Strings that is one String long. To access the value at a specific of an array, use array subscript notation: the name of the array followed by a pair of square brackets ([]) with the index in between them.
String[] anArrayOfStrings = {"string0", "string1", "string2"};
anArrayOfStrings[0]; //the first element
System.out.println(anArrayOfStrings[1]); //print the second element
anArrayOfStrings[2] = "new string value"; //assign the third element to a new value
if (anArrayOfStrings[0].equals("string0") //evaluate the first element and call a method
{
//this block will execute anArrayOfStrings[0] is "string0"
}
anArrayOfStrings[3]; //error, index out of bounds
Simply declaring the array would be
String[] names;
In your code you both declare and assign it in the same line by using an initializer list.
To assign individual elements, use the [] notation. Note that once you initialized you list to be only one String long, it cannot become longer than without be re-assigned. To declare an array of any size, you can use:
String[] arrayWithInitialSize = new String[5]; //holds five strings, each null to begin with
I've been reading up on the net about the issues with handling float and double types in java. Unfortunately, the image is still not clear. Hence, i'm asking here direct. :(
My MySQL table has various DECIMAL(m,d) columns. The m may range from 5 to 30. d stays a constant at 2.
Question 1.
What equivalent data-type should i be using in Java to work (i.e store, retrieve, and process) with the size of the values in my table? (I've settled with double - hence this post).
Question 2.
While trying to parse a double from a string, i'm getting errors
Double dpu = new Double(dpuField.getText());
for example -
"1" -> java.lang.NumberFormatException: empty String
"10" -> 1.0
"101" -> 10.0
"101." -> 101.0
"101.1" -> 101.0
"101.19" -> 101.1
What am i doing wrong? What is the correct way to convert a string to a double value?
And what measures should i take to perform operations on such values?
EDIT
This is the code -
System.out.println(dpuField.getText());
Double dpu = new Double(dpuField.getText());
System.out.println(dpu);
Yes, the problem lies with getText() reporting the wrong value of the dpuField.
This method is called on the JTextField keyTyped event. So what's going wrong here?
EDIT 2
Looking at :
http://journals.ecs.soton.ac.uk/java/tutorial/post1.0/ui/keylistener.html
Apparently, keyTyped() does not give me the keycode. I'll have to switch to keyRealeased()
What equivalent data-type should i be using in Java to work (i.e store, retrieve, and process) with the size of the values in my table? (I've settled with double - hence this post).
Since it's a DECIMAL field, you should prefer java.math.BigDecimal. You can store it in DB using PreparedStatement#setBigDecimal() and you can retrieve it from DB using ResultSet#getBigDecimal().
While trying to parse a double from a string, i'm getting errors
This can't be true. The problem lies somewhere else. Maybe it is just not returning the data you expect to be returned or you are not using/debugging the values you expect them to be.
if you need exact precision without rounding errors, you should use a BigDecimal.
Your code looks OK - could it be that dpuField.getText() somehow cuts the last character from the string values you list above?
Update: you say
Yes, the problem lies with getText() reporting the wrong value of the dpuField. This method is called on the JTextField keyTyped event.
Could it be that getText() returns the value of the field before the last typed key is actually appended to it?
For decimal, I believe you risk losing precision if you don't use a BigDecimal on the Java side, as some decimal fractions can't be stored as a binary fraction.
Prefer Double.valueOf(String) over the constructor, but that's a valid way. Something else must be going on (i.e. I doubt those are the actual String values you're passing in).
Question1: It's bad idea to map DECIMAL columns to Double, usually the BigDecimal is the correct type. http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html#1055175
Question 2: You are doing something wrong; print the String value before converting.