Results 1 to 5 of 5

Thread: string loop

  1. #1
    durdanto is offline Member
    Join Date
    Feb 2011
    Posts
    20
    Rep Power
    0

    Default string loop

    I need help to build series of output using user input using loop.

    desired output are following:

    Letters not tried yet: ABCDEFGHIJKLMNOPQRSTUVWXYZ
    Which letter should I check for? a

    Letters not tried yet: -BCDEFGHIJKLMNOPQRSTUVWXYZ
    Which letter should I check for? b
    Letters not tried yet: --CDEFGHIJKLMNOPQRSTUVWXYZ

    so on..

    but The one i coded, it does not take cumulative effect:

    Letters not tried yet: ABCDEFGHIJKLMNOPQRSTUVWXYZ
    Which letter should I check for? a

    Letters not tried yet: -BCDEFGHIJKLMNOPQRSTUVWXYZ
    Which letter should I check for? b

    Letters not tried yet: A-CDEFGHIJKLMNOPQRSTUVWXYZ

    My code are following :

    Java Code:
    String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    Scanner kb = new Scanner(System.in);
    String guessWord = kb.next();
    
    				System.out.print("Which letter should I check for? ");
    				
    				String guessLetter = kb.next();
    			
    				System.out.println("Letters not tried yet: "
    						+ letters.replace(guessLetter.toUpperCase(), " - "));

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default

    You never update your letters variable. The replace() function doesn't actually modify the String (Strings are immutable). Instead, it returns a new String with the replacement done. Read the API.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    292
    Rep Power
    4

    Default

    Do you use some loop for your code?

  4. #4
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by KevinWorkman View Post
    You never update your letters variable. The replace() function doesn't actually modify the String (Strings are immutable). Instead, it returns a new String with the replacement done. Read the API.
    OP, this is what I was hinting you in your previous thread : string

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  5. #5
    UrgeToLearn is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    durdanto,

    Since String is immutable, you can try using StringBuffer. The advantage of using StringBuffer is it has all the features like string along with mutability..

Similar Threads

  1. Exiting a while loop using a String?
    By Isshin in forum New To Java
    Replies: 4
    Last Post: 02-21-2010, 11:33 PM
  2. exiting a while loop with a string
    By Aldius in forum New To Java
    Replies: 3
    Last Post: 12-10-2009, 10:38 PM
  3. String and while loop
    By Exception in forum Java Applets
    Replies: 5
    Last Post: 09-24-2009, 12:32 PM
  4. Using string to terminate loop
    By mrblippy in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 06:16 AM
  5. terminating a while loop with a string
    By tkdvipers in forum New To Java
    Replies: 3
    Last Post: 07-09-2007, 11:23 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
  •