Results 1 to 16 of 16
  1. #1
    ForeverP is offline Member
    Join Date
    Apr 2011
    Posts
    14
    Rep Power
    0

    Default 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:

    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){
    Now obviously that's not the entire code, just the code needed for what I'm asking.

    Thanks so much for any help you can give me, I'm quite new to Java :)

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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?

  3. #3
    ForeverP is offline Member
    Join Date
    Apr 2011
    Posts
    14
    Rep Power
    0

    Default

    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.

  4. #4
    ForeverP is offline Member
    Join Date
    Apr 2011
    Posts
    14
    Rep Power
    0

    Default

    Sorry, double posted

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  6. #6
    ForeverP is offline Member
    Join Date
    Apr 2011
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    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.
    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.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  9. #9
    ForeverP is offline Member
    Join Date
    Apr 2011
    Posts
    14
    Rep Power
    0

    Default

    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) {
    }
    Java Code:
    Current version: null. Your version: 2
    I don't see why it's returning null now, do you see anything wrong with my code?
    Last edited by ForeverP; 04-27-2011 at 11:43 PM.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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);
    }

  11. #11
    ForeverP is offline Member
    Join Date
    Apr 2011
    Posts
    14
    Rep Power
    0

    Default

    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){

  12. #12
    ForeverP is offline Member
    Join Date
    Apr 2011
    Posts
    14
    Rep Power
    0

    Default

    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 . . .

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  15. #15
    ForeverP is offline Member
    Join Date
    Apr 2011
    Posts
    14
    Rep Power
    0

    Default

    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){
    Java Code:
    begin.java:57: variable str might not have been initialized
            if(str==currentVersion){
               ^
    1 error
    Press any key to continue . . .
    There is not another str anywhere besides what you see there
    Yes, there was public int str; but I removed that

  16. #16
    ForeverP is offline Member
    Join Date
    Apr 2011
    Posts
    14
    Rep Power
    0

    Default

    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

  1. Replies: 4
    Last Post: 01-01-2011, 11:52 AM
  2. Java String
    By levent in forum Java Tutorial
    Replies: 6
    Last Post: 05-14-2010, 10:26 AM
  3. Java String
    By ooopppsss in forum New To Java
    Replies: 3
    Last Post: 06-25-2009, 11:19 AM
  4. Replies: 2
    Last Post: 09-18-2008, 08:30 AM
  5. Replies: 0
    Last Post: 11-20-2007, 05:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •