Convert Data property value to Integer value - jena

I have Data property value "Physics_Preferred_Category" which contains integer value 1,2 etc.
I want to get this value and perform calculations later on it. But it gives me exception/error.
OntProperty phypref=model.getOntProperty(ns+ "Physics_Preferred_Category");
RDFNode myValue =indiv.getPropertyValue(phypref);
Literal phliteral= model.createTypedLiteral(myValue);
int phsum=phliteral.getInt(); //error highlight this line
int calculate= phsum+1;

Related

'String?' can not convert to 'String' in dart

A value of type 'String?' can't be assigned to a variable of type 'String'.
Try changing the type of the variable, or casting the right-hand type to 'String'.
This error message i got when i run this code.
This is a simple user input code on dart.
var person = ['abc', 'qwe', 'dfg'];
stdout.write('Enter Index : ');
String p = stdin.readLineSync(); //Error
int per = int.parse(p);
per > person.length
? stderr.write('Index does not exist')
: stdout.write('Person ${person[per]}');
}
Seems like readLineSync() returns nullable type. But you declare variable p as non-nullable. Either declare p as nullable: String? instead of String or make readLineSync() return default value if null:
String p = stdin.readLineSync() ?? "";// will return empty String if method readLineSync() returns null.
First of all check the null safety documentation, you'll learn everything you need to know
https://dart.dev/null-safety/understanding-null-safety
readLineSync returns a value of Type String? Meaning the return value must be a nullable String.
p is of type String and therefore expects a String (not a nullable String). the trick here is to cast stdin.readLineSync() to String:
String p = stdin.readLineSync() as String;
String p = stdin.readLineSync()!; #shorthand syntax
On top of that, your code needs some improvements. What if p can't be cast into an integer? One way to handle this is to add a try block and catch any FormatException.

Understanding difference between int? and int (or num? and num) [duplicate]

This question already has answers here:
What is Null Safety in Dart?
(2 answers)
Closed 1 year ago.
After defining a map (with letters as keys and scrabble tile scores as values)
Map<String, int> letterScore //I'm omitting the rest of the declaration
when I experiment with this function (in DartPad)
int score(String aWord) {
int result = 0;
for (int i = 0; i < aWord.length; ++i) {
result += letterScore[aWord[i]];
}
return result;
}
I consistently get error messages, regardless of whether I experiment by declaring variables as num or int:
Error: A value of type 'int?' can't be assigned to a variable of type
'num' because 'int?' is nullable and 'num' isn't [I got this after declaring all the numerical variables as int]
Error: A value of type 'num' can't be returned from a function with
return type 'int'.
Error: A value of type 'num?' can't be assigned to a variable of type
'num' because 'num?' is nullable and 'num' isn't.
I understand the difference between an integer and a floating point (or double) number, it's the int vs int? and num vs num? I don't understand, as well as which form to use when declaring variables. How should I declare and use int or num variables to avoid these errors?
Take this for example:
int x; // x has value as null
int x = 0; // x is initialized as zero
Both the above code are fine and compilable code. But if you enable Dart's null-safety feature, which you should, it will make the above code work differently.
int x; // compilation error: "The non-nullable variable must be assigned before can be used"
int x = 0; // No Error.
This is an effort made from the compiler to warn you wherever your variable can be null, but during the compile time. Awesome.
But what happens, if you must declare a variable as null because you don't know the value at the compile time.
int? x; // Compiles fine because it's a nullable variable
The ? is a way for you tell the compiler that you want this variable to allow null. However, when you say a variable can be null, then every time you use the variable, the compiler will remind you to check whether the variable is null or not before you can use it.
Hence the other use of the ?:
int? x;
print(x?.toString() ?? "0");
Further readings:
Official Docs: https://dart.dev/null-safety/understanding-null-safety
Null-aware operators: https://dart.dev/codelabs/dart-cheatsheet

Passing Object.Attr to function

Usually, a reference to an Object's Attribute Value returns that value.
Object o = current Object
display o."Object Text"
However, if I pass that reference to a function that expects a string parameter, I get an error.
string displaySomeString(string s) {
display s
}
Object o = current Object
displaySomeString(o."Object Text")
I get this result from the debugger:
-E- DXL: incorrect arguments for function (displaySomeString)
-I- DXL: All done. Errors reported: 1. Warnings reported: 0.
What gives? How do I robustly pass an Attribute value into a function?
Here's my suspicion. If you're passing the object attribute value directly in the function call--
displaySomeString(o.attr)
--instead try passing it with an empty string at the end:
displaySomeString(o.attr "")
Or set the attribute value as something like
string v = o.attr
and then pass v in as
displaysomeString(v)
and I think it might work. o.attr really isn't a string, but a derived type, and concatenating an empty string at the end casts it to a string.

Calling sqlite3_column_type returns SQLITE_INTEGER for a STRING column that contains only numbers causing an integer overflow

I'm working on a legacy project and have found an issue. The code is reading from an sqlite database. It performs a query and iterates over the statement and then for each column it gets the column name and the column value.
To get the value it calls sqlite3_column_type. Then it checks the types and uses sqlite3_column_int, sqlite3_column_double, sqlite3_column_text, etc. to read the value.
The issue is that there is a row in the DB that has a STRING column but the value contains only numbers (the value was set as 12 characters long, something like #"123456789012"). Calling sqlite3_column_type for this row returns SQLITE_INTEGER as it's only looking at the value of the column and not the declared column type. This means that the code will then read the value with sqlite3_column_int and I'll end up with an integer overflow and a number nothing like the original value contained in the DB.
Here is a quick example:
int columnType = sqlite3_column_type(statement, index);
if (columnType == SQLITE_INTEGER) {
//use sqlite3_column_int(statement, index)
}
else if (columnType == SQLITE_FLOAT) {
//use sqlite3_column_double(statement, index)
}
else if (columnType == SQLITE_TEXT) {
//use sqlite3_column_text(statement, index)
}
///etc...
How can I make sure the the value I get out from a STRING column containing only numbers is treated correctly? Can I somehow check the declared column type (other than querying and parsing out the original create table sql)? Is there a better way to do this?
Using column_decltype instead of column_type means that I get that declared column type (STRING) instead of the type of value. Then I can compare the result against STRING, TEXT, etc.to figure out how to read the value.

"Cannot subscript a value of type '[String]' with an index of type 'String'

I'm trying to display a dynamically updated array in a label using:
for i in result {
outputLbl.text = result[i].joinWithSeparator("\n")
}
However I get the error
Cannot subscript a value of type '[String]' with an index of type 'String'.
Any idea how I can fix this?
Note that when using the loop "header" for X in Y, you don't get the indices of Y, but the actual elements of Y. Judging from your error, results is an array of strings ([String]). Hence, i in you for loop represents---one by one---the elements in the String array results.
So, if you wanted to access the string elements one by one in the for loop above, you could use the approach in your example:
let result = ["Hello", "World"]
for myString in result {
// use each string element in some manner...
}
However, as you are using the array method joinWithSeparator(..), you should use, just as Leo writes in his comment above, this method directly on your array (and not their elements!)
let result = ["Hello", "World"]
outputLbl.text = result.joinWithSeparator("\n")
/* Hello\nWorld */
I think what you are doing here is trying to iterate through an array of string and then update a label that is in this case "outputLbl".Here you can do something like this
for i in result {
//result is array of strings.
// here i is individual element of result array
/* outputLbl.text = result[i].joinWithSeparator(ā€œ\nā€)*/
//instead you can write
outputLbl.text = i.joinWithSeparator(ā€œ\nā€)
}
The reason you are getting this error is as follows:
It seems you are confusing the type of i. The variable result is of type [String] which means it is an array of String types. By virtue of being an array, it must be subscripted with an Int, not a String. So something like result[0] or result[12] is valid, but something like result["hello"] is not valid. The variable i here is a String because it is a single element in an array of String types, which means that effectively, what you're trying to do by saying result[i] is something along the lines of result["hello"].
That having been said, the true solution to your problem is that the method joinWithSeparator(_:String) is not a String method but rather a Sequence type method. Which means it should be called on a Sequence like an object of type [String]. So what you should use is:
outputLbl.text = result.joinWithSeparator("\n")
What's going on here is the compiler is inferring i to be of type String since that's what result is. You should be more verbose in your naming conventions.
for specificString in result {
outputLbl1.text += "\n\(specifcString)"
}
EDITED for correctness.

Resources