Results 1 to 6 of 6
  1. #1
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default Searching command line

    I am trying to search through the command-line arguments and if one is found that does NOT begin with an uppercase letter, display an error message and terminate. I can't figure out how to check if the first letter is an uppercase letter or not. I tried multiple times, but this is probably the best I could come up with. It didn't compile. Can anyone give me some tips or something on how to do this?

    Java Code:
    	public static void main(String[] args) {
    		String thelist[] = new String[args.length];
    		int i=0;
    		while(i<args.length) {
    			if(thelist[i].charAt(0) != thelist[i].toUpperCase())
    					
    					
    		}
    		
    	}

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Java Code:
    String[] thelist = [B]args;[/B]
    int i=0;
    while(i<args.length) {
      if(thelist[i].charAt(0) != thelist[i].toUpperCase()[B].charAt(0))[/B]
           [B] System.out.println("NOT");[/B]
          [B]i++;[/B]
    		}
    Last edited by angryboy; 02-25-2009 at 06:21 AM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    There are two methods in the Character class that may help you here. Actually you only need to use one or the other. The API is here: Character (Java Platform SE 6)

    edit: since angryboy is giving code, this is what I have:
    Java Code:
    public class Fubar2
    {
      public static void main(String[] args)
      {
        for (String string : args)
        {
          if (Character.isLowerCase(string.charAt(0)))
          {
            System.out.println("Error!");
          }
        }
      }
    }

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    haha now now... i was modifying his original code. don't want chris comming after me...
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I know, and I wasn't going to post code, but since you did, and since the isLowerCase method just seemed much cleaner...

  6. #6
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    since fubarable is giving out good codes...
    i thought i test out the speed of different methods. I thought up of a couple: Character, regex, typecasting, OP
    Java Code:
    [COLOR=NAVY]import[/COLOR] my[COLOR=BLACK].[/COLOR]javastuff[COLOR=BLACK].[/COLOR]ExeTimer[COLOR=BLACK];[/COLOR]
    
    [COLOR=PURPLE]public[/COLOR] [COLOR=PURPLE]class[/COLOR] SpeedChar[COLOR=BLACK]{[/COLOR]
      [COLOR=PURPLE]public[/COLOR] [COLOR=PURPLE]static[/COLOR] [COLOR=PURPLE]void[/COLOR] main[COLOR=BLACK]([/COLOR]String[COLOR=BLACK][[/COLOR][COLOR=BLACK]][/COLOR] args[COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
        String[COLOR=BLACK][[/COLOR][COLOR=BLACK]][/COLOR] s [COLOR=BLACK]=[/COLOR] [COLOR=BLACK]{[/COLOR][COLOR=SILVER]"one"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Two"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Three"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"one"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Two"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Three"[/COLOR][COLOR=BLACK],[/COLOR]
                      [COLOR=SILVER]"one"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Two"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Three"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"one"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Two"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Three"[/COLOR][COLOR=BLACK],[/COLOR]
                      [COLOR=SILVER]"one"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Two"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Three"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"one"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Two"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Three"[/COLOR][COLOR=BLACK],[/COLOR]
                      [COLOR=SILVER]"one"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Two"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Three"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"one"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Two"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Three"[/COLOR][COLOR=BLACK],[/COLOR]
                      [COLOR=SILVER]"one"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Two"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Three"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"one"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Two"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Three"[/COLOR][COLOR=BLACK],[/COLOR]
                      [COLOR=SILVER]"one"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Two"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Three"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"one"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Two"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Three"[/COLOR][COLOR=BLACK],[/COLOR]
                      [COLOR=SILVER]"one"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Two"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Three"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"one"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Two"[/COLOR][COLOR=BLACK],[/COLOR] [COLOR=SILVER]"Three"[/COLOR][COLOR=BLACK],[/COLOR]
                    [COLOR=BLACK]}[/COLOR][COLOR=BLACK];[/COLOR]
                  
        ExeTimer e [COLOR=BLACK]=[/COLOR] [COLOR=NAVY]new[/COLOR] ExeTimer[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=PURPLE]int[/COLOR] loops [COLOR=BLACK]=[/COLOR] [COLOR=ORANGE][B]10000000[/B][/COLOR][COLOR=BLACK];[/COLOR] [COLOR=GREEN][I]//10,000,000[/I][/COLOR]
        
        [COLOR=GREEN][I]//test Fubarable[/I][/COLOR]
        System[COLOR=BLACK].[/COLOR]out[COLOR=BLACK].[/COLOR]println[COLOR=BLACK]([/COLOR][COLOR=SILVER]"miniFubar"[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        e[COLOR=BLACK].[/COLOR]start[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=NAVY]for[/COLOR][COLOR=BLACK]([/COLOR][COLOR=PURPLE]int[/COLOR] i[COLOR=BLACK]=[/COLOR][COLOR=ORANGE][B]0[/B][/COLOR][COLOR=BLACK];[/COLOR] i[COLOR=BLACK]<[/COLOR]loops[COLOR=BLACK];[/COLOR] i[COLOR=BLACK]++[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
          miniFubar[COLOR=BLACK]([/COLOR]s[COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=BLACK]}[/COLOR]    
        e[COLOR=BLACK].[/COLOR]stop[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        System[COLOR=BLACK].[/COLOR]out[COLOR=BLACK].[/COLOR]println[COLOR=BLACK]([/COLOR]e[COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR] [COLOR=GREEN][I]// Time:   13.159 secs[/I][/COLOR]
    
        [COLOR=GREEN][I]//test AngryBoy[/I][/COLOR]
        System[COLOR=BLACK].[/COLOR]out[COLOR=BLACK].[/COLOR]println[COLOR=BLACK]([/COLOR][COLOR=SILVER]"majorAngry"[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        e[COLOR=BLACK].[/COLOR]start[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=NAVY]for[/COLOR][COLOR=BLACK]([/COLOR][COLOR=PURPLE]int[/COLOR] i[COLOR=BLACK]=[/COLOR][COLOR=ORANGE][B]0[/B][/COLOR][COLOR=BLACK];[/COLOR] i[COLOR=BLACK]<[/COLOR]loops[COLOR=BLACK];[/COLOR] i[COLOR=BLACK]++[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
          majorAngry[COLOR=BLACK]([/COLOR]s[COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=BLACK]}[/COLOR]
        e[COLOR=BLACK].[/COLOR]stop[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR] 
        System[COLOR=BLACK].[/COLOR]out[COLOR=BLACK].[/COLOR]println[COLOR=BLACK]([/COLOR]e[COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR] [COLOR=GREEN][I]// Time:   5.538 secs[/I][/COLOR]
        
        [COLOR=GREEN][I]//test regex[/I][/COLOR]
        [COLOR=GREEN][I]// TAKES FOREVER!!![/I][/COLOR]
        [COLOR=GREEN]/* 
        System.out.println("generalRegex");
        e.start();
        for(int i=0; i<loops; i++){
          //generalRegex(s); 
        }    
        e.stop();
        System.out.println(e); // takes forever
        */[/COLOR]
        
        [COLOR=GREEN][I]//test OP[/I][/COLOR]
        System[COLOR=BLACK].[/COLOR]out[COLOR=BLACK].[/COLOR]println[COLOR=BLACK]([/COLOR][COLOR=SILVER]"captainMcChill"[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        e[COLOR=BLACK].[/COLOR]start[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=NAVY]for[/COLOR][COLOR=BLACK]([/COLOR][COLOR=PURPLE]int[/COLOR] i[COLOR=BLACK]=[/COLOR][COLOR=ORANGE][B]0[/B][/COLOR][COLOR=BLACK];[/COLOR] i[COLOR=BLACK]<[/COLOR]loops[COLOR=BLACK];[/COLOR] i[COLOR=BLACK]++[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
          captainMcChill[COLOR=BLACK]([/COLOR]s[COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=BLACK]}[/COLOR]    
        e[COLOR=BLACK].[/COLOR]stop[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        System[COLOR=BLACK].[/COLOR]out[COLOR=BLACK].[/COLOR]println[COLOR=BLACK]([/COLOR]e[COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR] [COLOR=GREEN][I]// Time:   17.505 secs[/I][/COLOR]
      [COLOR=BLACK]}[/COLOR]
    
      [COLOR=GREEN][I]// OP (tweeked)[/I][/COLOR]
      [COLOR=PURPLE]public[/COLOR] [COLOR=PURPLE]static[/COLOR] [COLOR=PURPLE]boolean[/COLOR] captainMcChill[COLOR=BLACK]([/COLOR]String[COLOR=BLACK][[/COLOR][COLOR=BLACK]][/COLOR] args[COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
        String[COLOR=BLACK][[/COLOR][COLOR=BLACK]][/COLOR] thelist [COLOR=BLACK]=[/COLOR] args[COLOR=BLACK];[/COLOR]
        [COLOR=PURPLE]int[/COLOR] i[COLOR=BLACK]=[/COLOR][COLOR=ORANGE][B]0[/B][/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=NAVY]while[/COLOR][COLOR=BLACK]([/COLOR]i[COLOR=BLACK]<[/COLOR]args[COLOR=BLACK].[/COLOR]length[COLOR=BLACK])[/COLOR] [COLOR=BLACK]{[/COLOR]
          [COLOR=BLACK][[/COLOR]i[COLOR=BLACK]][/COLOR][COLOR=BLACK][[/COLOR]i[COLOR=BLACK]][/COLOR][COLOR=GREEN][I]//if(thelist.charAt(0) != thelist.toUpperCase().charAt(0))[/I][/COLOR]
          [COLOR=GREEN][I]// changed, becuase it was too slow.[/I][/COLOR]
          [COLOR=NAVY]if[/COLOR][COLOR=BLACK]([/COLOR]thelist[COLOR=BLACK][[/COLOR]i[COLOR=BLACK]][/COLOR][COLOR=BLACK].[/COLOR]charAt[COLOR=BLACK]([/COLOR][COLOR=ORANGE][B]0[/B][/COLOR][COLOR=BLACK])[/COLOR] [COLOR=BLACK]!=[/COLOR] Character[COLOR=BLACK].[/COLOR]toUpperCase[COLOR=BLACK]([/COLOR]thelist[COLOR=BLACK][[/COLOR]i[COLOR=BLACK]][/COLOR][COLOR=BLACK].[/COLOR]charAt[COLOR=BLACK]([/COLOR][COLOR=ORANGE][B]0[/B][/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK])[/COLOR]
            [COLOR=BLACK];[/COLOR] [COLOR=GREEN][I]// do nothing.[/I][/COLOR]
            [COLOR=GREEN][I]// return false;[/I][/COLOR]
          i[COLOR=BLACK]++[/COLOR][COLOR=BLACK];[/COLOR]
      	[COLOR=BLACK]}[/COLOR]
        [COLOR=NAVY]return[/COLOR] [COLOR=NAVY][B]true[/B][/COLOR][COLOR=BLACK];[/COLOR]
      [COLOR=BLACK]}[/COLOR]
      
      [COLOR=GREEN][I]// takes forever[/I][/COLOR]
      [COLOR=PURPLE]public[/COLOR] [COLOR=PURPLE]static[/COLOR] [COLOR=PURPLE]boolean[/COLOR] generalRegex[COLOR=BLACK]([/COLOR]String[COLOR=BLACK][[/COLOR][COLOR=BLACK]][/COLOR] args[COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
        [COLOR=NAVY]for[/COLOR] [COLOR=BLACK]([/COLOR]String string [COLOR=BLACK]:[/COLOR] args[COLOR=BLACK])[/COLOR]
          [COLOR=NAVY]if[/COLOR] [COLOR=BLACK]([/COLOR]string[COLOR=BLACK].[/COLOR]matches[COLOR=BLACK]([/COLOR][COLOR=SILVER]"[a-z].*"[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK])[/COLOR]
            [COLOR=BLACK];[/COLOR] [COLOR=GREEN][I]// do nothing[/I][/COLOR]
            [COLOR=GREEN][I]//return false;[/I][/COLOR]
        [COLOR=NAVY]return[/COLOR] [COLOR=NAVY][B]true[/B][/COLOR][COLOR=BLACK];[/COLOR]  
      [COLOR=BLACK]}[/COLOR]
      
      [COLOR=GREEN][I]// works w/ ascii only!![/I][/COLOR]
      [COLOR=PURPLE]public[/COLOR] [COLOR=PURPLE]static[/COLOR] [COLOR=PURPLE]boolean[/COLOR] majorAngry[COLOR=BLACK]([/COLOR]String[COLOR=BLACK][[/COLOR][COLOR=BLACK]][/COLOR] args[COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
        [COLOR=NAVY]for[/COLOR] [COLOR=BLACK]([/COLOR]String string [COLOR=BLACK]:[/COLOR] args[COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
          [COLOR=PURPLE]char[/COLOR] ch [COLOR=BLACK]=[/COLOR] string[COLOR=BLACK].[/COLOR]charAt[COLOR=BLACK]([/COLOR][COLOR=ORANGE][B]0[/B][/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
          [COLOR=NAVY]if[/COLOR] [COLOR=BLACK]([/COLOR][COLOR=BLACK]([/COLOR][COLOR=BLACK]([/COLOR]ch[COLOR=BLACK]-[/COLOR][COLOR=SILVER][B]'a'[/B][/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK]|[/COLOR][COLOR=BLACK]([/COLOR][COLOR=SILVER][B]'z'[/B][/COLOR][COLOR=BLACK]-[/COLOR]ch[COLOR=BLACK])[/COLOR][COLOR=BLACK])[/COLOR] [COLOR=BLACK]>=[/COLOR] [COLOR=ORANGE][B]0[/B][/COLOR][COLOR=BLACK])[/COLOR]
            [COLOR=BLACK];[/COLOR] [COLOR=GREEN][I]// do nothing[/I][/COLOR]
            [COLOR=GREEN][I]//return false;[/I][/COLOR]
        [COLOR=BLACK]}[/COLOR]
        [COLOR=NAVY]return[/COLOR] [COLOR=NAVY][B]true[/B][/COLOR][COLOR=BLACK];[/COLOR]  
      [COLOR=BLACK]}[/COLOR]
    
      [COLOR=GREEN][I]// uses alot of recursions!! take a look at the src: Character.java[/I][/COLOR]
      [COLOR=PURPLE]public[/COLOR] [COLOR=PURPLE]static[/COLOR] [COLOR=PURPLE]boolean[/COLOR] miniFubar[COLOR=BLACK]([/COLOR]String[COLOR=BLACK][[/COLOR][COLOR=BLACK]][/COLOR] args[COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
        [COLOR=NAVY]for[/COLOR] [COLOR=BLACK]([/COLOR]String string [COLOR=BLACK]:[/COLOR] args[COLOR=BLACK])[/COLOR]
          [COLOR=NAVY]if[/COLOR] [COLOR=BLACK]([/COLOR]Character[COLOR=BLACK].[/COLOR]isLowerCase[COLOR=BLACK]([/COLOR]string[COLOR=BLACK].[/COLOR]charAt[COLOR=BLACK]([/COLOR][COLOR=ORANGE][B]0[/B][/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK])[/COLOR]
            [COLOR=BLACK];[/COLOR] [COLOR=GREEN][I]// do nothing[/I][/COLOR]
            [COLOR=GREEN][I]//return false;[/I][/COLOR]
        [COLOR=NAVY]return[/COLOR] [COLOR=NAVY][B]true[/B][/COLOR][COLOR=BLACK];[/COLOR]
      [COLOR=BLACK]}[/COLOR]
    [COLOR=BLACK]}[/COLOR]
    Last edited by angryboy; 03-01-2009 at 02:26 AM. Reason: dull colors
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Similar Threads

  1. Command line argument
    By denisatandi in forum New To Java
    Replies: 8
    Last Post: 10-17-2012, 12:37 AM
  2. Command Line Arguments
    By Nakira in forum NetBeans
    Replies: 10
    Last Post: 02-04-2010, 04:45 PM
  3. Command Line Argument
    By right2001 in forum New To Java
    Replies: 6
    Last Post: 02-17-2009, 03:08 PM
  4. Exporting from the command line
    By o1121 in forum Eclipse
    Replies: 1
    Last Post: 08-09-2007, 08:29 PM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 01:23 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
  •