Results 1 to 11 of 11
  1. #1
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Detecting Capitalization

    I have this program in my Java class that is due on Monday (Pig latin translator). There are many rules to this and I just kind of started but this is what I have so far:

    Java Code:
    import java.util.*;
    public class IgpayAtinlay {
    	
    	public static void main(String []args){
    		
    		Scanner Keyboard = new Scanner(System.in);
    		
    		
    
    		String current_word;
    		
    		System.out.print("English: ");
    		String input = Keyboard.nextLine();
    		
    		String[] word = input.split(" ");
    		
    		for(int i=0; i<=input.length()-1; i++)
    		{
    			current_word=word[i];
    			wordToPig(current_word);
    			
    			
    		}
    
    		
    		
    		
    		
    		
    	}
    	
    	public static boolean isCapitalized (String word){
    		
    		[B]if(word.charAt(0)){[/B]
    			return true;
    			
    		}
    		else
    			return false;
    		
    	}
    	
    	public static String lowerCase(String word){
    		
    		word=word.toLowerCase();
    		return word;
    	}
    	
    	
    	
    	
    	/*public static String upperCase(String word){
    		String temp, new_word;
    		
    		temp=word.substring(1);
    		temp=temp.toUpperCase();
    		
    		new_word=temp;
    		
    		for(int i=1; i<word.length(); i++){
    		new_word=new_word+word[i];
    		
    		
    		}
    		*/
    	
    	public static String wordToPig (String english){
    		
    		String copy, firstletter, fin;
    		
    		if(isCapitalized(english)==true){
    			lowerCase(english);
    			//upperCase(current_word);
    		}
    		firstletter = english.substring(0);
    		copy = english.substring(1,english.length()-1 );
    		fin = copy+firstletter+"ay";
    		return fin;
    		
    		
    		
    
    		
    		
    	
    		
    	}
    }
    As you can see, the area that is bolded I need help with. I know with C++, you can easily just say if(char > 97 || char < 65) etc, you can figure out if its capital or not (because in C++ chars are secretly integers). However, I dont think this is the case in Java.

    Any help would be appreciated.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Detecting Capitalization

    You can test a char against 'a' for example (char > 'a'). chars can be treated as int in java
    This kind of code: (char > 97) is poor because you have to look up the value of 97 to see what char it is.
    Using a literal like 'Z' shows exactly what you are testing.

  3. #3
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Re: Detecting Capitalization

    Thanks for the reply. However, that isn't what I am looking for. For example, if someone entered:

    Daniel

    I want to say: if(firstletter is capital)
    so I started saying

    if(word.charAt(0) > ???)
    I don't know what the word is yet so I can't actually write the specific character.

    Would this work?
    if(word.charAt(0) > 'z')

    Would ^^^^ that mean that the word's first index be a capital letter?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Detecting Capitalization

    You should write a small program and try some different values to see what the results are.
    Would ^^^^ that mean that the word's first index be a capital letter?
    Do you know whether 'a' is > or < 'A'?
    Try this: System.out.println("'a' > 'A' = " + ('a' > 'A'));

  5. #5
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Re: Detecting Capitalization

    I got:

    'a' > 'A' = true

    so I should write:

    if(word.charAt(0) > 'z')
    then its a capital, right?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Detecting Capitalization

    so I should write:
    if(word.charAt(0) > 'z')
    then its a capital, right?
    Write a small test program, try different values and see what you get.

  7. #7
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Re: Detecting Capitalization

    Thank you for your help! I found out that this is the order:

    ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx yz

    Therefore if(word.charAt(0) < 'a')

    we know it is capital :)!
    (Just for reference in case others stumble upon this thread)

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Detecting Capitalization

    we know it is capital :)!
    Sometimes yes, sometimes no.
    There are a lot of other character values that are less than 'a'.
    The best test would be for the letter to be in the range from 'A' to 'Z'

  9. #9
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Re: Detecting Capitalization

    Ok so how about

    if(word.charAt(0) >= 'A' && word.charAt(0) <= 'Z')

    :-)

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Detecting Capitalization

    That looks about right

  11. #11
    SFNYC is offline Member
    Join Date
    Sep 2010
    Posts
    16
    Rep Power
    0

    Default Re: Detecting Capitalization

    What about something like:

    Java Code:
       public static boolean isCapitalized (String word) {
            return Character.isUpperCase(word.charAt(0));
        }

Similar Threads

  1. Detecting if string or int
    By Alk in forum New To Java
    Replies: 6
    Last Post: 05-14-2011, 11:26 PM
  2. Detecting a lost Gui Connection
    By jeffpaulwilson in forum AWT / Swing
    Replies: 0
    Last Post: 03-15-2011, 03:37 PM
  3. Detecting a lost X connection
    By jeffpaulwilson in forum AWT / Swing
    Replies: 2
    Last Post: 03-09-2011, 07:54 PM
  4. Using Regex for Capitalization
    By Lil_Aziz1 in forum New To Java
    Replies: 4
    Last Post: 02-15-2010, 12:23 AM
  5. Detecting Browser Settings
    By arupranjans in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 07-31-2007, 02:31 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
  •