Results 1 to 16 of 16
Thread: Java String to Int
- 04-27-2011, 10:08 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 14
- Rep Power
- 0
Java String to Int
I know, the title sounds like I'm asking for "Integer.parseInt(str);" but, that's not what I'm asking for!
I don't understand how this isn't working, but here's my problem.. I've got this code to get the 'version' of my program from my website, which works perfectly fine and returns 2. It's supposed to check if 'str' which is my String of what's returned from my website (nr-gold.com/version.txt), is equal to currentVersion which is 2.
The problem is, when I attempt to parseInt(str) which str is a String of 2, it's saying that str is an int, when it's clearly a string, so it won't parseInt it. I've also tried making currentVersion a string then doing if str.equals(currentVersion), and that did not work. Here is my code:
Now obviously that's not the entire code, just the code needed for what I'm asking.Java Code:public int currentVersion = 2; public int str2 = -1; try { URL url = new URL("http://www.nr-gold.com/version.txt"); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String str; while ((str = in.readLine()) != null) { // System.out.println("Current version: "+str+". Your version: 2."); str = in.readLine(); //Integer.parseInt(str); int str2 = Integer.valueOf(str); } in.close(); } catch (MalformedURLException e) { } catch (IOException e) { } if (str2 == currentVersion){
Thanks so much for any help you can give me, I'm quite new to Java :)
- 04-27-2011, 10:15 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
when I attempt to parseInt(str) which str is a String of 2, it's saying that str is an int, when it's clearly a string, so it won't parseInt it.
It's clearer if you post exact and entire compiler messages (or runtime exceptions it isn't clear) along with some indication of which line(s) in your code they are referring to.
ANyway, you declare two str2 variables. Do you mean to?
- 04-27-2011, 10:19 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 14
- Rep Power
- 0
It doesn't have an error compiling, it has an error whilst running.
Yes, I meant to have 2 str variables, because I was just testing other methods.
Error:
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.valueOf(Unknown Source)
at begin.start(begin.java:52)
at begin.createConnection(begin.java:87)
at begin.createConnection(begin.java:92)
at begin.createConnection(begin.java:92)
at begin.<init>(begin.java:196)
at begin.main(begin.java:352)
The createConnection doesn't have an error, it has to do with the parseInt and valueOf, when I comment out the valueOf it runs fine.
- 04-27-2011, 10:20 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 14
- Rep Power
- 0
Sorry, double posted
- 04-27-2011, 10:23 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Also you might get a clearer idea of what is going on if your output both of the strings you read each time around the while loop.
And Integer.valueOf() - like Integer.parseInt() - will throw a NumberFormatException which you should do something about.
- 04-27-2011, 10:27 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 14
- Rep Power
- 0
I've done that, they both appear exactly as "2".
Simply 2, nothing else, but one of them is an int and the other is a String/int or something that I cannot compare the two with. I did if(str == currentVersion) which were both output as 2, and it appeared as if they are not the same, because it went to the else.
Do you understand what I'm saying?
Here, give me a moment and I'll show you the output.
- 04-27-2011, 10:30 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
OK so str2 is null when you attempt to parse it. (end of stream?) Did you understand my point that you are reading two lines each time around the while loop?
I'm still a bit mystified by the int value of str2 (the one in the while loop). You go to all that effort to obtain it ... but then do nothing at all with the value.
- 04-27-2011, 10:36 PM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
I've done that, they both appear exactly as "2".
The runtime won't lie to you. str is null when you parse it, not "2".
I did if(str == currentVersion) which were both output as 2, and it appeared as if they are not the same, because it went to the else.
I don't really understand this. But "str==currentVersion" won't compile: try it to see why. And "str2==currentVersion" - which is what you originally posted - is irrelevent because that str2 is not the one in the while loop.
- 04-27-2011, 10:39 PM #9
Member
- Join Date
- Apr 2011
- Posts
- 14
- Rep Power
- 0
Ah, for some reason str is null now. I don't understand why, same exact code that was returning it as 2 before.
Here's the code:
Java Code:try { URL url = new URL("http://www.nr-gold.com/version.txt"); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String str; while ((str = in.readLine()) != null) { str = in.readLine(); System.out.println("Current version: "+str+". Your version: "+currentVersion); //int str2 = Integer.valueOf(str); } in.close(); } catch (MalformedURLException e) { } catch (IOException e) { }I don't see why it's returning null now, do you see anything wrong with my code?Java Code:Current version: null. Your version: 2
Last edited by ForeverP; 04-27-2011 at 10:43 PM.
- 04-27-2011, 10:45 PM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
You're still not printing both of the strings you read.
What is the output of
Java Code:while ((str = in.readLine()) != null) { [color=blue]System.out.println("read " + str);[/color] str = in.readLine(); [color=blue]System.out.println("read " + str);[/color] System.out.println("Current version: "+str+". Your version: "+currentVersion); //int str2 = Integer.valueOf(str); }
- 04-27-2011, 10:50 PM #11
Member
- Join Date
- Apr 2011
- Posts
- 14
- Rep Power
- 0
Ah, sorry I didn't understand what you were saying.
Great! Now it returns as 2, I removed the str = in.readLine();
Now, my problem still remains. I cannot compare "str" to "currentVersion". I tried without Integer.parseInt(str); and with it, and neither worked, though they both said "Current version: 2".
Here's my code if you want to see it.
Java Code:try { URL url = new URL("http://www.nr-gold.com/version.txt"); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String str; while ((str = in.readLine()) != null) { System.out.println("Current version: "+str+". Your version: "+currentVersion); //Integer.parseInt(str); - Did not work. } in.close(); } catch (MalformedURLException e) { } catch (IOException e) { } if (str == currentVersion){
- 04-27-2011, 10:53 PM #12
Member
- Join Date
- Apr 2011
- Posts
- 14
- Rep Power
- 0
Extra info:
I attempted to compare them as strings, though it won't compile.
Java Code:public String currentVersion = "2";
Java Code:begin.java:57: int cannot be dereferenced if (str.equals(currentVersion)){ ^ 1 error Press any key to continue . . .
- 04-27-2011, 11:04 PM #13
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
It's OK I think to forget parsing. After all version "numbers" are really strings, and often have forms that can't be parsed as ints ("1.0.2b" or whatever).
There are two things wrong with "if(str==currentVersion)". The first is that you don't compare strings (or other obejcts) with ==. Use the .equals() method instead. Ie
Java Code:if(str.equals(currentVersion)) {
The reason is that two different words (like the nineth and the twelveth in this sentence) can be equal.
The other thing is that you have two str variables. One of them is declared in the try block and it only exists within that try block. This is a general point about scope in Java. Once you declare something after a { it only exists until the next matching }. If that code compiles at all it can only be because you have another str variable declared somewhere: possibly a global one like the public int str2 in your original post.
So move the "String str" declaration to before the start of the try block. Then it will be available for the if statement. Also give it a nonnull value: otherwise "str.equals(...)" may fail. An empty string might be a good choice.
- 04-27-2011, 11:08 PM #14
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
I attempted to compare them as strings, though it won't compile.
Our posts are crossing. If you are comparing as strings then currentVersion should be changed so that it's a string.
Java Code:public String currentVersion = "2";
Also you need to fix the scoping issue I mentioned: you evidently have an int variable str declared somewhere.
- 04-27-2011, 11:26 PM #15
Member
- Join Date
- Apr 2011
- Posts
- 14
- Rep Power
- 0
Java Code:public void start(){ String str; try { URL url = new URL("http://www.nr-gold.com/version.txt"); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); while ((str = in.readLine()) != null) { System.out.println("Current version: "+str+". Your version: "+currentVersion); } in.close(); } catch (MalformedURLException e) { } catch (IOException e) { } //if (str.equals(currentVersion)){ if(str==currentVersion){There is not another str anywhere besides what you see thereJava Code:begin.java:57: variable str might not have been initialized if(str==currentVersion){ ^ 1 error Press any key to continue . . .
Yes, there was public int str; but I removed that
- 04-28-2011, 01:31 AM #16
Member
- Join Date
- Apr 2011
- Posts
- 14
- Rep Power
- 0
Thanks for your help pbrock, though I found the solution myself! Here's the code if anyone's interested
Java Code:public void checkVersion(){ try { double newVer2 = -1; String newVer; URL url = new URL("http://www.nr-gold.com/version.txt"); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); while ((newVer = in.readLine()) != null) { newVer2 = Double.parseDouble(newVer); System.out.println("Current version: "+newVer2+". Your version: "+currentVersion+"."); if(newVer2 == currentVersion){ sM("You have the latest client!"); start(); } else { sM("Your client is out of date, please download the new version!"); } } in.close(); } catch (MalformedURLException e) { } catch (IOException e) { } }
Similar Threads
-
arrange(java.lang.String[][]) in Test cannot be applied to (java.lang.String) arrange
By prizzly in forum New To JavaReplies: 4Last Post: 01-01-2011, 10:52 AM -
Java String
By levent in forum Java TutorialReplies: 6Last Post: 05-14-2010, 09:26 AM -
Java String
By ooopppsss in forum New To JavaReplies: 3Last Post: 06-25-2009, 10:19 AM -
Error: cannot resolve symbol' on Person (java.lang.String, java.lang.String)
By baltimore in forum New To JavaReplies: 2Last Post: 09-18-2008, 07:30 AM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks