Results 1 to 6 of 6
Thread: Searching command line
- 02-25-2009, 04:36 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 47
- Rep Power
- 0
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()) } }
- 02-25-2009, 04:46 AM #2
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 05:21 AM.
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
-
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!"); } } } }
- 02-25-2009, 05:19 AM #4
haha now now... i was modifying his original code. don't want chris comming after me...
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
-
I know, and I wasn't going to post code, but since you did, and since the isLowerCase method just seemed much cleaner...
- 02-25-2009, 06:00 PM #6
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 01:26 AM. Reason: dull colors
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
Similar Threads
-
Command line argument
By denisatandi in forum New To JavaReplies: 8Last Post: 10-16-2012, 11:37 PM -
Command Line Arguments
By Nakira in forum NetBeansReplies: 10Last Post: 02-04-2010, 03:45 PM -
Command Line Argument
By right2001 in forum New To JavaReplies: 6Last Post: 02-17-2009, 02:08 PM -
Exporting from the command line
By o1121 in forum EclipseReplies: 1Last Post: 08-09-2007, 07:29 PM -
Unable to execute command line command in java
By LordSM in forum New To JavaReplies: 1Last Post: 08-08-2007, 12:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks