Results 1 to 8 of 8
- 07-26-2010, 04:25 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 39
- Rep Power
- 0
Checking If A String Contains Symbols
Hi guys, I am trying to find out how to check if a string (with a constantly changing value) contains symbols.
I'd like to say If my string (resourceline) contains any of the values ("$, =, ", >, :") || if the resourceline's value already exists within the array then loop back to the main method and continue reading next line.
Else
add to the array.
please can anyone help
would instances of below work
boolean checkContains1=resourceline.contains("$");
boolean checkContains2=resourceline.contains("=");
boolean checkContains3=resourceline.contains(""");
boolean checkContains4=resourceline.contains(">");
boolean checkContains5=resourceline.contains(":");
Example code
Java Code:/** method to find the value between the beginning and end of a string*/ public Find(String fileName, String begin, String end,String whereToWrite) throws java.io.IOException { try { /** read in file */ String resourceline = null; ArrayList<String> arrayList = new ArrayList<String>(); File myDir = new File("C:/Documents and Settings/Kieren McDonald/Desktop/Nick/Java/Test/resources"); if (myDir.exists() && myDir.isDirectory()) { File[] files = myDir.listFiles(); for (int i = 0; i < files.length; i++) { br = new BufferedReader(new FileReader(files[i])); while ((resourceline = br.readLine()) != null) { resourceline = find(begin, end, resourceline); if (resourceline != null) { resourceline = resourceline.trim(); arrayList.add(resourceline); } } System.out.println("/**This Is A List Of Includes Within The Component*/"); sortAndPrint(arrayList); System.out.println("/*******This Is The End Of The list Of Includes*******/"); writeResults(arrayList, "C:\\Documents and Settings\\Kieren McDonald\\Desktop\\Nick\\Java\\Test\\filetest.txt"); } } else { System.out.println("This is not a directory"); }
- 07-26-2010, 04:45 PM #2
To avoid duplicates I'd use a Set rather than a List. Use String.matches() to check if the resourceline contains any of the values.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-26-2010, 04:52 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 39
- Rep Power
- 0
Update
Yes but i have to be careful as this is step one of a development in my programme.
For example im just trying this idea below, but the compiler is throwing an error on the if (checkContains1 || checkContains2 || checkContains3|| checkContains4|| !=true) line.
Its telling me that != is an illegal start to an expression, however its not allow ==true either so im a bit stumped at the moment.
Im pretty new to this so im reading articles/documentation as i go through.
I'm hoping im nearly there now.
Java Code:/** method to find the value between the beginning and end of a string*/ public Find(String fileName, String begin, String end,String whereToWrite) throws java.io.IOException { try { /** read in file */ String resourceline = null; ArrayList<String> arrayList = new ArrayList<String>(); File myDir = new File("C:/Documents and Settings/Kieren McDonald/Desktop/Nick/Java/Test/resources"); if (myDir.exists() && myDir.isDirectory()) { File[] files = myDir.listFiles(); for (int i = 0; i < files.length; i++) { br = new BufferedReader(new FileReader(files[i])); while ((resourceline = br.readLine()) != null) { resourceline = find(begin, end, resourceline); if (resourceline != null) { boolean checkContains1=resourceline.contains("$"); boolean checkContains2=resourceline.contains("="); boolean checkContains3=resourceline.contains(">"); boolean checkContains4=resourceline.contains(":"); if (checkContains1 || checkContains2 || checkContains3|| checkContains4|| !=true) { main(); } if (arrayList.contains(resourceline)){ main(); } } else{ resourceline = resourceline.trim(); arrayList.add(resourceline); } } System.out.println("/**This Is A List Of Includes Within The Component*/"); sortAndPrint(arrayList); System.out.println("/*******This Is The End Of The list Of Includes*******/"); writeResults(arrayList, "C:\\Documents and Settings\\Kieren McDonald\\Desktop\\Nick\\Java\\Test\\filetest.txt"); } } else { System.out.println("This is not a directory"); }
- 07-26-2010, 04:58 PM #4
Get rid of the evaluation:
Why do you have to be careful? You can throw away a few dozen lines of code using a Set and a regex.Java Code:if (checkContains1 || checkContains2 || checkContains3|| checkContains4)
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-26-2010, 05:01 PM #5
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-26-2010, 05:20 PM #6
Member
- Join Date
- Jul 2010
- Posts
- 39
- Rep Power
- 0
Apologies, thats a typo. Im wondering if i can use
if (true(checkContains1 || checkContains2 || checkContains3 || checkContains4)) aswel.
Java Code:public Find(String fileName, String begin, String end,String whereToWrite) throws java.io.IOException { try { /** read in file */ String resourceline = null; ArrayList<String> arrayList = new ArrayList<String>(); File myDir = new File("C:/Documents and Settings/Kieren McDonald/Desktop/Nick/Java/Test/resources"); if (myDir.exists() && myDir.isDirectory()) { File[] files = myDir.listFiles(); for (int i = 0; i < files.length; i++) { br = new BufferedReader(new FileReader(files[i])); while ((resourceline = br.readLine()) != null) { resourceline = find(begin, end, resourceline); if (resourceline != null) { boolean checkContains1=resourceline.contains("$"); boolean checkContains2=resourceline.contains("="); boolean checkContains3=resourceline.contains(">"); boolean checkContains4=resourceline.contains(":"); if (!checkContains1 || !checkContains2 || !checkContains3 || !checkContains4) { public static void main(); } if (arrayList.contains(resourceline)){ public static void main(); } } else{ resourceline = resourceline.trim(); arrayList.add(resourceline); } } System.out.println("/**This Is A List Of Includes Within The Component*/"); sortAndPrint(arrayList); System.out.println("/*******This Is The End Of The list Of Includes*******/"); writeResults(arrayList, "C:\\Documents and Settings\\Kieren McDonald\\Desktop\\Nick\\Java\\Test\\filetest.txt"); } } else { System.out.println("This is not a directory"); }
- 07-26-2010, 05:55 PM #7
WTF?! Reread reply #4.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-27-2010, 09:07 AM #8
Member
- Join Date
- Jul 2010
- Posts
- 39
- Rep Power
- 0
Im using if (checkContains1 || checkContains2 || checkContains3 || checkContains4)
because i already have a regex and the variable for the resourceline is constantly changing. so a pattern of characters are not all going to appear, only one or two will.
Also this is a development of an earlier program i ran except the earlier version pulled resource names, the program i am working on now is to pull out idoc variables.
Once the two seperate programs are completed i am going to integrate them in together. Both use regex's so I am trying to avoid having too many.
Similar Threads
-
checking for ints in a String
By SteroidalPsycho in forum New To JavaReplies: 1Last Post: 03-26-2010, 06:09 PM -
Help with checking for a certain format in a String
By SteroidalPsycho in forum New To JavaReplies: 2Last Post: 03-26-2010, 04:56 AM -
spliting a string and checking each token's format
By Implode in forum New To JavaReplies: 1Last Post: 10-18-2009, 08:41 PM -
How to print chars with symbols from a string
By blacksky in forum New To JavaReplies: 23Last Post: 01-06-2009, 01:14 PM -
How to cut symbols from a string?
By gutters in forum New To JavaReplies: 3Last Post: 06-16-2008, 03:47 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks