I am working with Xamarin.
And this is the problem. One of the function receives Java.Lang.String as its argument and I have String of C#. I need to typecast C#'s String into Java's String.
How can I do that? Any suggestions?
Thanks.
solution:
string a = "some_text";
Java.Lang.String javaA = new Java.Lang.String(a);
What you need to do is to convert the C# string to a neutral type existing in both c# and Java. so, you can either use a byte array some type of sql type (like a CLOB), or a mechanism similar to serialisation/deserialisation.
Here is the answer
This is how we can convert C# String into Java String.
Thanks for every answer.
They all helped in the right direction.
http://forums.xamarin.com/discussion/18654/better-way-to-convert-string-to-java-object
Related
C method _strtod_l can convert string "13.361389" to double value 13.361389000000001 , is there equivalent in C# and Java also can get the same result? I found the Convert.ToDouble in C# cannot do it.
For C# you can use Double.Parse("13.361389", CultureInfo.InvariantCulture)
For Java you can use Double.parseDouble("13.361389")
We have a class named as Show.
Can anybody tell me how to convert data which is String into generic type? For example if we have
String sn=null;
and we want to cast it into Show then how we can do it?
your question is really very unclear. but if you are talking about converting data in String format to primitive data types then you can use parser. like if you want to convert "9" which is basically a string to int then you can use
int i = Integer.parseInt("9");
In java when we take input from console we get a String, even if we want an integer as input we get a input in String format, then we covert it in integer format using several methods, like Integer.parseInt(). Where as C/C++ also take input from console but there we get integer values directly from console we does not require methods to convert them. Then why does java follows such long procedure. **What is the reason behind such an architecture of Java ?
//In java we follow the following process
public static void main(String args[])
{int i = Integer.parseInt( args[0]);// here we get input in String format and then convert it
}
//In C++ we follow the following :
void main()
{int i;
cin>>i;
}
Both C/C++ and Java takes input form Console then why java takes it in String Format and C++ does not ??
Check out java.util.Scanner, it might do what you need:
http://download.oracle.com/javase/6/docs/api/java/util/Scanner.html
Java - If you use the Scanner class you can get the input in the required data type. It's not only String java accepts.
If you take an integer from the console in C, it will require you to atoi() the input.
You always get a string when reading from stdin. In C++ however, the >> operator overload used when writing to an int performs the conversion - so it's just a different way of converting but in both languages it's necessary.
You are right that it's nicer in C++ though since you can use pretty much the same code no matter what data type you have.
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.
Here is the java code
usageType = (String) c.getSrcValue("USAGETYPE");
c is a arraylist.
I populate it with this field from DB.
"USAGETYPE" NUMBER(*,0),
I get the following error
java.lang.ClassCastException: java.math.BigDecimal cannot be cast to String
Can you please help me out
Well, you cannot convert an object to a string by casting. Not in Java, in any case.
Try
usageType = c.getSrcValue("USAGETYPE").toString();
That is, if you actually need it as a string, which smells a little dubious in the first place. Usually the only place where numbers are needed as strings is the UI and you've got appropriate other places to do that conversion, normally (e.g. CellRenderers in Swing).
Simply write
usageType = c.getSrcValue("USAGETYPE").toString();
or
usageType = ""+c.getSrcValue("USAGETYPE");