public static void getClientUpdate() {
try {
String inputLine;
double version = 0;
URL url = new URL开发者_运维问答("http://ds-forums.com/client/version.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
version = inputLine;
System.out.println("The version is: " + version);
} catch (Exception x) {
System.out.println(x);
}
}
This is going to sound really stupid but I have a txt file that contains a double and I want to save it as a double. Instead it wants a string. Am I doing this wrong or is there just a few steps I need to add?
Well, you need to parse the string into a double. For example, using Double.parseDouble
. I should point out that a version string doesn't sound like a great idea to store as a double... you might want to consider using BigDecimal
instead. Doubles are generally better for "real world" physical values such as distances - not "artificial" values such as version numbers. I don't think I'd even use BigDecimal
to be honest... it's not like version numbers are usually treated in the same way as "normal" numbers.
Have you considered creating your own type to represent a version number?
You can parse the string you obtained: Double.parseDouble(inputLine.trim())
But, in fact, I don't think versions should be doubles. In your particular case, today, this might be the case, but it can easily change. Here are some examples of popular "versionings", that cannot be represented as doubles:
3.0.5
2.1.GA
1.5-SNAPSHOT
0.8b
So, I'd suggest leaving this a string.
Double.parseDouble(string) is what you're after.
精彩评论