Page 1 of 2 12 LastLast
Results 1 to 20 of 27
Like Tree1Likes

Thread: Integer to String, can't get it to work

  1. #1
    tigertomas is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default Integer to String, can't get it to work

    Hi.
    I try to get the following code to run:

    Java Code:
    for (int i = 0; i < 1; i++) {
    			str = Integer.toString(i);
    			if (i < 10) {
    				str = "0" + str;
    			}
    			
    			
    			for (int j = 0; j < 1; j++) {
    				str2 = Integer.toString(j);
    				if (j < 10) {
    					str2 = "0" + str2;
    					
    				}
    				strfinal = str2+str;
    				grid2.add(strfinal);
    
    			}
    		}

    As you can see, I will only add 1 value to grid2, which is the String 0000. But when I compare it to "0000" I get a 'false' return. Can someone please help me out? Thank you.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Integer to String, can't get it to work

    How are you comparing it?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,503
    Rep Power
    5

    Default Re: Integer to String, can't get it to work

    Also, from the looks of it you are trying to pad on the left with zeros. Unless you are required to program it youself, there is an easier way using String.format.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    tigertomas is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default Re: Integer to String, can't get it to work

    Quote Originally Posted by jim829 View Post
    Also, from the looks of it you are trying to pad on the left with zeros. Unless you are required to program it youself, there is an easier way using String.format.

    Regards,
    Jim
    The thing is that I want the output to act like a string, so I can use it in another class. Why can't I do it like that? Will your tip solve it? :)

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Integer to String, can't get it to work

    You need to show how your are comparing the strings.

    Jim is just suggesting a more standard way of formatting numbers, using the format() method.
    It will still be a string of the format "0000".
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    tigertomas is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default Re: Integer to String, can't get it to work

    Quote Originally Posted by Tolls View Post
    You need to show how your are comparing the strings.

    Jim is just suggesting a more standard way of formatting numbers, using the format() method.
    It will still be a string of the format "0000".
    Ok. I will implement this in a djikstra algorithm. I have a "from", "current" and "to" that consists of a string of 4 numbers. Then I will check, each timestep, what my current node is, whether or not I'm there yet etc. So basically I want a fast way to add nodes to a 100*100 grid, and I'd prefer to not have to do it manually. This is where the loops come in.

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Integer to String, can't get it to work

    No.
    Show the code.
    You say "But when I compare it to "0000" I get a 'false' return.", but there's no code up there that shows this comparison.
    Please, just show that code, because that is the code that is probably wrong.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    tigertomas is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default Re: Integer to String, can't get it to work

    Quote Originally Posted by Tolls View Post
    No.
    Show the code.
    You say "But when I compare it to "0000" I get a 'false' return.", but there's no code up there that shows this comparison.
    Please, just show that code, because that is the code that is probably wrong.
    This is where I make the comparison:

    System.out.print(grid2.get(0) == "0000");




    But the code works when I do it like this:

    grid2.add("0000");

  9. #9
    tigertomas is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default Re: Integer to String, can't get it to work

    Quote Originally Posted by Tolls View Post
    No.
    Show the code.
    You say "But when I compare it to "0000" I get a 'false' return.", but there's no code up there that shows this comparison.
    Please, just show that code, because that is the code that is probably wrong.

    You can see the loops on the top of the page, where I add 0000 to grid2.

    grid.add("0000");

    System.out.println(grid2.get(0) + " " + grid.get(0));
    System.out.println(grid2.get(0) == grid.get(0));

    This will give:

    0000 0000
    false

  10. #10
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Integer to String, can't get it to work

    Quote Originally Posted by tigertomas View Post
    You can see the loops on the top of the page, where I add 0000 to grid2.

    grid.add("0000");

    System.out.println(grid2.get(0) + " " + grid.get(0));
    System.out.println(grid2.get(0) == grid.get(0));

    This will give:

    0000 0000
    false
    when comparing strings don't use the == operator use the equals method.

  11. #11
    tigertomas is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default Re: Integer to String, can't get it to work

    Quote Originally Posted by superhaNds View Post
    when comparing strings don't use the == operator use the equals method.
    Ok, I'll try that. Thanks.

  12. #12
    tigertomas is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default Re: Integer to String, can't get it to work

    Quote Originally Posted by superhaNds View Post
    when comparing strings don't use the == operator use the equals method.
    Wow, it worked! Thanks a lot man.

  13. #13
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,503
    Rep Power
    5

    Default Re: Integer to String, can't get it to work

    Which is exactly why Tolls ask you (twice) how you were comparing it and to show the code.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  14. #14
    tigertomas is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default Re: Integer to String, can't get it to work

    Quote Originally Posted by jim829 View Post
    Which is exactly why Tolls ask you (twice) how you were comparing it and to show the code.

    Regards,
    Jim
    Ok, my bad. I really didn't think it had any to do with the usage ==. Since == works in every other case, I figured it would here as well.

  15. #15
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,876
    Rep Power
    5

    Default Re: Integer to String, can't get it to work

    Quote Originally Posted by tigertomas View Post
    I really didn't think it had any to do with the usage ==. Since == works in every other case, I figured it would here as well.
    So do you now know why in this particular case it DOESN'T work? Because otherwise you'll be back pretty soon making the same mistake again.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Integer to String, can't get it to work

    Quote Originally Posted by tigertomas View Post
    Ok, my bad. I really didn't think it had any to do with the usage ==. Since == works in every other case, I figured it would here as well.
    That was an assumption and assumptions turn out to be false; the == operator works as expected with primitive values such as int, long, double etc. For class type objects, the == operator is too stupid to look inside of the box, i.e. it doesn't compare each character of two Strings etc. Those objects should do that themselves; that's why the String class has the equals( ... ) method implemented and many other classes have that method implemented. aamof, this method is so important that even the Object class has it implemented; the consequence is that all type of objects can be compared for equality with the equals( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    tigertomas is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default Re: Integer to String, can't get it to work

    Quote Originally Posted by gimbal2 View Post
    So do you now know why in this particular case it DOESN'T work? Because otherwise you'll be back pretty soon making the same mistake again.
    Yeah, I guess it's cause when I type 0000 into a String, it's only built up of one string, but in the case where I loop and add strings to strings, it contains several. Is that correct?

  18. #18
    tigertomas is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default Re: Integer to String, can't get it to work

    Quote Originally Posted by JosAH View Post
    That was an assumption and assumptions turn out to be false; the == operator works as expected with primitive values such as int, long, double etc. For class type objects, the == operator is too stupid to look inside of the box, i.e. it doesn't compare each character of two Strings etc. Those objects should do that themselves; that's why the String class has the equals( ... ) method implemented and many other classes have that method implemented. aamof, this method is so important that even the Object class has it implemented; the consequence is that all type of objects can be compared for equality with the equals( ... ) method.

    kind regards,

    Jos
    Thanks, and I know that now :). It's just that I didn't know the == operator only worked in some cases, and I didn't know about the equals operator. Thanks again guys :).

  19. #19
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,503
    Rep Power
    5

    Default Re: Integer to String, can't get it to work

    Quote Originally Posted by tigertomas View Post
    It's just that I didn't know the == operator only worked in some cases..
    Actually, the == operator works in all cases. It just doesn't do what most folks new to Java think it is doing.

    Regards,
    Jim
    Last edited by jim829; 11-29-2013 at 02:29 PM.
    gimbal2 likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  20. #20
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,876
    Rep Power
    5

    Default Re: Integer to String, can't get it to work

    Quote Originally Posted by tigertomas View Post
    and I didn't know about the equals operator.
    equals() is a METHOD, not an operator. I hope you know what the difference is.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Page 1 of 2 12 LastLast

Similar Threads

  1. Get integer value from constant string
    By sonikku in forum New To Java
    Replies: 5
    Last Post: 05-17-2013, 08:41 AM
  2. How to convert integer to String
    By mCoder in forum New To Java
    Replies: 2
    Last Post: 12-27-2011, 04:00 AM
  3. string to integer conversion
    By boys21 in forum New To Java
    Replies: 3
    Last Post: 10-17-2011, 03:58 PM
  4. Integer to String
    By zervine in forum Forum Lobby
    Replies: 3
    Last Post: 09-12-2008, 12:07 PM
  5. String to Integer conversion
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-17-2007, 03: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
  •