NewStats: 3,264,528 , 8,184,015 topics. Date: Wednesday, 11 June 2025 at 12:21 PM j4x3

6z3e3g

Java - Programming - Nairaland 6f542m

Java (865 Views)

(4)

(1) (Go Down)

HenryBios: 8:29am On Aug 13, 2010
I want to develop an application in java which searches special characters in a .CSV, Flat File and Excel sheet. I want an alert once it finds any special characters in these files.Please provide the complete code.
IbrahimB: 7:53pm On Aug 14, 2010
The complete code">
Bamid467: 10:46pm On Aug 15, 2010
There's no way You Can get the Complete code, You can only get assistance so pls be specific.
HenryBios: 5:15am On Aug 16, 2010
I am using this code to read the txt file.


import java.io.*;
class FileRead
{
public static void main(String args[])
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("textfile.txt"wink;
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}



Which code I have to add to get the alert message that your file contains special characters.

Please help!!!
IbrahimB: 7:14pm On Aug 16, 2010
Good. Now people can step in and help. Note: this is probably not valid Java code but your program should look something like this:

char[] specialXters = { '*', '%', '&', '/', '\' }; //I don't know if this is what you mean by special characters?

while ((strLine = br.readLine()) != null)   
{

      // Print the content on the console
      System.out.println (strLine);

     foreach(char c in strLine)
     {
           int i = 0;
           while(i < 5)
//because I have 5 special characters in this example
           {
               if(c == specialXters[i++])
               {
                 System.out.println("Special Character found!"wink;
                 break;
               }
           }
     }
}


Essentially, create an array or list to hold your special characters. For each line you read, iterate over the characters in the line to find if the character occurs in your "special character array", if that happens, alert the that a special character has been found.
HenryBios: 7:32am On Aug 17, 2010
import java.io.*;
class FileRead
{
public static void main(String args[])
{

try{


var iChars = "*|,\":<>[]{}`\';()@&$#%";
for (var i = 0; i < string.length; i++) {
if (iChars.indexOf(string.charAt(i)) != -1){
System.out.println("Characters found"wink;
}


catch (Exception e){
System.err.println("Error: " + e.getMessage());
}


}
}
}

Please let me know what is wrong with the above code.

It is giving some compilation errors.
IbrahimB: 2:47pm On Aug 17, 2010
You did not post the compiler errors? I am not sure there is a var keyword in Java - however, I may be wrong. Looks more like Javascript to me. Try this:

char[] specialXters = {'*', '|', '\', ':', '<', '>', '[', ']', '{', '}', '\', ';', '(', ')', '@', '&', '$', '#', '%'};
int numberOfXters = 19; //about 19 characters in this case

for(char c: strLine)
{
int i = 0;
while(i <   numberOfXters)
{
  if(c == specialXters[i++])
  {
   System.out.println("Characters found"wink;
   break;
  }
}
}


Insert this into your code. I don't do Java though, but I think should work. If you have any problems tell us the compiler error you're getting as well.
donclemo(m): 3:57am On Aug 28, 2010
good job guys
SayoMarvel(m): 1:03pm On Aug 29, 2010
Very good job. Just a software engineering tip, if you encouter this problem a little often, its better to create a special class (maybe StringProcessor) with static method contains(String s, char[] symbols). You should now read each line of the file using java.util.Scanner(new java.io.File("dataFile.txt"wink.readLine();. ( to place that in a try block because it throws a couple of exceptions e.g IOException, SecurityException etc) that should return a string. that string to the method you created in your StringProcessor class. Implement your contains method as stated above (only that you need some little syntax modification but @IbrahimB's algorithm is okay). I should have ed the complete source code but I'm kind'a busy. I hope my explanation helps.

(1) (Reply)

Java Programming

(Go Up)

Sections: How To . 13
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or s on Nairaland.