Results 1 to 5 of 5
  1. #1
    Allspark is offline Member
    Join Date
    Sep 2010
    Posts
    21
    Rep Power
    0

    Default 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. -

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    What is this program supposed to achieve?

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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 02:21 AM.

  4. #4
    Allspark is offline Member
    Join Date
    Sep 2010
    Posts
    21
    Rep Power
    0

    Default

    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 08:01 AM.
    - Winners compare their achievements with their goals, while losers compare their achievements with those of other people. -

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 09:49 AM
  2. RMI question
    By alvandrood in forum New To Java
    Replies: 1
    Last Post: 09-14-2009, 01:36 PM
  3. question
    By ayoood in forum Java Software
    Replies: 6
    Last Post: 07-07-2008, 02:32 PM
  4. JSP Question
    By maheshkumarjava in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-29-2008, 11:51 AM
  5. a question
    By slytheman in forum Java Servlet
    Replies: 0
    Last Post: 03-12-2008, 05:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •