Results 1 to 8 of 8
  1. #1
    SwissR is offline Member
    Join Date
    Jul 2010
    Posts
    39
    Rep Power
    0

    Default 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");   
                }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    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

  3. #3
    SwissR is offline Member
    Join Date
    Jul 2010
    Posts
    39
    Rep Power
    0

    Default 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");   
                }

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    Get rid of the evaluation:
    Java Code:
    if (checkContains1 || checkContains2 || checkContains3|| checkContains4)
    Why do you have to be careful? You can throw away a few dozen lines of code using a Set and a regex.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    Quote Originally Posted by SwissR View Post
    the compiler is throwing an error on the if (checkContains1 || checkContains2 || checkContains3|| checkContains4|| !=true)
    And what is
    Java Code:
    checkContains4|| !=true
    supposed to be anyway?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    SwissR is offline Member
    Join Date
    Jul 2010
    Posts
    39
    Rep Power
    0

    Default

    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");   
                }

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    WTF?! Reread reply #4.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  8. #8
    SwissR is offline Member
    Join Date
    Jul 2010
    Posts
    39
    Rep Power
    0

    Default

    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

  1. checking for ints in a String
    By SteroidalPsycho in forum New To Java
    Replies: 1
    Last Post: 03-26-2010, 06:09 PM
  2. Help with checking for a certain format in a String
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 03-26-2010, 04:56 AM
  3. spliting a string and checking each token's format
    By Implode in forum New To Java
    Replies: 1
    Last Post: 10-18-2009, 08:41 PM
  4. How to print chars with symbols from a string
    By blacksky in forum New To Java
    Replies: 23
    Last Post: 01-06-2009, 01:14 PM
  5. How to cut symbols from a string?
    By gutters in forum New To Java
    Replies: 3
    Last Post: 06-16-2008, 03:47 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
  •