Results 1 to 12 of 12
Thread: string
- 02-17-2011, 10:07 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 20
- Rep Power
- 0
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, " - "));
- 02-17-2011, 10:17 AM #2
I think you have problem with reading data from command-line
there kb.next(); - what is kb?Skype: petrarsentev
http://TrackStudio.com
- 02-17-2011, 10:25 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 20
- Rep Power
- 0
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.
- 02-17-2011, 10:26 AM #4
After replacing with "-" assign it back to the original String. Like,
And then print the String again. See what happens?Java Code:letters = letters.replace(guessLetter, "-");
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 02-17-2011, 10:37 AM #5
Even you original attempt should also work. Try to evaluate the input though. Have a print statement and check out what actually you receive?
GoldestJava Code:System.out.println(guessLetter);
Last edited by goldest; 02-18-2011 at 05:27 AM. Reason: Removed.
Java Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 02-17-2011, 10:37 AM #6
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, "-")); }May be you are entering a not correct data.Java Code:A Letters not tried yet: -BCDEFGHIJKLMNOPQRSTUVWXYZ
Skype: petrarsentev
http://TrackStudio.com
- 02-17-2011, 10:39 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 20
- Rep Power
- 0
its working now but how do i make it ignore the case.
for instant, if user type samll s
to arrive at: "ABCDEFGHIJKLMNOPQR-TUVWXYZ"
- 02-17-2011, 10:50 AM #8
Member
- Join Date
- Feb 2011
- Posts
- 20
- Rep Power
- 0
I tried equalsIgnoreCase(.....)
it returns True/false though
not "ABCDEFGHIJKLMNOPQR-TUVWXYZ"
- 02-17-2011, 10:59 AM #9
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();
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 02-17-2011, 11:13 AM #10
Member
- Join Date
- Feb 2011
- Posts
- 20
- Rep Power
- 0
Thnaks goldest, it really helped.
- 02-17-2011, 11:19 AM #11
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!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 02-17-2011, 11:24 AM #12
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
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
-
Basic string problem, just need to extract two numbers out of string
By gonzoateafly in forum New To JavaReplies: 6Last Post: 12-06-2010, 09:26 AM -
Binary-algorithm -> Insert String to sorted String-ArrayList
By Muskar in forum Advanced JavaReplies: 12Last Post: 11-26-2010, 08:33 AM -
Test for all empty Strings in LinkedHashMap<String,ArrayList<String>
By albertkao in forum New To JavaReplies: 1Last Post: 11-04-2010, 06:53 PM -
The constructor Person(String, String, Date) is undefined
By fh84 in forum New To JavaReplies: 7Last Post: 11-03-2009, 02:18 AM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks