Need help reading words between <> and saving to array - java

Hello all I am trying to take a predetermined .txt file as shown below and read only the words between the < >, then add them to an array and write them to an HTML file.
the txt file is as follows.
for some reason its not showing the words in-between the < > on here.
"" = word between < >
A, <""">
B, <""">
C, <""">
etc.
here is my current code for attempting to read from the file, I understand the .split and .delimeter problem is these methods read everything before the chosen character.
import java.io.*;
import java.util.Scanner;
public class TickTacTwice {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new File("/Users/Muhammed/Documents/tic_tac_twice.txt"));
while(sc.hasNextLine()) {
String S = sc.nextLine();
String [] words = S.split("<");
System.out.println(words[0]);
}
sc.close();
}
}
Any help would be much appreciated.
Thanks

It looks as though you are just printing the first item in the split array "words", is it possible the first occurrence of a <> is empty? To view all items in the array you can use a loop like the following:
var i;
for (i = 0; i < words.length; i++) {
System.out.println(words[i]);
}
Each of those lines will contain all characters until the next "<" so you will then need to trim to the ">"
An easier way to do this is using Regular Expressions - there are some good examples here on StackOverflow.
Edit: Combining your code with TAsks would produce what you are after:
import java.io.*;
import java.util.Scanner;
public class TickTacTwice {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new File("/Users/Muhammed/Documents/tic_tac_twice.txt"));
while(sc.hasNextLine()) {
String S = sc.nextLine();
Pattern pattern = Pattern.compile("<(.+?)>");
Matcher m = pattern.matcher(S);
while(m.find()) {
System.out.println(m.group(0));
}
}
sc.close();
}
}

Related

search for multiple strings from a text file in java

I'm trying to search of multiple words given from a user ( i used array to store them in ) from one txt file , and then if that word presented once in the file it will be displayed and if it's not it won't.
also for the words itself , if it's duplicated it will search it once.
the problem now when i search for only one it worked , but with multiple words it keeps repeated that the word isn't present even if it's there.
i would like to know where should i put the for loop and what's the possible changes.
package search;
import java.io.*;
import java.util.Scanner;
public class Read {
public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
String[] words=null;
FileReader fr = new FileReader("java.txt");
BufferedReader br = new BufferedReader(fr);
String s;
System.out.println("Enter the number of words:");
Integer n = sc.nextInt();
String wordsArray[] = new String[n];
System.out.println("Enter words:");
for(int i=0; i<n; i++)
{
wordsArray[i]=sc.next();
}
for (int i = 0; i <n; i++) {
int count=0; //Intialize the word to zero
while((s=br.readLine())!=null) //Reading Content from the file
{
{
words=s.split(" "); //Split the word using space
for (String word : words)
{
if (word.equals(wordsArray[i])) //Search for the given word
{
count++; //If Present increase the count by one
}
}
if(count == 1)
{
System.out.println(wordsArray[i] + " is unique in file ");
}
else if (count == 0)
{
System.out.println("The given word is not present in the file");
}
else
{
System.out.println("The given word is present in the file more than 1 time");
}
}
}
}
fr.close();
}
}
The code which you wrote is error prone and remember always there should be proper break condition when you use while loop.
Try the following code:
public class Read {
public static void main(String[] args)
{
// Declaring the String
String paragraph = "These words can be searched";
// Declaring a HashMap of <String, Integer>
Map<String, Integer> hashMap = new HashMap<>();
// Splitting the words of string
// and storing them in the array.
String[] words = new String[]{"These", "can", "searched"};
for (String word : words) {
// Asking whether the HashMap contains the
// key or not. Will return null if not.
Integer integer = hashMap.get(word);
if (integer == null)
// Storing the word as key and its
// occurrence as value in the HashMap.
hashMap.put(word, 1);
else {
// Incrementing the value if the word
// is already present in the HashMap.
hashMap.put(word, integer + 1);
}
}
System.out.println(hashMap);
}
}
I've tried by hard coding the values, you can take words and paragraph from the file and console.
The 'proper' class to use for extracting words from text is java.text.BreakIterator
You can try the following (reading line-wise in case of large files)
import java.text.BreakIterator;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import java.nio.file.Files;
import java.nio.file.Paths;
public class WordFinder {
public static void main(String[] args) {
try {
if (args.length < 2) {
WordFinder.usage();
System.exit(1);
}
ArrayList<String> argv = new ArrayList<>(Arrays.asList(args));
String path = argv.remove(0);
List<String> found = WordFinder.findWords(Files.lines(Paths.get(path)), argv);
System.out.printf("Found the following word(s) in file at %s%n", path);
System.out.println(found);
} catch (Throwable t) {
t.printStackTrace();
}
}
public static List<String> findWords(Stream<String> lines, ArrayList<String> searchWords) {
List<String> result = new ArrayList<>();
BreakIterator boundary = BreakIterator.getWordInstance();
lines.forEach(line -> {
boundary.setText(line);
int start = boundary.first();
for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) {
String candidate = line.substring(start, end);
if (searchWords.contains(candidate)) {
result.add(candidate);
searchWords.remove(candidate);
}
}
});
return result;
}
private static void usage() {
System.err.println("Usage: java WordFinder <Path to input file> <Word 1> [<Word 2> <Word 3>...]");
}
}
Sample run:
goose#t410:/tmp$ echo 'the quick brown fox jumps over the lazy dog' >quick.txt
goose#t410:/tmp$ java WordFinder quick.txt dog goose the did quick over
Found the following word(s) in file at quick.txt
[the, quick, over, dog]
goose#t410:/tmp$

Read the contents of the “dictionary.txt” and add all four letter words to an ADT ArrayList<String> words = new ArrayList<>();

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class BuildGraph {
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner((new File("dictionary.txt")));
ArrayList<String> words=new ArrayList<String>();
while(sc.hasNextLine()){
if(sc.next().length()==4){
words.add(sc.next());
//sc.next();
}
System.out.println(words);
}
// sc.close();
for (int i = 0; i < words.size(); i++) {
System.out.println(words.get(i));
}
}
}
This is my code, and I am trying to read only 4 letter words from a dictionary file, but when i run my code it gives me all the words from dictionary files
Thank-you in advance.
You code needs some tweaks. Basically you should not be calling sc.next() twice to get the same element as calling next() moves the pointer to the next element
Rewrite your while loop to this:
while(sc.hasNextLine() && sc.hasNext()){
String word = sc.next();
if(word.length()==4){
words.add(word);
}
System.out.println(words);
}

How to load text file into string array with only import java.io.* and no other library

Basically i added "import java.util.Scanner". but I wanted my code to work without that library and only "import java.io*" . However i want all my words (english word in the dictionary with the total of 109562 words in this case) in my text file to be inside the string array. Hence, in this case, without the scanner. how to do that?
import java.io.*;
import java.util.Scanner;
public class tester{
public static void main (String [] args) throws IOException{
File f = new File("C:/Users/alienware14/Documents/words.txt");
String [] words = new String [109562];
readWords(f , words);
/*
System.out.println("----ALL WORDS IN WORDS.TXT----");
for(int i=0; i<words.length; i++){
System.out.println("");
System.out.print(words[i]);
} */
}
public static String [] readWords(File f , String [] words) throws FileNotFoundException {
Scanner s;
s = new Scanner(f);
for(int i = 0; i < words.length; i++){
while (words[i] == null) {
words[i] = s.next();
}
}
s.close();
return words;
}
}
You could use a java.io.FileReader instead of a Scanner. Just google 'Java read file' to find an example for it (Using a Scanner for reading files is quite exotic). Though I don't really understand why you have a problem with importing a Scanner. Sounds like homework..
Try split method
String[] words = yourtext.split(" "); //user space in split method for words

Print output on the same line

I'm making a translator in Java to translate a fake language that I came up with for fun. I input an English word and it returns it's equivalent word in the other language. It's successfully translating everything, but each new word is on a separate line and I just want the output on one line. I'm still new to Java but here is my code:
import java.io.*;
import java.util.*;
public class Translator {
private static Scanner scan;
public static void main(String[] args) {
HashMap <String, String> XanthiumLang = new HashMap <String, String>();
XanthiumLang.put("hello", "fohran");
XanthiumLang.put("the", "krif");
XanthiumLang.put("of", "ney");
XanthiumLang.put("to", "dov");
XanthiumLang.put("and", "ahrk");
Scanner scan = new Scanner(System.in);
String sentence = scan.nextLine();
String[] result = sentence.split(" ");
for(int i = 0; i < result.length; i++){
if(XanthiumLang.containsKey(result[i])){
result[i] = XanthiumLang.get(result[i]);
}
System.out.println(result[i]);
}
}
}
I only have a few words in the code as of right now and they are stored in a hashmap. Anyways like I said the output of each word is on a separate line, not on just one line. Any ideas or changes to my code would be helpful!
Use System.out.print();. Doing so will print the entire array on one line. System.out.println(); will print the result on a new line each time (hence the ln at the end).
import java.io.*;
import java.util.*;
public class Translator {
private static Scanner scan;
public static void main(String[] args) {
HashMap <String, String> XanthiumLang = new HashMap <String, String>();
XanthiumLang.put("hello", "fohran");
XanthiumLang.put("the", "krif");
XanthiumLang.put("of", "ney");
XanthiumLang.put("to", "dov");
XanthiumLang.put("and", "ahrk");
Scanner scan = new Scanner(System.in);
String sentence = scan.nextLine();
String[] result = sentence.split(" ");
for(int i = 0; i < result.length; i++){
if(XanthiumLang.containsKey(result[i])){
result[i] = XanthiumLang.get(result[i]);
}
System.out.print(result[i]);
}
}
}
More on the different formats here.

Reading a sequence until the empty line

I am writing a Java program. I need help with the input of the program, that is a sequence of lines containing two tokens separated by one or more spaces.
import java.util.Scanner;
class ArrayCustomer {
public static void main(String[] args) {
Customer[] array = new Customer[5];
Scanner aScanner = new Scanner(System.in);
int index = readInput(aScanner, array);
}
}
It is better to use value.trim().length()
The trim() method will remove extra spaces if any.
Also String is assigned to Customer you will need to create a object out of the String of type Customer before assigning it.
Try this code... You can put the file you want to read from where "stuff.txt" currently is. This code uses the split() method from the String class to tokenize each line of text until the end of the file. In the code the split() method splits each line based on a space. This method takes a regex such as the empty space in this code to determine how to tokenize.
import java.io.*;
import java.util.ArrayList;
public class ReadFile {
static ArrayList<String> AL = new ArrayList<String>();
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("stuff.txt"));
String datLine;
while((datLine = br.readLine()) != null) {
AL.add(datLine); // add line of text to ArrayList
System.out.println(datLine); //print line
}
System.out.println("tokenizing...");
//loop through String array
for(String x: AL) {
//split each line into 2 segments based on the space between them
String[] tokens = x.split(" ");
//loop through the tokens array
for(int j=0; j<tokens.length; j++) {
//only print if j is a multiple of two and j+1 is not greater or equal to the length of the tokens array to preven ArrayIndexOutOfBoundsException
if ( j % 2 ==0 && (j+1) < tokens.length) {
System.out.println(tokens[j] + " " + tokens[j+1]);
}
}
}
} catch(IOException ioe) {
System.out.println("this was thrown: " + ioe);
}
}
}

Categories