Results 1 to 5 of 5
Thread: IgnoreCase question
- 11-12-2010, 06:26 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 21
- Rep Power
- 0
IgnoreCase question
I'm trying to make this program ignore cases from the input... but i'm not sure where i need to put IgnoreCase in order for it to ignore the case.
Java Code:import java.util.Scanner; import java.io.*; public class Assignment4 { public static void main (String [] args) throws IOException { String word = "", modWord = "", modWord2 = ""; int vowel_position = 0, letter_position = 0; char letter; PrintWriter outputfile; Scanner inputfile; File filereader = new File("phrases2.txt"); inputfile = new Scanner(filereader); //new PrintWriter = while (inputfile.hasNext()) { word = inputfile.next(); [COLOR="Red"]if (word.charAt(0) != 'a' && word.charAt(0) != 'e' && word.charAt(0) != 'i' && word.charAt(0) != 'o' && word.charAt(0) != 'u' && word.charAt(0) != 'y')[/COLOR] { [COLOR="Red"]while (word.charAt(vowel_position) != 'a' && word.charAt(vowel_position) != 'e' && word.charAt(vowel_position) != 'i' && word.charAt(vowel_position) != 'o' && word.charAt(vowel_position) != 'u' && word.charAt(vowel_position) != 'y') [/COLOR] { vowel_position++; } for (letter_position = 0; letter_position < vowel_position; letter_position++) { letter = word.charAt(letter_position); modWord += letter; } for (; vowel_position < word.length(); vowel_position++) { letter = word.charAt(vowel_position); modWord2 += letter; } System.out.println(modWord2 + "-" + modWord + "ay"); vowel_position = 0; modWord = ""; modWord2 = ""; } else { word += "-yay"; System.out.println(word); inputfile.nextLine(); } } inputfile.close(); System.exit(0); } }- Winners compare their achievements with their goals, while losers compare their achievements with those of other people. -
- 11-13-2010, 12:54 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
What is this program supposed to achieve?
- 11-13-2010, 01:18 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
What is this program supposed to achieve?
I agree.
@OP: programmers are literal and simple minded folk. (but hardworking, perceptive and clever to make up for that). So instead of (or as well as) "ignore cases from the input", try "when I input ... I expect the output to be ... but what I get is ...". To allow other people to run your code and see it in action - you could replace the file based input with something that just sets word to be some value that illustrates the problem.
-------------------------
Consider an idiom like:
Java Code:if("aeiouy".indexOf(word.charAt(vowel_position)) == -1) { // ...Last edited by pbrockway2; 11-13-2010 at 01:21 AM.
- 11-13-2010, 06:52 AM #4
Member
- Join Date
- Sep 2010
- Posts
- 21
- Rep Power
- 0
this program is supposed to translate english into pig-latin. so when the word begins with the vowel it adds a -yay to the end of the word. If it starts with a consonant, it finds the vowel, and will then get every letter before the first vowel and assigns it to modWord and then from that first vowel to the end of the word is assigned to modWord2. so modWord2 print plus modWord + -ay. So for example:
apple>>apple-yay
broom>>oom-bray
cat>>at-cay
yellow>>yellow-yay
style>>yle-stay
this program works perfectly right now except that if the input has a capital letter it won't read it. Now, my question is, is there a way to have the if or while ignore the input's case. like for example, typicall when you want it to ignore the case you say if (!letter_grade.equalsIgnoreCase("done")) this statement says that if the string "letter_grade" while ignoring input's case does not equal "done" then execute that if statement. so i'm trying to find this ignorecase thing.Last edited by Allspark; 11-13-2010 at 07:01 AM.
- Winners compare their achievements with their goals, while losers compare their achievements with those of other people. -
- 11-13-2010, 07:37 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
An expression like
Java Code:word.charAt(0) != 'a'
is simply comparing two primitive values and they are either equal or not. Although the equals() method has an ignores case alternative, the simple == (or !=) operator on primitive values does not (case wouldn't make sense for doubles or booleans etc).
Notice that 'a' is a lower case letter. And - as you observe - there is no problem if word is lower case. So ... make word lowercase!
String's toLowerCase() method
Since you may want the possibly-not-lower-case version as well when you are constructing the pig latin you might use a second variable for the lower case version.
Similar Threads
-
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
RMI question
By alvandrood in forum New To JavaReplies: 1Last Post: 09-14-2009, 12:36 PM -
question
By ayoood in forum Java SoftwareReplies: 6Last Post: 07-07-2008, 01:32 PM -
JSP Question
By maheshkumarjava in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 03-29-2008, 10:51 AM -
a question
By slytheman in forum Java ServletReplies: 0Last Post: 03-12-2008, 04:11 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks