Creating email addresses from names in a text file - java

I've got a text file with a name (first+family name) on every line, from these names I want to create email addresses (in this format : firstname.familyname#address.com.).
My plan is to do something like this:
Make an array with a string for every line in the text file
Split every string in two parts: first name, family name. Where the first name is the first word in the string and the family name is every other word.
Make a new array (splitNameN) for every string (N total strings) with two elements: first name, family name
From each splitNameN array, create a new string: "first name" + "." +"family name"+"#mail.com"
Add all these strings together seperated by a comma (eg: barrack.obama#mail.com;donald.trump#mail.com)
The main thing I dont know how to do is splitting the names and putting them into correct arrays, I can split them but then they lose their grouping.
public static void main(String[] args) throws IOException
{
File file = new File("D:\\NetbeansProjects\\Emails\\src\\emails\\lijst.txt");
BufferedReader abc = new BufferedReader(new FileReader(file));
List<String> data = new ArrayList<String>();
String s;
while((s=abc.readLine())!=null) {
data.add(s);
}
abc.close();
System.out.println(data);
List<String> splitdata = new ArrayList<String>();
for(String strLine : data) {
String[] strWord = strLine.split("[\\s']");
for (String c : strWord) {
splitdata.add(c);
}
}
System.out.println(splitdata);
}

After reading data you can create an 2d array and store first and last name there and then concatenate them to create an email address as you asked for.
String[][] splitdata = new String[data.size()][2];
int rowNum = 0;
for (String strLine : data) {
String[] strWord = strLine.split("[\\s]");
// Store it in an array as you asked for or join them right here
splitdata[rowNum][0] = strWord[0];
splitdata[rowNum][1] = strWord[1];
++rowNum;
}
for (String[] row: splitdata) {
System.out.println(row[0] + "." + row[1] + "#mail.com");
}
If you are using java8 the whole thing as be written as..
Path path = Paths.get("D:\\NetbeansProjects\\Emails\\src\\emails\\lijst.txt");
String commaJoinedEmails = String.join(",", Files.lines(path)
.map(line -> line.split("\\s"))
.map(tokens -> tokens[0] + "." + tokens[1] + "#mail.com")
.toArray(String[]::new));

I would suggest you to read the file line by line.
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
// process the line.
}
This is the most common way of doing it. In the current line parameter you will store the first and family names and after that you can process the two names (preferably with a StringBuffer) and add the result to the final string of your choice.
Good luck :)

You are missing a part in your loop from your explication your lines is like :
firstname1 familyname1
firstname2 familyname2
So after you split you can easily use :
for (String strLine : data) {
String[] strWord = strLine.split("[\\s']");
splitdata.add(strWord[0] + "." + strWord[1] + "#mail.com");
//------------first name last name
}
System.out.println(splitdata);
Output :
[firstname1.familyname1#mail.com, firstname2.familyname2#mail.com]
Note you have to verify your names before you use them, what if the name have many parts, also you have to use the right delimiter between the names.

Related

How do I split multiple lines with multiple delimeters?

I have a .txt file with multiple lines with many emails such as Mike#sport.member.com, Laura#music.member.com, Quinn#music.member.com. How do I split them so I can add them to seperate arraylists like music or sport?
Mike: sport
Laura: music
Quinn: music
Thanks so much.
you can use regex
Pattern pattern = Pattern.compile("(\\w+)#(\\w+).(\\w+).(\\w+)");
Matcher matcher = pattern.matcher("Laura#music.member.com");
if (matcher.find()) {
matcher.group(1); //this return the name
matcher.group(2); //this return music sport
matcher.group(3); // get the company name
matcher.group(4); // get the domain com in this case
}
use this to fill the arraylist as you like
and have a nice day :)
If you don't mind using ArrayUtils you can use this code:
String[] emails = new String[] {"Mike#sport.member.com", "Laura#music.member.com", "Quinn#music.member.com"};
int startLength;
String[][] split = new String[emails.length][];
for(int i = 0; i < emails.length; i++) {
split[i] = emails[i].split("#"); //split by #
startLength = split[i].length;
for(int j = 0; j < startLength; j++) {
split[i] = ArrayUtils.addAll(split[i], split[i][0].split("\\.")); //Split by . and add items to the end of the array
split[i] = ArrayUtils.remove(split[i], 0); //remove the first item of the array
}
}
System.out.println(Arrays.deepToString(split));
Output:
[[Mike, sport, member, com], [Laura, music, member, com], [Quinn, music, member, com]]
What this does is it uses .split("#") to split the emails into arrays separated by "#" Then goes through for array and splits each item in the array with .split(".").
As it splits the elements the second time it adds them to the end of the split array and removes the first item (which was the item that it just split up).
Note that I had to make the startLength variable to keep track of the original size of the split array because in the second loop its constantly changing length.
This gives you a 2D array with all the items but I won't blame you for no using this because its a bit of a mess.
There are two steps to solve this problem.
Read lines from file(a.txt).
public List<String> readFile() throws IOException {
String fileName = "/Users/folder/a.txt";
FileInputStream inputStream = new FileInputStream(fileName);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String str = null;
List<String> list = new ArrayList<>();
while ((str = bufferedReader.readLine()) != null) {
list.add(str);
}
//close
inputStream.close();
bufferedReader.close();
return list;
}
Split line, take out data.
List<String> lines = readFile();
Pattern pattern = Pattern.compile("([a-zA-Z]+)#([a-zA-Z]+)");
for (String line : lines) {
Matcher matcher = pattern.matcher(line);
if (matcher.find()){
System.out.println(matcher.group(1) + ":" + matcher.group(2));
}
}
The result is
Mike:sport
Laura:music
Quinn:music

check if a string is contained in a text file of words in java

I have a text file (collection of all valid english words) from a github project that looks like this words.txt
My text file is under the resources folder in my project.
I have also a list of rows obtained from a table in mysql.
What i'm trying to do is to check if all the words in a every row are valid english words, that's why I compare each row with the words contained in my file.
This what i've tried so far :
public static void englishCheck(List<String> rows) throws IOException {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
int lenght, occurancy = 0;
for ( String row : rows ){
File file = new File(classLoader.getResource("words.txt").getFile());
lenght = 0;
if ( !row.isEmpty() ){
System.out.println("the row : "+row);
String[] tokens = row.split("\\W+");
lenght = tokens.length;
for (String token : tokens) {
occurancy = 0;
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null ){
if ((line.trim().toLowerCase()).equals(token.trim().toLowerCase())){
occurancy ++ ;
}
if (occurancy == lenght ){ System.out.println(" this is english "+row);break;}
}
}
}
}
}
this works only for the very first rows, after that my method loops over the rows only displaying them and ignores the comparison, I would like to know why this isn't working for my set of rows, It works also if I predefined my list like this List<String> raws = Arrays.asList(raw1, raw2, raw3 ) and so on
You can use the method List#containsAll(Collection)
Returns true if this list contains all of the elements of the
specified collection.
lets assume you have both list flled myListFromRessources and myListFromRessources then you can do:
List<String> myListFromRessources = Arrays.asList("A", "B", "C", "D");
List<String> myListFromRessources = Arrays.asList("D", "B");
boolean myInter = myListFromRessources.containsAll(myListFromSQL);
System.out.println(myInter);
myListFromSQL = Arrays.asList("D", "B", "Y");
myInter = myListFromRessources.containsAll(myListFromSQL);
System.out.println(myInter);
You can read words.txt file, convert words into lower case, then put words into HashSet.
Use the boolean contains(Object o) or boolean containsAll(Collection<?> c); methods to compare each word.
The time was O(n).
TIP: Do not read file in every loop. Reading file is very very slow.
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("words.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
List<String> wordList = new LinkedList<String>(); // You do not know word count, LinkedList is a better way.
String line = null;
while ((line = reader.readLine()) != null) {
String[] words = line.toLowerCase().split("\\W+");
wordList.addAll(Arrays.asList(words));
}
Set<String> wordSet = new HashSet<String>(wordList.size());
wordSet.addAll(wordList);
// then you can use the wordSet to check.
// You shold convert the tokens to lower case.
String[] tokens = row.toLowerCase().split("\\W+");
wordSet.containsAll(Arrays.asList(tokens));
The reason your code doesn't work is that occurancy can never be anything other than 0 or 1. You can see that by following the logic or going through a debugger.
If your words.txt file is not too large, and you have enough RAM available, you can speed up processing by reading the words.txt file into memory at the start. Also, you only ever need to call toLowerCase() once, instead of every time you compare. However, be careful with locales. The following code should work as long as you haven't got any non-English characters such as a German eszett or a Greek sigma.
public static void englishCheck(List<String> rows) throws IOException {
final URI wordsUri;
try {
wordsUri = ClassLoader.getSystemResource("words.txt").toURI();
} catch (URISyntaxException e) {
throw new AssertionError(e); // can never happen
}
final Set<String> words = Files.lines(Paths.get(wordsUri))
.map(String::toLowerCase)
.collect(Collectors.toSet());
for (String row: rows)
if (!row.isEmpty()) {
System.out.println("the row : " + row);
String[] tokens = row.toLowerCase().split("\\W+");
if (words.containsAll(Arrays.asList(tokens)))
System.out.println(" this is english " + row);
}
}

Java - Read and storing in an array

I want to read the contents of a text file, split on a delimiter and then store each part in a separate array.
For example the-file-name.txt contains different string all on a new line:
football/ronaldo
f1/lewis
wwe/cena
So I want to read the contents of the text file, split on the delimiter "/" and store the first part of the string before the delimiter in one array, and the second half after the delimiter in another array. This is what I have tried to do so far:
try {
File f = new File("the-file-name.txt");
BufferedReader b = new BufferedReader(new FileReader(f));
String readLine = "";
System.out.println("Reading file using Buffered Reader");
while ((readLine = b.readLine()) != null) {
String[] parts = readLine.split("/");
}
} catch (IOException e) {
e.printStackTrace();
}
This is what I have achieved so far but I am not sure how to go on from here, any help in completing the program will be appreciated.
You can create two Lists one for the first part and se second for the second part :
List<String> part1 = new ArrayList<>();//create a list for the part 1
List<String> part2 = new ArrayList<>();//create a list for the part 2
while ((readLine = b.readLine()) != null) {
String[] parts = readLine.split("/");//you mean to split with '/' not with '-'
part1.add(parts[0]);//put the first part in ths list part1
part2.add(parts[1]);//put the second part in ths list part2
}
Outputs
[football, f1, wwe]
[ronaldo, lewis, cena]

Read the each string text from file in java

I am new in java. I just wants to read each string in java and print it on console.
Code:
public static void main(String[] args) throws Exception {
File file = new File("/Users/OntologyFile.txt");
try {
FileInputStream fstream = new FileInputStream(file);
BufferedReader infile = new BufferedReader(new InputStreamReader(
fstream));
String data = new String();
while ((data = infile.readLine()) != null) { // use if for reading just 1 line
System.out.println(""+data);
}
} catch (IOException e) {
// Error
}
}
If file contains:
Add label abc to xyz
Add instance cdd to pqr
I want to read each word from file and print it to a new line, e.g.
Add
label
abc
...
And afterwards, I want to extract the index of a specific string, for instance get the index of abc.
Can anyone please help me?
It sounds like you want to be able to do two things:
Print all words inside the file
Search the index of a specific word
In that case, I would suggest scanning all lines, splitting by any whitespace character (space, tab, etc.) and storing in a collection so you can later on search for it. Not the question is - can you have repeats and in that case which index would you like to print? The first? The last? All of them?
Assuming words are unique, you can simply do:
public static void main(String[] args) throws Exception {
File file = new File("/Users/OntologyFile.txt");
ArrayList<String> words = new ArrayList<String>();
try {
FileInputStream fstream = new FileInputStream(file);
BufferedReader infile = new BufferedReader(new InputStreamReader(
fstream));
String data = null;
while ((data = infile.readLine()) != null) {
for (String word : data.split("\\s+") {
words.add(word);
System.out.println(word);
}
}
} catch (IOException e) {
// Error
}
// search for the index of abc:
for (int i = 0; i < words.size(); i++) {
if (words.get(i).equals("abc")) {
System.out.println("abc index is " + i);
break;
}
}
}
If you don't break, it'll print every index of abc (if words are not unique). You could of course optimize it more if the set of words is very large, but for a small amount of data, this should suffice.
Of course, if you know in advance which words' indices you'd like to print, you could forego the extra data structure (the ArrayList) and simply print that as you scan the file, unless you want the printings (of words and specific indices) to be separate in output.
Split the String received for any whitespace with the regex \\s+ and print out the resultant data with a for loop.
public static void main(String[] args) { // Don't make main throw an exception
File file = new File("/Users/OntologyFile.txt");
try {
FileInputStream fstream = new FileInputStream(file);
BufferedReader infile = new BufferedReader(new InputStreamReader(fstream));
String data;
while ((data = infile.readLine()) != null) {
String[] words = data.split("\\s+"); // Split on whitespace
for (String word : words) { // Iterate through info
System.out.println(word); // Print it
}
}
} catch (IOException e) {
// Probably best to actually have this on there
System.err.println("Error found.");
e.printStackTrace();
}
}
Just add a for-each loop before printing the output :-
while ((data = infile.readLine()) != null) { // use if for reading just 1 line
for(String temp : data.split(" "))
System.out.println(temp); // no need to concatenate the empty string.
}
This will automatically print the individual strings, obtained from each String line read from the file, in a new line.
And afterwards, I want to extract the index of a specific string, for
instance get the index of abc.
I don't know what index are you actually talking about. But, if you want to take the index from the individual lines being read, then add a temporary variable with count initialised to 0.
Increment it till d equals abc here. Like,
int count = 0;
for(String temp : data.split(" ")){
count++;
if("abc".equals(temp))
System.out.println("Index of abc is : "+count);
System.out.println(temp);
}
Use Split() Function available in Class String.. You may manipulate according to your need.
or
use length keyword to iterate throughout the complete line
and if any non- alphabet character get the substring()and write it to the new line.
List<String> words = new ArrayList<String>();
while ((data = infile.readLine()) != null) {
for(String d : data.split(" ")) {
System.out.println(""+d);
}
words.addAll(Arrays.asList(data));
}
//words List will hold all the words. Do words.indexOf("abc") to get index
if(words.indexOf("abc") < 0) {
System.out.println("word not present");
} else {
System.out.println("word present at index " + words.indexOf("abc"))
}

Java - take name from string

I'm developing a Java application that make some statistic stuff.
This application take all data from a .txt file which is supplied by the user.
The first line of that file contains the name of the sets of data that follows like this:
velx,vely,velz
//various data
I need to analyze that first line and retrieve the three name of variables, I correctly get the first two but I'm not able to get the last one.
There the code to get names:
public ArrayList<String> getTitle(){
// the ArrayList originally is not here but in the class intestation
// I copied it here to simplify code's understanding
ArrayList<String> title = new ArrayList<String>();
try {
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
int titleN = 0;
String line = br.readLine(); //read the first line of file
String temp;
System.out.println(ManageTable.class.getName() + " Line: " + line);
int c = line.length();
for(int i = 0; i <c; i++){
if((line.charAt(i) == ',') || **ANOTHER CONDITION** ){
temp = sb.toString();
System.out.println(ManageTable.class.getName() +" Temp is: " + temp);
title.add(temp);
System.out.println(ManageTable.class.getName() + " Title added");
sb.delete(0, sb.length());
}else{
sb.append(line.charAt(i));
}
}
} catch (IOException ex) {
Logger.getLogger(ManageTable.class.getName()).log(Level.SEVERE, null, ex);
}
return title;
}
I need to add a second condition to the if statement in order to find out when the line is ended and save the last name, even if its not followed by ','
I tried using:
if((line.charAt(i) == ',') || (i==c))
but from the name I get, always miss a character.
How can I check the end of the line and so get the full name?
If line contains just three names separated by comma, you can do
String[] names = line.split(",");
No need for all this looping. You can just split the line around the comma to get an array:
String[] names = line.split(",");

Categories

Resources