A non well formed numeric value encountered in laravel - laravel

I am trying to add two variables together. I believe both contain an integer, but when I draw what is stored within $product->mileage, I receive the following error:
A non well formed numeric value encountered
$oilchange = $request->oilchange_at_kms;
$product = Product::find($request->product_id);
$mileage = $product->mileage; // Error within this variable, but it is an int
$total = $mileage + $oilchange;
How can I test this, or how can I find the problem in my code?

This error usually pops up when you try to add an integer with a string or some type of non numeric field.
You can test this by using the PHP gettype() method:
dump(gettype($product->mileage));
dd(gettype($oilchange));
If it turns out that one of these is a string (possibly from a form response), you can cast it to an int if you are certain that the value will always be an int.
$mileage = (int)$product->mileage;
Not really recommending this, as you should try to resolve the types within the variables first, but it may help you in testing.

Related

Getting error while access the struct type of array element as undefined (type []ParentIDInfo has no field or method PCOrderID)

I am new to golang and I have one issue which I think community can help me to solve it.
I have one data structure like below
type ParentIDInfo struct {
PCOrderID string `json:"PCorderId",omitempty"`
TableVarieties TableVarietyDC `json:"tableVariety",omitempty"`
ProduceID string `json:"PRID",omitempty"`
}
type PCDCOrderAsset struct {
PcID string `json:"PCID",omitempty"`
DcID string `json:"DCID",omitempty"`
RequiredDate string `json:"requiredDate",omitempty"`
Qty uint64 `json:"QTY",omitempty"`
OrderID string `json:"ORDERID",omitempty"`
Status string `json:"STATUS",omitempty"`
Produce string `json:"Produce",omitempty"`
Variety string `json:"VARIETY",omitempty"`
Transports []TransportaionPCDC `json:"Transportaion",omitempty"`
ParentInfo []ParentIDInfo `json:"ParentInfo",omitempty"`
So I have issue to access the PCOrderID which inside the []ParentIDInfo . I have tried below however I getting error as "pcdcorder.ParentInfo.PCOrderID undefined (type []ParentIDInfo has no field or method PCOrderID)"
keyfarmercas = append(keyfarmercas, pcdcorder.ParentInfo.PCOrderID)
Any help will be very good
Thanks in advance
PCDCOrderAsset.ParentInfo is not a struct, it does not have a PCOrderID field. It's a slice (of element type ParentIDInfo), so its elements do, e.g. pcdcorder.ParentInfo[0].PCOrderID.
Whether this is what you want we can't tell. pcdcorder.ParentInfo[0].PCOrderID gives you the PCOrderID field of the first element of the slice. Based on your question this may or may not be what you want. You may want to append all IDs (one from each element). Also note that if the slice is empty (its length is 0), then pcdcorder.ParentInfo[0] would result in a runtime panic. You could avoid that by first checking its length and only index it if its not empty.
In case you'd want to add ids of all elements, you could use a for loop to do that, e.g.:
for i := range pcdorder.ParentInfo {
keyfarmercas = append(keyfarmercas, pcdcorder.ParentInfo[i].PCOrderID)
}

Using processing and getting an unexpected token on my array. #hackathon

String[] files= {};
int[] fileNumber = {0};
String commandPromptTxt = "";
String CPTDummy = "";
String blankDummy = "";
String[] currentFile = {};
void makeFile(String[] file, int fileNum, String name1, int level1, int[]parents1, int[] children1, String type1) {
//Warning if you make a file and use the same file number more than once you will override the file
files[fileNum]= {"10"};
};
So I have that amazing piece of code in processing and I am getting an error unexpected token:{ where I say files[fileNum] = {}; also even when I enter values into the brackets I get the same error. Any ideas of a fix for this? Thanks.
Why are you including brackets at all?
The syntax you're using is an array initializer. You use it correctly here:
String[] files= {};
This initializes your files variable to an empty array. You also use the syntax correctly here:
int[] fileNumber = {0};
This initializes your fileNumber variable to an array with a single index, and in that index is the value 0.
This line is where it stops making sense:
files[fileNum]= {"10"}
First of all, you've already initialized your files variable to an array with zero indexes. That means that even if this would compile, you'd get an ArrayIndexOutOfBoundsException, because you're trying to use indexes of an array that doesn't have any.
Secondly, you're misusing the array initialization syntax. I'm pretty sure you don't want the indexes of your array to also be arrays, otherwise you'd have to make them 2D arrays.
So, to sum it up, you need to do two things:
1: Initialize your arrays to actually have indexes. Something like this:
String[] files = new String[10]; //array with 10 indexes
2: Stop misusing the array initialization syntax and just pass values into the array indexes:
files[fileNum]= "10";
You might be better off using ArraysLists instead though. Then you don't need to know how many indexes you'll have ahead of time, and you can simply call the add() function to add stuff to them.
More info can be found in the Processing reference.

Record non-scalar parameter in Omnet++

I am using a non-scalar parameter for my parameter study:
*.server.serviceTime = ${B=exponential(20ms), exponential(35ms)}
However, compared to the other scalar parameters, the B parameter is not shown in the Browse Data section of the results, which I was using until now to export the results of my parameter study:
How can I record the parameter of the exponential distribution (B) that I'm using?
The serviceTime is declared in the .ned as follows:
volatile double serviceTime #unit(s);
If I'm not mistaken you would like to record the mean value of the exponential distribution. Here is an example how the PureAlohaExperiment sample does this:
[Config PureAlohaExperiment]
...
Aloha.numHosts = ${numHosts=10,15,20}
Aloha.host[*].iaTime = exponential(${mean=1,2,3,4,5..9 step 2}s)
i.e. put the interation variable inside the exponential function.
You may put in a NED module a parameter called B. Then, you do the following in the omnetpp.ini:
**.B = ${B=exponential(20ms), exponential(35ms)}
Finally, you record the B NED parameter in the finish() function:
recordScalar("B", par("B"));
There is an option param-record-as-scalar for saving parameter as a scalar. An example of using it:
*.server.serviceTime.param-record-as-scalar = true
However, it doesn't work for volatile parameters (there is an error during finishing simulation). It seems that it is intentionally behaviour to avoid registering "meaningless" random values.
If you really need current random value of volatile parameter, you should record it as a new scalar just after reading it, for example:
double serviceTime = par("serviceTime").doubleValue();
recordScalar("serviceTime 1", serviceTime);
// ... later
serviceTime = par("serviceTime").doubleValue();
recordScalar("serviceTime 2", serviceTime);

Why/How to use passed constants in function?

I've seen classes where constants are passed to methods, I guess its done to define some kind of setting in that function. I cant find it anywhere now to try to find out the logic, so I though I could ask here. How and why do you use this concept and where can I find more information about it?
The example below is written in PHP, but any language that handles constants would do I guess..
// Declaring class
class ExampleClass{
const EXAMPLE_CONST_1 = 0;
const EXAMPLE_CONST_2 = 1;
function example_method($constant(?)){
if($constant == ExampleClass::EXAMPLE_CONST_1)
// do this
else if($constant == ExampleClass::EXAMPLE_CONST_2)
// do that
}
}
// Using class
$inst = new ExampleClass();
$inst->example_method(ExampleClass::EXAMPLE_CONST_1);
To me its more clear to pass "ExampleClass::EXAMPLE_CONST_1" than to just pass "1", but it's that the only reason to pass constant?
Simply passing 1 doesn't say much. By having a constant you can have a description about the settings in the name.
example:
constant RAIN = 1;
method setWeather(RAIN);
Atleast that's how and why I use it.
It is always a good idea to avoid literals being passed around. By assigning a name, anyone reading your code has a chance to understand what that value means - a number has no meaning. It might also help you maintaining your code: If for some requirement the value has to be changed, you can easily do it in one place, instead of checking each and every value occurrence.

string was not recognized as a valid datetime. in c#

i am using this code for passing datetime.
Convert.ToDateTime(textBox1.Text)
but the compiler show an error that string was not recognized as a valid datetime. so how to avoid this error and is a datetime field in my database.
Convert.ToDateTime(textBox1.Text)
This code will throw an exception if the string value in textBox1.Text doesn't represent a valid DateTime (or at least can't be parsed into one with default functionality in C#). You can add some defensive programming to handle errors like this.
The DateTime type (as well as most, if not all, common value types in .NET) has a method on it called TryParse() specifically for the purpose to attempting to parse a value into that type without throwing an exception. The method returns true if the parse was successful, false otherwise. And it accepts an out parameter to hold the resulting parsed value (or the original value if parsing is unsuccessful).
So instead of this:
var dateTimeValue = Convert.ToDateTime(textBox1.Text);
You could use something like this:
var dateTimeValue = DateTime.MinValue;
if (DateTime.TryParse(textBox1.Text, out dateTimeValue))
// use the value for something

Resources