The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Parsing strings

Posted by Hiran on January 16, 2002 at 2:32 PM

Why don't you read in the lines one at a time (use a BufferedReader), parse the line and check for certain criteria. For example, you can check to see if there is a space anywhere in the string, and then store it to display to the user. You can also check for double @ symbols, and anything else that constitutes an invalid email. I've included some code to demonstrate.


public class FindInvalidEmail
{
private BufferedReader emailFile;

// Creates a BufferedReader object using the fileName string
// for the path and file name
public FindInvalidEmail(String fileName)
{
emailFile = new BufferedReader(new FileReader(fileName));
}

// Finds the invalid email and returns it
public String parseString()
{
String lineOfFile = "";
String invalidEmail = "There are no invalid email addresses";
while ((lineOfFile = emailFile.readLine()) != "")
{
for (int i=0; i {
if (lineOfFile.charAt(i) == ' ')
{
invalidEmail = "The invalid email address is " + lineOfFile
}
}
}
return invalidEmail;
}

public static void main(String[] args)
{
FindInvalidEmail invEm = new FindInvalidEmail("c:\\test.txt");
System.out.println(invEm.parseString());
}


That should do it, and you could modify it to return a string array that holds all the invalid addresses, if need be. Hope this helps.
Hiran


> >


> > public class testproggie
> > {
> > public static void main(String args[])
> > {
> > byte invalidemail[]={112,108,101,97,115,101,32,100,111,32,121,111,117,114,32,111,119,110,32,104,111,109,101,119,111,114,107};
> > FileInputStream in=new FileInputStream("images\\aa.txt");
> > int b=0;
> > while (true)
> > {
> > b=in.read();
> > if(b==-1) break;
> > }
> > System.out.println("The invalid address is : " + new String(invalidemail));
> > }
> > }
> >






Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us