Code Explosion

Code that explodes conventions

find out the first non-repeated word in the file

Problem # 1: Suppose you have a plain text file. Write a simple java program to find out the first non-repeated word in the file.

Note:  this problem is given by Therap Java Fest in their facebook page.

Solution:

Finding solution for this problem is quite easy.  We have two parts in this program. First one, we have to read text from file and then find the first non-repeated word in the text . So we need to write two methods.  Say they are, readTextFromFile() and findFirstNonRepeatedWord().

First method is pretty straightforward. It will take a text file name with its path as string.  And then read the text file and return as a String.  So first let’s create a file object and put the file name of its constructor argument.  And then create another object of BufferedRader and put a FileReader object argument to its constructor and FileReader ’s constructor takes the File object as its constructor argument.

There are some benefits using BufferedReader. It can speed up IO quite a bit. Using its readLine()  method, we can read a single line at time in the loop rather than read character one by one.

As this method reads all the text and returns as a single string, so a lot of concatenation is required for adding every single line to a String which is returned by this method. String is an immutable object. So if the file is large, here its needed a lot of concatenation which is costly for immutable object. So it’s better to use StringBuilder.  StringBuilder is mutable object and it’s much faster.

Now let’s talk about second method.

We have String now. Let’s split it first using split method in string. Split method takes a string regex and returns an array of String. In our case, let’s split our string using single space. We need a map now. Map is an interface provided by Java collection Framework. Map cannot contain duplicate keys; each key can map to at most one value. Here map has string key and integer value. The key is our word and integer is a counter.

We use a foreach loop for the array of string. Inside the loop we use an if-checking. We find integer value from the map using string word as a value. If the word is found the map, the get method returns the value, otherwise it return null. If returns value is null which means no value for that word in the map then we insert a ’1′. If there is already a value for that word, increment the value by 1.

After the first part is done, we use for-each loop in the String array which we first splitted from the parameter string. Then look up every word in the map.  If there is a 1, return that word because it is the first non-repeating word. If none of words have value ‘1’ then returns null which means there are no non-repeating words in the file.

Source Code:


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;

public class FirstNonRepeatedWordFinder {

public static String readTextFromFile(String fileName)
throws FileNotFoundException {
StringBuilder builder = new StringBuilder();
File file = new File(fileName);
// check if file is exist or not, if not exist throw a Exceptions
if (!file.exists()) {
throw new FileNotFoundException();
}

BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(file));
String line = "";
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}

public static String findNonRepeatedWords(String text) {

Map<String, Integer> map = new Hashtable<>();
Integer intgr = null;

String[] split = text.split(" "); // split this text using single space.

for (String str : split) {

intgr = map.get(str);
// If there is no value for that word in the
// map then insert a '1'
if (intgr == null) {
map.put(str, new Integer(1));
} else {
// else if there is already a value for that word, increment the
// value by 1
map.put(str, new Integer(intgr.intValue() + 1));
}
}
// /System.out.println(map);

for (String str : split) {
if (map.get(str).intValue() == 1) {
return str;
}
}

return null;
}

public static void main(String[] args) {
try {
System.out
.println("First Non Repeated Word is: "
+ findNonRepeatedWords(readTextFromFile("findNonRepeatedWord.txt")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

Download Source : CLICK HERE

Keep calm and carry on coding and smile.

About these ads

2 Comments on “find out the first non-repeated word in the file

  1. Collin
    September 10, 2012

    Nice article. Very clearly and simply demonstrated.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Information

This entry was posted on September 10, 2012 by in Java, Java Puzzle, Tips.

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 291 other followers

RSS Jenkov.com

  • Java Exception Handling - New book published!
    Download this book for free from Amazon until and including march 1st! Exception handling is an aspect of Java development that has not received as much attention as it deserves. If you do not have exception handling under control, you risk that your application or data ends up in an unhealthy state, which can have serious consequences. That is why I wrote t […]
  • HTML4 Compressed - Kindle book published
    In this day and age it should be easy and cheap to learn HTML4, which everyone needs to learn in order to learn HTML5. Therefore I published this Kindle book which explains HTML4. The parts of HTML4 that is not removed in HTML5, that is.
  • jQuery Compressed - Updated
    I have updated the jQuery Compressed book to be compatible with version 1.9.1 of jQuery.
  • Displaying SVG in Browsers
    The SVG tutorial has been updated, showing how to include SVG in HTML pages using the img HTML element, or the SVG element.
  • Maven Tutorial.
    This Maven tutorial explains the core concepts of Maven which are important to understand for new Maven users.

my tweets

Follow

Get every new post delivered to your Inbox.

Join 291 other followers