Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

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

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

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

    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
    Forum VIP
    Join Date
    Jun 2011
    Posts
    313
    My Mood
    Bored
    Thanks
    46
    Thanked 87 Times in 73 Posts
    Blog Entries
    4

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

    Are you aware that your for loops only run once with the value 0? Also, for string equality tests use:

    if (str.equals(str2))

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Quote Originally Posted by ChristopherLowe View Post
    Are you aware that your for loops only run once with the value 0? Also, for string equality tests use:

    if (str.equals(str2))
    Yeah, I'm aware of that. I only changed it to see if I could find the error. 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?

  4. #4
    Forum VIP
    Join Date
    Jun 2011
    Posts
    313
    My Mood
    Bored
    Thanks
    46
    Thanked 87 Times in 73 Posts
    Blog Entries
    4

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

    I don't understand. Integer.toString(i) returns the string representing the integer 'i'. What is the error and what are you expecting?

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Quote Originally Posted by ChristopherLowe View Post
    I don't understand. Integer.toString(i) returns the string representing the integer 'i'. What is the error and what are you expecting?
    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.

    --- Update ---

    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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

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

    The == operator says that the two references in the expression do not refer to the same object.
    == does not look at the contents of two objects. The equals() method does that.

    --- Update ---

    Another waste of time helping an OP that has cross posted:
    Integer to String, can't get it to work
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Quote Originally Posted by Norm View Post
    The == operator says that the two references in the expression do not refer to the same object.
    == does not look at the contents of two objects. The equals() method does that.

    --- Update ---

    Another waste of time helping an OP that has cross posted:
    Integer to String, can't get it to work
    Thanks a lot for the reply. It's just that I have a deadline so I wanted to increase the chances of me getting an answer. I really appreciate it!

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

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

    In the future please post links to the other sites where you have posted the same question.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Quote Originally Posted by Norm View Post
    In the future please post links to the other sites where you have posted the same question.
    Okay, sorry.

Similar Threads

  1. Return type of converting integer to string for linked list
    By jcfor3ver in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 22nd, 2013, 06:37 PM
  2. Problem with String (and integer) conversion!
    By tonynsx in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 7th, 2013, 12:44 PM
  3. Replies: 6
    Last Post: June 3rd, 2013, 04:57 AM
  4. Print 000 infront of String, I can get INT to work but not string
    By keat84 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 1st, 2012, 11:23 PM
  5. Help with code for converting 4 digit string to integer
    By danielp1213 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2011, 09:38 PM