Java Reflection passing parameters - java

HI,
I have methods each of them requires integer ,string respectively. I read the inputs from my xml file. I will not be aware of what the type of inputs it will be. I am using reflection to invoke the method. I read the xml and store it as string. I invoke the method by passing in the parameter. One of the method expects an integer, but I pass in string. When I try to do the getType and cast, it is throwing class cast exception.
Anyhelp would be appreciated.
Thanks,
Priya.R

Java is strongly typed language. You can not pass a string to integer expecting method. You should convert string to integer, you can use Integer.parseInt() ..

If all inputs are Strings in the XML file, then there really is no difference between an XML file and a normal text file, is there?
The main problem is the representation of data types: you are not using XML as it's meant to be. XML files should represent the particular data type an input has. For example, a person's age should be represented as an int. You lose type semantics when you encode everything as a String.
As for actual code, use the XMLEncoder and XMLDecoder java classes located here and here, respectively.
Basically, you'll do something like:
XMLEncoder encoder = new XMLEncoder();
XMLDecoder decoder = new XMLDecoder();
Encoding (aka: Storing the data to the XML File)
- Write the first input as an integer type (encoder.writeInt(someIntValue))
- Write the second input as a String: encoder.writeString(someStrValue)
- etc
When decoding, you decode an integer first, then a String, etc.

Related

WriteUTF cannot be applied to (int)

for (Account accountObj : accounts) {
System.out.print(accountObj);
account.writeUTF(accountObj.getAccountHolderName());
account.writeUTF(accountObj.getAccountType());
account.writeUTF(accountObj.getBalance());
account.writeUTF(accountObj.getAccountNumber());
}
'writeUTF(java.lang.String)' in 'java.io.DataOutputStream' cannot be applied to '(double)'
'writeUTF(java.lang.String)' in 'java.io.DataOutputStream' cannot be applied to '(int)'
so, I got these two errors, and I even tried parseInt and double before storing the values in the ArrayList yet, am a little confused, some help would be kind : )
If you take a look at the docs of that class, you can see that each type has its own method for writing it to the DataOutputStream.
That means to write a double, use the writeDouble method and the writeInt method for int values.
The UTF in writeUTF(String) hint at the fact that the String will be written to the stream in a specific encoding. A UTF encoding only makes sense for writing Strings.

Can i pass a list as a parameter to a method in Scala/Java which is defined this way detectFormatAutomatically(delimitersForDetection:Char*)

I'm creating a library to help correct CSV, in order to do it, I'm using univocity parser library.
I'm using the CSVParserSettings class method detectFormatAutomatically which is defined this way
detectFormatAutomatically(delimitersForDetection:Char*)
Is there any way I can pass a scala list or any list as an argument in order to not having to define this chars in the code itself.
I should be able to pass a string with the chars via Linux terminal and then parse it to a list of chars in my Scala code. The problem is that I get the list of chars but I cannot pass it as an argument to the method.
My code:
val settings = new CsvParserSettings()
val list = List(',',';',':','|')
settings.detectFormatAutomatically(list)
The error is: Type mismatch, expected: Char, actual: List[Char]
Is there any way I can get what I want to do.
Use
settings.detectFormatAutomatically(list:_*)

Writing multiple data types into a pre-existing file in eclipse

Okay so I'm making a making a car parking system and am trying to store data into a pre-existing text file to read at a later date.
Storing the data works fine, using the code
Files.write(Paths.get("FilePath.txt"),"\r\nhello".getBytes(), StandardOpenOption.APPEND);`
However, when I try to store multiple data types for example:
Files.write(
Paths.get(
"C:\\Users\\A612475\\Desktop\\Project1\\TextFiles\\TicketData.txt"),
"\r\nhello" + regNo.getBytes(),
StandardOpenOption.APPEND
);`
with regNo being declared as a string elsewhere, I get the following message:
The method write(Path, byte[], OpenOption...) in the type Files is not applicable for the arguments (Path, String, StandardOpenOption)
I can store the data in the string by writing the File.write method out dozens of times, but is there a more practical way to do this?
EDIT: Solution was found here: How to append text to an existing file in Java
The problem here is simple, instead of passing a byte array you are passing a string. "\r\nhello" + regNo.getBytes() Is acts as a simple concatenation operation and thus what you are getting is a string, Thus this error.

Convert String to model or Statement in jena using Java program?

I made a program for RDF by using jena in java... I have to return the result in string format.. and then in other function i have to get it as a string format and convert it to either model or statement.... Is that possible... If so how to do that... could some one help me with a sample code...
Thanks in advance
If the RDF you want to serialize is less than your complete model, then create a temporary memory model and copy into it the statements to want to write. Use Model.write to convert those statements to a string (in RDF/XML, Turtle or N-triples format). When you want to load a string containing RDF, create a java.io.StringReader object containing your string and pass that to the Model.read method.
It may be important to note that, according to the latest JavaDoc, the two Model.read() methods that take a Reader as a method parameter all say "Using this method is often a mistake.". I do not know why the JavaDoc says that, but it does. An alternative that I am using is to pass in an InputStream, as shown (where 'is' is the InputStream):
// read(InputStream in, String base, String lang)...
memModel.read(is, null,"TTL");
If you need to turn a String into an InputStream first, you can use:
InputStream is = new ByteArrayInputStream( str.getBytes() );

Why can I only convert a Representation to a string once in RESTlet?

So,
I'm trying to convert a Representation to a String or a StringWriter either using the getText() or write() method. It seems I can only call this method once successfully on a Representation... If I call the method again, it returns null or empty string on the second call. Why is this? I'd expect it to return the same thing every time:
public void SomeMethod(Representation rep)
{
String repAsString = rep.getText(); // returns valid text for example: <someXml>Hello WOrld</someXml>
String repAsString2 = rep.getText(); // returns null... wtf?
}
If I'm "doing it wrong" then I'd be open to any suggestions as to how I can get to that data.
The javadocs explain this:
The content of a representation can be
retrieved several times if there is a
stable and accessible source, like a
local file or a string. When the
representation is obtained via a
temporary source like a network
socket, its content can only be
retrieved once.
So presumably it's being read directly from the network or something similar.
You can check this by calling isTransient(). If you need to be able to read it multiple times, presumably you should convert it to a string and then create a new Representation from that string.
It's because in general the Representation doesn't actually get read in from the InputStream until you ask for it with getText(), and once you've asked for it, all the bytes have been read and converted into the String.
This is the natural implementation for efficiency: rather than creating a potentially very large String and then converting that String into something useful (a JSON object, a DOM tree, or whatever), you write your converter to operate on the InputStream instead, avoiding the costs of making and reading that huge String.
So for example if you have a large XML file being PUT into a web service, you can feed the InputStream right into a SAX parser.
(As #John notes, a StringRepresentation wraps a String, and so can be read multiple times. But you must be reading a Request's representation, which is most likely an InputRepresentation.)

Categories