big txt file to String? [duplicate] - java

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();
}
}
}

Related

Words and lines counter from user input for Java

I have done this code, it prints correctly the total number of lines but for the total number of words it always prints total of 1 word. Can someone help me please, Thanks!
import java.util.*;
public class LineAndWordCounter{
public static void main(String[]args){
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
String line = scan.next();
linesCounter(scan);
wordsCounter(new Scanner(line) );
}
}
public static void linesCounter(Scanner linesInput){
int lines = 0;
while(linesInput.hasNextLine()){
lines++;
linesInput.nextLine();
}
System.out.println("lines: "+lines);
}
public static void wordsCounter(Scanner wordInput){
int words = 0;
while(wordInput.hasNext()){
words++;
wordInput.next();
}
System.out.println("Words: "+words);
}
}
This looks rather complicated to me.
You can just save each line in an ArrayList and accumulate the words in a variable.
Something like this:
List<String> arrayList = new ArrayList<>();
int words = 0;
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
String line = scan.nextLine();
arrayList.add(line);
words += line.split(" ").length;
System.out.println("lines: " + arrayList.size());
System.out.println("words: " + words);
}
scan.close();
You should also not forget to call the close() method o the Scanner to avoid a resource leak
scan.next()
returns the next "word".
If you create a new Scanner out of that one word, it will only ever see one word.
This will happen with
String line = scan.next();
wordsCounter(new Scanner(line) );

Read a word from a file

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);
}
}
}

Writing a program in Java to read in multiple strings from user and compare to text file

I am attempting to write a program that will take user input ( a long message of characters), store the message and search a text file to see if those words occur in the text file. The problem I am having is that I am only ever able to read in the first string of the message and compare it to the text file. For instance if I type in "learning"; a word in the text file, I will get a result showing that is is found in the file. However if I type "learning is" It will still only return learning as a word found in the file even though "is" is also a word in the text file. My program seems to not be able to read past the blank space. So I suppose my questions is, how do I augment my program to do this and read every word in the file? Would it also be possible for my program to read every word, with or without spaces, in the original message taken from the user, and compare that to the text file?
Thank you
import java.io.*;
import java.util.Scanner;
public class Affine_English2
{
public static void main(String[] args) throws IOException
{
String message = "";
String name = "";
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
System.out.println("Please enter in a message: ");
message = scan.next();
Scanner file = new Scanner(new File("example.txt"));
while(file.hasNextLine())
{
String line = file.nextLine();
for(int i = 0; i < message.length(); i++)
{
if(line.indexOf(message) != -1)
{
System.out.println(message + " is an English word ");
break;
}
}
}
}
}
I recommend you first process the file and build a set of legal English words:
public static void main(String[] args) throws IOException {
Set<String> legalEnglishWords = new HashSet<String>();
Scanner file = new Scanner(new File("example.txt"));
while (file.hasNextLine()) {
String line = file.nextLine();
for (String word : line.split(" ")) {
legalEnglishWords.add(word);
}
}
file.close();
Next, get input from the user:
Scanner input = new Scanner(System.in);
System.out.println("Please enter in a message: ");
String message = input.nextLine();
input.close();
Finally, split the user's input to tokens and check each one if it is a legal word:
for (String userToken : message.split(" ")) {
if (legalEnglishWords.contains(userToken)) {
System.out.println(userToken + " is an English word ");
}
}
}
}
You may try with this. With this solution you can find each word entered by the user in your example.txt file:
public static void main(String[] args) throws IOException
{
String message = "";
String name = "";
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
System.out.println("Please enter in a message: ");
message = scan.nextLine();
Scanner file = new Scanner(new File("example.txt"));
while (file.hasNextLine())
{
String line = file.nextLine();
for (String word : message.split(" "))
{
if (line.contains(word))
{
System.out.println(word + " is an English word ");
}
}
}
}
As Mark pointed out in the comment, change
scan.next();
To:
scan.nextLine();
should work, i tried and works for me.
If you can use Java 8 and Streams API
public static void main(String[] args) throws Exception{ // You need to handle this exception
String message = "";
Scanner input = new Scanner(System.in);
System.out.println("Please enter in a message: ");
message = input.nextLine();
List<String> messageParts = Arrays.stream(message.split(" ")).collect(Collectors.toList());
BufferedReader reader = new BufferedReader(new FileReader("example.txt"));
reader.lines()
.filter( line -> !messageParts.contains(line))
.forEach(System.out::println);
}
You have many solution, but when it comes to find matches I suggest you to take a look to the Pattern and Matcher and use Regular Expression
I haven't fully understood your question, but you could do add something like this (I did not tested the code but the idea should work fine):
public static void main(String[] args) throws IOException{
String message = "";
String name = "";
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
System.out.println("Please enter in a message: ");
message = scan.next();
Scanner file = new Scanner(new File("example.txt"));
String pattern = "";
for(String word : input.split(" ")){
pattern += "(\\b" + word + "\\b)";
}
Pattern r = Pattern.compile(pattern);
while(file.hasNextLine())
{
String line = file.nextLine();
Matcher m = r.matcher(line);
if(m.matches()) {
System.out.println("Word found in: " + line);
}
}
}

Using four methods to count and display the lines, words, and characters in a file in java

This is my first time using more than one method in a progam and I have also never tried to count lines, words and letters in a file. So I've read three Java books file sections and then looked at questions on the forum here and had a go. I think I am doing something wrong with variable names, as I don't know the rules with where you put them in in multi method etc. If you could say something like that is glaringly wrong because you have to do this, that would be helpful. I can't use the debugger because I can't compile it. Anyway, thanks for looking at this.
import java.util.Scanner;
import java.io.*;
/**
* A program to count the number of lines, words and characters in a text file.
*
* #author
* #version 2014
*/
public class WordCount
{
public static void main(String[] args) throws FileNotFoundException
{
// get the filename
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the name of the file: ");
String filename = keyboard.nextLine();
File file = new File(filename);
Scanner inputFile = new Scanner(file);
lines(filename);
words(filename);
characters(filename);
}
/**
* Count the number of lines in a file, and print out the result.
*
*/
public static void lines(String filename) throws FileNotFoundException
{
Scanner inputFile = new Scanner(file);
int lines = 0;
while (inputFile.hasNextLine())
{
lines++;
file.nextLine();
}
return lines;
inputFile.close();
}
/**
* Count the number of words in a file, and print out the result.
*
*/
public static void words(String filename) throws FileNotFoundException
{
Scanner inputFile = new Scanner(file);
int words = 0;
while (input.hasNextLine())
{
String word = input.next();
words++;
}
return words;
inputFile.close();
}
/**
* Count the number of characters in a file, and print out the result.
*
*/
public static void characters(String filename) throws FileNotFoundException
{
Scanner inputFile = new Scanner(file);
int characters = 0;
while (inputFile.hasNextLine())
{
String line = inputFile.nextLine();
for(int i = 0; i < line.length(); i++)
{ characters = line.charAt(i);
if (character != 32)
{
characters++;
}
}
}
return characters;
inputFile.close();
System.out.println("The number of lines is: " + lines);
System.out.println("The number of words is: " + words);
System.out.println("The number of characters is: " + characters);
System.out.println();
}
}
Change the following
public static int lines(String filename) throws FileNotFoundException
//change the return type to int instead of void
{
Scanner inputFile = new Scanner(filename); //filename instead of file
int lines = 0;
while (inputFile.hasNextLine())
{
lines++;
inputFile.nextLine(); // ---do----
}
inputFile.close(); // after return it will become unreachable
return lines;
}
Same applies to other methods
Your code contains several problems. So let us fix that one by one in each method:
public static void main(String[] args) throws FileNotFoundException
{
// get the filename
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the name of the file: ");
String filename = keyboard.nextLine();
keyboard.close(); // <- don't forget to close this resource
File file = new File(filename);
//Scanner inputFile = new Scanner(file); // <- this line is unnecessary, because you never use that variable
lines(file); // <- you already obtained a File object, so use it ;)
words(file);
characters(file);
}
Next method:
public static void lines(File file) throws FileNotFoundException // mind the new argument type
{
Scanner inputFile = new Scanner(file);
int lines = 0;
while (inputFile.hasNextLine())
{
lines++;
inputFile.nextLine(); //<- use "inputFile" instead of "file"
}
inputFile.close();
//return lines; // <- you can't return something if your method uses "void" as the return type
//your comment on that method says that you like to print the result, so let's do that
System.out.println("The number of lines is: " + lines);
}
And the next one:
public static void words(File file) throws FileNotFoundException // <- new argument type
{
Scanner inputFile = new Scanner(file);
int words = 0;
while (inputFile.hasNextLine())
{
//String word = inputFile.next(); // that won't work like you think, but we can do a little "trick" here
String line = inputFile.nextLine(); // read the current line of text
words += line.split(" ").length; // split the line using whitespace and add the number of words to the current value of variable "words"
//words++; // <- not needed anymore
}
//return words; // <- like before: not possible if return type is void
inputFile.close();
//your comment on that method says that you like to print the result, so let's do that
System.out.println("The number of words is: " + words);
}
And the last method:
public static void characters(File file) throws FileNotFoundException
{
Scanner inputFile = new Scanner(file);
int characters = 0;
int count = 0; // new variable to count the characters
while (inputFile.hasNextLine())
{
String line = inputFile.nextLine();
for(int i = 0; i < line.length(); i++)
{
characters = line.charAt(i); // <- no harm in using a new line :)
if (characters != 32) // <- you wrote "character"
{
count++; // <- "characters" is the current character itself, we won't increase that. For example "a" would become "b" and we don't want/need that :)
}
}
}
//return characters; // <- you know that already ...
inputFile.close();
//System.out.println("The number of lines is: " + lines); // <- "lines" and "words" are unknown here, but we printed these results already
//System.out.println("The number of words is: " + words);
System.out.println("The number of characters is: " + count); // <- print the count of characters
System.out.println();
}
After this changes you code should work now :).
It seems the methods only need the File object.
int n = countLines(file);
System.out.println("Lines: " + n);
...
public static int countLines(File textFile) throws IOException
{
Scanner inputFile = new Scanner(textFile);
int lines = 0
while (inputFile.hasNextLine())
{
lines++;
inputFile.nextLine();
}
inputFile.close();
return lines;
}
For nextLine you asked the wrong object, Scanner needed.
return returns from the method, so close has come before.
As you return the line count, you must return int.
Method names contain a verb by convention.
The method knows no file or filename.
It also cannot alter a variable if you pass a variable, like file,
You can give the new paramaeter the same name, but for clarity not done here.
Problem number 1:
public static void lines(String filename) throws FileNotFoundException
{
Scanner inputFile = new Scanner(file);
int lines = 0;
while (inputFile.hasNextLine())
{
lines++;
file.nextLine();
}
return lines; // <----------------
inputFile.close();
}
This is a void method but you have a return lines statement. Either remove it or change the method header to public static int lines(String filename) throws FileNotFoundException
Problem number 2
public static void words(String filename) throws FileNotFoundException
{
Scanner inputFile = new Scanner(file);
int words = 0;
while (input.hasNextLine())
{
String word = input.next();
words++;
}
return words; <-------------
inputFile.close();
}
Again here you ask from a void method to return a value. Change method header to public static int words(String filename) throws FileNotFoundException
Problem number 3:
public static void characters(String filename) throws FileNotFoundException
{
Scanner inputFile = new Scanner(file);
int characters = 0;
while (inputFile.hasNextLine())
{
String line = inputFile.nextLine();
for(int i = 0; i < line.length(); i++)
{ characters = line.charAt(i);
if (character != 32)
{
characters++;
}
}
}
return characters; //<---------------
inputFile.close();
System.out.println("The number of lines is: " + lines);
System.out.println("The number of words is: " + words);
System.out.println("The number of characters is: " + characters);
System.out.println();
}
Again here you have a return int statement on a void method. Just change the method header to this: public static int characters(String filename) throws FileNotFoundException

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.

Categories