Results 1 to 12 of 12

Thread: string

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

    Default string

    How do we eliminate a letter from a string?

    For example :

    string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

    then user inputs a letter. i.e: A

    How do perform the following:

    Letters not tried yet: "-BCDEFGHIJKLMNOPQRSTUVWXYZ"

    I tried this but won't work:

    Java Code:
    String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    String guessLetter = kb.next();
    
    System.out.println ("Letters not tried yet: " + letters.replace(guessLetter, " - "));

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    I think you have problem with reading data from command-line
    there kb.next(); - what is kb?
    Skype: petrarsentev
    http://TrackStudio.com

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

    Default

    Scanner kb = new Scanner(System.in);
    which I declared it before. sorry i did not mention it.

    And i aslo can't figure out how it make it case insensitive.

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

    Wink

    After replacing with "-" assign it back to the original String. Like,
    Java Code:
    letters = letters.replace(guessLetter, "-");
    And then print the String again. See what happens?

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

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

    Wink

    Even you original attempt should also work. Try to evaluate the input though. Have a print statement and check out what actually you receive?
    Java Code:
    System.out.println(guessLetter);
    Goldest
    Last edited by goldest; 02-18-2011 at 06:27 AM. Reason: Removed.
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  6. #6
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Well It works correct.
    Java Code:
    public static void main(String[] arg) throws Exception {
             Scanner kb = new Scanner(System.in);
             String value = kb.next();
             String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
             System.out.println ("Letters not tried yet: " + letters.replace(value, "-"));
         }
    Java Code:
    A
    Letters not tried yet: -BCDEFGHIJKLMNOPQRSTUVWXYZ
    May be you are entering a not correct data.
    Skype: petrarsentev
    http://TrackStudio.com

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

    Default

    its working now but how do i make it ignore the case.

    for instant, if user type samll s
    to arrive at: "ABCDEFGHIJKLMNOPQR-TUVWXYZ"

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

    Default

    I tried equalsIgnoreCase(.....)
    it returns True/false though

    not "ABCDEFGHIJKLMNOPQR-TUVWXYZ"

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

    Wink

    Considering that your String contains ONLY Uppercase letters, why don't you convert your input into uppercase when you receive it?
    Java Code:
    guessLetter = guessLetter.toUpperCase();

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

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

    Default

    Thnaks goldest, it really helped.

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

    Wink

    Quote Originally Posted by durdanto View Post
    Thnaks goldest, it really helped.
    Glad to know that.

    But make sure to visit the Java API documentation more frequently. You will get some more details over there.
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  12. #12
    javaforum$ is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default Convert Case

    Scanner in = new Scanner(System.in);
    String curr = "ABCDEFcGHCIJKLMN";
    String opt = in.next();//Enter 'c'
    System.out.println(curr);
    System.out.println(opt);
    String lower = opt.toLowerCase();
    String upper = opt.toUpperCase();
    curr = curr.replace(lower, "-");
    curr = curr.replace(upper, "-");
    System.out.println(curr);

Similar Threads

  1. Replies: 6
    Last Post: 12-06-2010, 10:26 AM
  2. Replies: 12
    Last Post: 11-26-2010, 09:33 AM
  3. Replies: 1
    Last Post: 11-04-2010, 07:53 PM
  4. Replies: 7
    Last Post: 11-03-2009, 03:18 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
  •