₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,148 members, 8,420,555 topics. Date: Friday, 05 June 2026 at 01:22 AM

Toggle theme

Java - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingJava (876 Views)

1 Reply (Go Down)

Java by HenryBios(op): 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.
Re: Java by IbrahimB: 7:53pm On Aug 14, 2010
The complete code?  undecided
Re: Java by 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.
Re: Java by HenryBios(op): 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!!!
Re: Java by 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 user that a special character has been found.
Re: Java by HenryBios(op): 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.
Re: Java by 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.
Re: Java by donclemo(m): 3:57am On Aug 28, 2010
good job guys
Re: Java by 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();. (remember to place that in a try block because it throws a couple of exceptions e.g IOException, SecurityException etc) that should return a string. pass 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 uploaded the complete source code but I'm kind'a busy. I hope my explanation helps.
1 Reply

234

Integration Of Network Management System (nms) To Helpdesk SoftwareAll Programmers & Webmasters I Need Your Help Here!What Nigerians Searched For Most On Google 2012