Read a word from a file - java

If someone could help me figure out how to search if a word exists in a file, I would greatly appreciate it. I do know how to read an entire text file though.
And this is what I have so far:
public static void main(String[] args) throws IOException {
File file = new File("words.txt");
Scanner sc = new Scanner(System.in);
System.out.println("Enter a word you would like to search for:");
String word = sc.nextLine();
List<String> words = new ArrayList<>();
try {
sc = new Scanner(file).useDelimiter( ",");
while (sc.hasNext()) {
final String wordFromFile = sc.nextLine();
if (wordFromFile.contains(word)) {
// a match!
System.out.println("The entered word: " + word + " exists in the dictionary");
break;
}
}
} catch (IOException e) {
System.out.println(" cannot write to file " + file.toString());
}
}
}

Just iterate through all the words in file an insert each into a HashSet from the file first. This is linear time O(n) to accomplish, no way around this as you got to read in the whole file.
Assuming one word from file it's like:
HashSet<String> set = new HashSet<>();
while (sc.hasNext()) {
set.add(sc.nextLine();
}
If someone a sticker any they really want it read to a list type collection, you can generate a HashSet like this from the list:
Set<String> set = new HashSet<>(wordList);
Note: This conversion operation is also O(n), so to read it into a list and convert you're O(2n), which is still O(n), but if this list is long far from optimal
For the lookup and/or insertion of the new word you check, can then do it in O(1) time.
if (set.contains(word)) {
//...blah..blah...bla...
} else {
set.add(word);
}
Hence the hash in the name HashSet.

This might help you understand
public static void main(String a[]){
File file = new File("words.txt");
Scanner sc = new Scanner(System.in);
System.out.println("Enter a word you would like to search for:");
String word = sc.nextLine();
boolean exist = false;
List<String> words = new ArrayList<String>();
sc = new Scanner(file);
while(sc.hasNext()){
words.add(sc.next());
}
for(int i=0;i<words.size();i++){
if(words.get(i).equals(word)){
System.out.println("The entered word: " + word + " exists in the dictionary");
exist = true;
break;
}
}
if(!exist){
System.out.println("This word is not in the dictionary.");
System.out.println("Do you want to add it");
if(System.in.read() == 'y'){
words.add(word);
}
}
}

Related

Simple java arraylist programming question

I am trying to tell whether that string was found in the list or not.
For instance, if I put Max in my list and search for Max, it should say "Max was found" If not, then it should say "Max was not found"
I do not know how to approach to getting the answer from here.
import java.util.ArrayList;
import java.util.Scanner;
public class OnTheList {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<String> list = new ArrayList<>();
while (true) {
String input = scanner.nextLine();
if (input.equals("")) {
break;
}
list.add(input);
}
System.out.print("Search for? ");
System.out.print(scanner.nextLine());
if (list.contains(list)) ----> I think this is the part where I am not getting it
System.out.println(" was found!");
else
System.out.println(" was not found");
}
}
You may store the word to search, here you ask for it with scanner.nextLine() and print it but didn't save it. Then use the variable you saved the word in, to check into the List
System.out.print("Search for? ");
String toSearch = scanner.nextLine();
if (list.contains(toSearch))
System.out.println(toSearch + " was found!");
else
System.out.println(toSearch + " was not found");
Here you didn't store the user input that you are getting from Search for ,and you are trying to search an element of the list but passing list as the argument for the contains() method ,So first store the user input for a variable the search that input by passing it as the argument to contains() method and make sure to close the scanner variable at the end of the program to avoid memory leaks like below.
import java.util.ArrayList;
import java.util.Scanner;
public class OnTheList {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<String> list = new ArrayList<>();
while (true) {
String input = scanner.nextLine();
if (input.equals("")) {
break;
}
list.add(input);
}
System.out.print("Search for? ");
String toSearch = scanner.nextLine();
if (list.contains(toSearch))
System.out.println(" was found!");
else
System.out.println(" was not found");
scanner.close();
}
}

Text analysis Word Counter Java

I need to write code that reads and does a text analysis of a file. One of the things it needs to do is to count how many words there are in the file. I wrote a method countWords, but when I run the program it returns 0. The text file I am using contains the following:
Ask not what your country can do for you
ask what you can do for your country
So it clearly should return 17 and not 0. What did I do wrong?
public class TextAnalysis {
public static void main (String [] args) throws IOException {
File in01 = new File("a5_testfiles/in01.txt");
Scanner fileScanner = new Scanner(in01);
System.out.println("TEXT FILE STATISTICS");
System.out.println("--------------------");
System.out.println("Length of the longest word: " + longestWord(fileScanner));
System.out.println("Number of words in file wordlist: " );
countWords(fileScanner);
}
public static String longestWord (Scanner s) {
String longest = "";
while (s.hasNext()) {
String word = s.next();
if (word.length() > longest.length()) {
longest = word;
}
}
return (longest.length() + " " + "(\"" + longest + "\")");
}
public static void countWords (Scanner s) throws IOException {
int count = 0;
while(s.hasNext()) {
String word = s.next();
count++;
}
System.out.println(count);
}
try this?
void countWords()
{
String temp;
File path = new File("c:/Bala/");//give ur path
File file = new File(path, "Bala.txt");//give ur filename
FileReader fr = new FileReader(file);
char cbuf[] = new char[(int) file.length()];
fr.read(cbuf);
temp = new String(cbuf);
String count[]=test.split("\\s");
System.out.println("Count:"+t.length);
}
You already read the scanner and reading it again. just create another scanner to use in count words method
fileScanner = new Scanner(<your file object>);
before
countWords(fileScanner);
Hope this helps.
Declare a new scanner for your count words method, the problem lies under s.next(); it reads the next word in your buffer and discard the previous ones, so after you called your longest word method, the scanner buffer has been used up.

I have to make a loop taking a users input until "done" is entered

I'm trying to make an ArrayList that takes in multiple names that the user enters, until the word done is inserted but I'm not really sure how. How to achieve that?
ArrayList<String> list = new ArrayList<String>();
String input = null;
while (!"done".equals(input)) {
// prompt the user to enter an input
System.out.print("Enter input: ");
// open up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// read the input from the command-line; need to use try/catch with the
// readLine() method
try {
input = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read input!");
System.exit(1);
}
if (!"done".equals(input) && !"".equals(input))
list.add(input);
}
System.out.println("list = " + list);
I would probably do it like this -
public static void main(String[] args) {
System.out.println("Please enter names seperated by newline, or done to stop");
Scanner scanner = new Scanner(System.in); // Use a Scanner.
List<String> al = new ArrayList<String>(); // The list of names (String(s)).
String word; // The current line.
while (scanner.hasNextLine()) { // make sure there is a line.
word = scanner.nextLine(); // get the line.
if (word != null) { // make sure it isn't null.
word = word.trim(); // trim it.
if (word.equalsIgnoreCase("done")) { // check for done.
break; // End on "done".
}
al.add(word); // Add the line to the list.
} else {
break; // End on null.
}
}
System.out.println("The list contains - "); // Print the list.
for (String str : al) { // line
System.out.println(str); // by line.
}
}
String[] inputArray = new String[0];
do{
String input=getinput();//replace with custom input code
newInputArray=new String[inputArray.length+1];
for(int i=0; i<inputArray.length; i++){
newInputArray[i]=inputArray[i];
}
newInputArray[inputArray.length]=input
intputArray=newInputArray;
}while(!input.equals("done"));
untested code, take it with a grain of salt.
ArrayList<String> names = new ArrayList<String>();
String userInput;
Scanner scanner = new Scanner(System.in);
while (true) {
userInput = scanner.next();
if (userInput.equals("done")) {
break;
} else {
names.add(userInput);
}
}
scanner.close();

big txt file to String? [duplicate]

This question already has answers here:
Reading a plain text file in Java
(31 answers)
Closed 9 years ago.
okay so I know this code isn't entirely orthodox, but regardless it compiles & runs. the problem is once i input a txt file via the command line it only converts the first line in the file into String text. (yes, i know im using the nextLine() method.that is temp until i find a better way). How can i get the entire txt file, that has line breaks, into one string? thanks in advance for any suggestions/ tips.
import java.util.*;
public class Concordance{
static Scanner kb;
public static void main(String arg[]){
//input text file, create array, create List, and call other methods
kb = new Scanner(System.in);
String text = kb.nextLine();
String[] words = text.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+");
List<String> list = Arrays.asList(text.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+"));
System.out.println("number of words in text (including duplicates) is: " + words.length);
System.out.println("");
alphaPrint(list);
System.out.println("");
uniqueWord(list);
}//end main
//prints text in alphabetical order and counts unique words
public static void alphaPrint(List<String> list){
int count = 0;
TreeSet<String> uniqueWord = new TreeSet<String>(list);
System.out.println("text in alphabetical order: ");
Collections.sort(list);
for (String word : uniqueWord) {
System.out.println(word);
count++;
}
System.out.println("");
System.out.println("unique word count is: " + count);
}//end alphaprint
//method will find and print frequency counts
public static void uniqueWord(List<String> list){
System.out.println("text with word frequencies: ");
TreeSet<String> uniqueWord = new TreeSet<String>(list);
for (String word : uniqueWord) {
System.out.println(word + ": " + Collections.frequency(list, word));
}
}//end unique word
}//end class
Maybe something like this
StringBuilder s = new StringBuilder();
while(kb.hasNextLine()){
s.append(kb.nextLine())
}
text = s.toString();
Or maybe build the array in the while loop.. using kb.hasNext(pattern)
---- Edit
You can run the the application using ./java filename < textfile.txt
The code must iterate through the supplied file using a loop. Here is an example:
public class FileToString {
public static void main(String[] args) {
System.out.println("Please Enter a File:");
Scanner scanner = new Scanner(System.in);
String fileName = scanner.nextLine();
Scanner fileScanner;
try {
File file = new File(fileName);
fileScanner = new Scanner(file);
String text = "";
while (fileScanner.hasNext()) {
text += fileScanner.nextLine();
}
System.out.println(text);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

Using one text file to search through another text file

So I've been trying to get this to work for some time. Let me preface this by saying that I'm not a programmer. It's more a of a hobby that I've recently taken up. I've been trying to get 2 text files to search through each other line by line. i.e. One has a bunch of words (around 10, one per line), and the other has many more (close to 500) also one per line. What I would like is for my program to say how many times each of the words in the smaller text file appears in the larger one. What i have so far is:
import java.util.Scanner;
import java.io.File;
import java.util.regex.Pattern;
public class StringSearch
{
public static void main (String args[]) throws java.io.IOException
{
int tot = 0;
Scanner scan = null;
Scanner scan2 = null;
String str = null;
String str2 = null;
File file = new File("C:\\sample2.txt");
File file2 = new File("C:\\sample3.txt");
scan = new Scanner(file);
scan2 = new Scanner(file2);
while (scan.hasNextLine())
{
str = scan.nextLine();
tot = 0;
while (scan2.hasNextLine())
{
str2 = scan2.nextLine();
if(str.equals(str2))
{
tot++;
}
}
System.out.println("The String = " + str + " and it occurred " + tot + " times");
}
}
}
Not sure why this isnt working. It reads the first word in the first text file fine and counts how many times it appears in the second one, but then it just stop and doesnt move on the the second word in the first file. I hope that makes sense. Something is wrong with the second while loop I think, but I have no idea what.
So, any help would be greatly appreciated. I'm hoping to get this to work and move on to more complicated projects in the future. Gotta start somewhere right?
Cheers Guys
The issue you are running across is that you are using a scanner within a scanner. The way that you currently have your scanners nested, it causes one scanner to completely read through its entire text file for the first word, but after that first run through, it has already read the entire file and will never return true for scan2.hasNextLine().
A better way to achieve what you want is what remyabel stated. You should create an array that will contain all of the words from your small file that will be iterated through every time you go through a word in your other file. You would also need to create something to keep track of how many times each word is hit so you could use something like a hashmap.
It would look something along the lines of this:
Scanner scan = null;
Scanner scan2 = null;
String str = null;
String str2 = null;
File file = new File("C:\\sample2.txt");
File file2 = new File("C:\\sample3.txt");
scan = new Scanner(file);
scan2 = new Scanner(file2);
//Will contain all of your words to check against
ArrayList<String> dictionary = new ArrayList<String>();
//Contains the number of times each word is hit
HashMap<String,Integer> hits = new HashMap<String, Integer>();
while(scan.hasNextLine())
{
str = scan.nextLine();
dictionary.add(str);
hits.put(str, 0);
}
while (scan2.hasNextLine())
{
str2 = scan2.nextLine();
for(String str: dictionary)
{
if(str.equals(str2))
{
hits.put(str, hits.get(str) + 1);
}
}
}
for(String str: dictionary)
{
System.out.println("The String = " + str + " and it occurred " + hits.get(str) + " times");
}
}
Create a buffered reader and read the file into a map of <String, Integer>:
String filename = args[0];
BufferedReader words = new BufferedReader(new FileReader(FILENAME));
Map<String, Integer>m = new HashMap<String, Integer>();
for(String word: words.readLine()){
if(word!=null && word.trim().length()>0) {
m.add(String, 0);
}
}
Then read the words list and increment the map's value each time you find one:
String filename = args[1];
BufferedReader listOfWords = new BufferedReader(new FileReader(FILENAME2));
for(String word: listOfWords.readLine()){
if(word!=null && word.trim().length()>0) {
if(m.get(word)!=null){
m.add(word, m.get(word) + 1);
}
}
}
Then print the results:
for(String word: map.keys()){
if(map.get(word)>0){
System.out.println("The String = " + word + " occurred " + map.get(word) + " times");
}
}
Your approach with using nested loops would scan the second file for every word in the first one. This would be highly inefficient. I suggest loading the first file in a HashMap.
Not only this would leverage on quick lookups, you could update the count of occurrence easily as well. Not to mention, you would be scanning the second file just once and any duplicates that you might have in the first one would automatically be ignored (as the results would be the same).
Map<String, Integer> wordCounts = new HashMap<String, Integer>();
Scanner scanner = new Scanner("one\nfive\nten");
while (scanner.hasNextLine()) {
wordCounts.put(scanner.nextLine(), 0);
}
scanner.close();
scanner = new Scanner("one\n" + // 1 time
"two\nthree\nfour\n" +
"five\nfive\n" + // 2 times
"six\nseven\neight\nnine\n" +
"ten\nten\nten"); // 3 times
while (scanner.hasNextLine()) {
String word = scanner.nextLine();
Integer integer = wordCounts.get(word);
if (integer != null) {
wordCounts.put(word, ++integer);
}
}
scanner.close();
for (String word : wordCounts.keySet()) {
int count = wordCounts.get(word);
if (count > 0) {
System.out.println("'" + word + "' occurs " + count + " times.");
}
}
Output :
'ten' occurs 3 times.
'five' occurs 2 times.
'one' occurs 1 times.
Its just a simple logic issue..
add following statement below System.out.println
scan2 = new Scanner(file2);

Categories