List<String> with entities to encode to UTF-8 - java

I have list which get regex value and add to List
private static List<String> listaOfQuestion(Scanner sc, List<File> listaQuestion) {
List<String> question = new ArrayList<String>();
for (File input1 : listaQuestion) {
try {
sc = new Scanner(input1);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (sc.hasNextLine()) {
Scanner s = new Scanner(sc.nextLine());
while (s.hasNext()) {
String words = s.nextLine();
try {
question.add(getTagValuesQ(words).toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return question;
}
I want to parse all value like
List
Bielańska
Wyziński
Wciślik
To
List
Bielańska
Wyzińska
Wciślik
To UTF-8, i'm searching throught the forum, and i didn't see solution or i just dont get it.
I appreciate every form of help, but because i'm new the best will be standard example or something like this which i will be able to understand.
I solved my problem, i needed use
<...>
Scanner s = new Scanner(sc.nextLine());
while(s.hasNext()){
String words = s.nextLine();
String decoded = org.apache.commons.lang3.StringEscapeUtils.unescapeHtml4(words);
<...>

I tried using Apache Common Lang and solved it:
String s = "Bielańska Wyziński Wciślik";
String decoded = org.apache.commons.lang3.StringEscapeUtils.unescapeHtml4(s);
System.out.println(decoded);
Output:
Bielańska Wyziński Wciślik
https://commons.apache.org/proper/commons-lang/download_lang.cgi

Related

How to parse text file

demo.txt :
FD1,true,102400,4000,0.01,103,83.25
FD0,false,102400,4000,0.01,103,83.25
I want to access each line 1st then from each line i want to access each element and pass this as parameter to a function createFogDevice to perform some action.
like createFogDevice(FD1,true,1024,4000,0.01,103,83.25)
Can anybody help how we can write code for this ?
Scanner scanner = new Scanner(new File());
while(scanner.hasNextLine()) {
String currentLine = scanner.nextLine();
String[] dataPoints = currentLine.split(",");
String a = dataPoints[0];
boolean b = Boolean.parseBoolean(dataPoints[1]);
// ....
createFogDevice(a,b,c/*...*/);
}
Try This :-
try (Stream<String> stream = Files.lines(Paths.get("demo.txt"))) {
stream.forEach(ob->createFogDevice(ob.split(",")));
} catch (IOException e) {
e.printStackTrace();
}
Also you can modify createFogDevice() method to pass Array of String as argument :-
private static void createFogDevice(String[] inputParams) {
// your code goes here
}

Making a List in Java from a text file

In Java I'm using the Scanner to read from a text file,
for example (cat, dog, mouse).
When I use the System.out.println() the output appears like cat, dog, mouse
I want the list to look like this
cat
dog
mouse
any help code below
Scanner scan = null;
Scanner scan2 = null;
boolean same = true;
try {
scan = new Scanner(new
File("//home//mearts//keywords.txt"));
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
List<String> firstLines = new ArrayList<String>();
while (scan.hasNextLine()) {
firstLines.add(scan.nextLine());
System.out.println(firstLines);
}
You are reading the file line by line, instead of taking the delimiters into consideration:
try (Scanner scan =
new Scanner("//home//mearts//keywords.txt").useDelimiter(", ")) {
while (scan.hasNext()) {
System.out.println(scan.next());
}
} catch (FileNotFoundException e) {
e.printStackTrace(); // Or something more useful
}
Try something like:
firstLines.forEach(System.out::println);
By the way, as you are only reading lines, you may also want to have a look at java.nio.file.Files:
Path keywordsFilepath = Paths.get(/* your path */...);
Files.lines(keywordsFilepath)
.forEach(System.out::println);

Reading a file into an array Java

So I have a project in which I have to read book reference numbers and book titles from a .txt file into an array, and then a user is to enter a reference number that will do a search for the book with that reference number, so here is what i have, Keep in mind I'm not very experienced with java
public class Book {
ArrayList<String> books = new ArrayList<String>();
BufferedReader br = null;
{
try {
br = new BufferedReader(new FileReader("BookList.txt"));
String book;
while ((book = br.readLine()) != null) {
books.add(book);
}
} catch (IOException e){
} finally {
try {
br.close();
} catch (IOException ex) {
}
}
String [] bookList = new String[books.size()];
books.toArray(bookList);
}
}
That is to read the file into an array list, and then convert the array list into an array
Im not 100% sure if that's right so if theres a problem, I would gladly take your solution.
The problem i'm having is when i try to set up a method that allows a user to search
private void FindItActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input = Input.getText();
for(int i=0; i<bookList.length; i++){
}
}
I get an error that says cannot find symbol bookList, but im not sure what why
Thanks for any help or advice you may be able to offer
your init code is inside a scoped brackets, you missed a method declaration by the way.
you cant reach bookList as its not a class parameter but declared in the scope.
put a method declaration (above the try) and add bookList declaration under your BufferedReader variable instead of declaring it in the scope.
You need to have bookList available to all of your methods
public class Book {
ArrayList<String> books = new ArrayList<String>();
String[] bookList;
BufferedReader br = null;
// ...
Then you need to set it to something. Your current line books.toArray(bookList); uses bookList as the argument for toArray to know what kind of array it is producing, then it will return an array of that type. So you need to do
this.bookList = books.toArray(bookList);
Your code should look like this:
public class Book {
ArrayList<String> books = new ArrayList<String>();
BufferedReader br = null;
String[] bookList; //difference (bookList is now visible to all methods in class)
{
try {
br = new BufferedReader(new FileReader("BookList.txt"));
String book;
while ((book = br.readLine()) != null) {
books.add(book);
}
} catch (IOException e){
} finally {
try {
br.close();
} catch (IOException ex) {
}
}
bookList = new String[books.size()]; //difference
books.toArray(bookList);
}
}
private void FindItActionPerformed(java.awt.event.ActionEvent evt){
String input;
input = Input.getText();
for(int i=0; i<bookList.length; i++){
//do something...
}
}
Problem in your code is that you have tried to use variable bookList which was in different scope defined.

Collections sort not working properly

I am new to Java. I am trying to added few words from a text file to my existing text based word list. I have the below code doing
Add words from an file to existing list
Sort the list of words
Save the words to a text file
"wordList" is an arraylist with existing words.
private void updateDictionaryFile(String filepath) {
String textCurrentLine = "";
BufferedReader dictionaryFile = null;
try {
Scanner fileScanner = new Scanner(new File(filepath));
while(fileScanner.hasNextLine()){
System.out.println("fileScanner.hasNextLine() "+ fileScanner.hasNextLine());
textCurrentLine = fileScanner.nextLine();
if(textCurrentLine.length() > 0)
if (!wordList.contains(textCurrentLine)) {
wordList.add(textCurrentLine);
}
}
Collections.sort(wordList);
String newFile = filepath.replace(".txt", "_new.txt");
PrintWriter pw = new PrintWriter(new FileOutputStream(newFile));
for (int i = 0; i < wordList.size(); i++) {
pw.println(wordList.get(i).toString());
}
pw.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (dictionaryFile != null) {
dictionaryFile.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Word listed in new file is not sorted. Am I missing something in between?
Below is the output
A
Achieve
Although
Anything
Ask
Avoid
Badly
Bluma
But
Find
Forget
Goal
Goals
How
In
It
Just
Keep
Know
NOT
Often
Once
One
Psychologists
Reasoning
Reject
Remember
Research
Russian
Shifting
Sidestep
So
Sometimes
Start
Stop
The
This
Those
Under
Visualise
Visualising
We
What
When
With
You
Zeigarnik
a
aa
aah
aahed
aahing
aahs
aal
aalii
aaliis
aals
aardvark
aardwolf
aargh
aarrgh
aarrghh
aas
Collections.sort(wordList); will work perfectly. if need to ignore the case then use below code.
Collections.sort(wordList,String.CASE_INSENSITIVE_ORDER);

Cant match Srt subtitle using Regex in Java

In try in this code to parse an srt subtitle:
public class MatchArray {
public static void main(String args[]) {
File file = new File(
"C:/Users/Thiago/workspace/SubRegex/src/Dirty Harry VOST - Clint Eastwood.srt");
{
try {
Scanner in = new Scanner(file);
try {
String contents = in.nextLine();
while (in.hasNextLine()) {
contents = contents + "\n" + in.nextLine();
}
String pattern = "([\\d]+)\r([\\d]{2}:[\\d]{2}:[\\d]{2}),([\\d]{3})[\\s]*-->[\\s]*([\\d]{2}:[\\d]{2}:[\\d]{2}),([\\d]{3})\r(([^|\r]+(\r|$))+)";
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(contents);
ArrayList<String> start = new ArrayList<String>();
while (m.find()) {
start.add(m.group(1));
start.add(m.group(2));
start.add(m.group(3));
start.add(m.group(4));
start.add(m.group(5));
start.add(m.group(6));
start.add(m.group(7));
System.out.println(start);
}
}
finally {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
But when i execute it, it dosent capture any group, when try to capture only the time with this pattern:
([\\d]{2}:[\\d]{2}:[\\d]{2}),([\\d]{3})[\\s]*-->[\\s]*([\\d]{2}:[\\d]{2}:[\\d]{2}),([\\d]{3})
It works. So how do I make it capture the entire subtitle?
I can not quite understand your need but i thought this can help.
Please try the regex:
(\\d+?)\\s*(\\d+?:\\d+?:\\d+?,\\d+?)\\s+-->\\s+(\\d+?:\\d+?:\\d+?,\\d+?)\\s+(.+)
I tried it on http://www.myregextester.com/index.php and it worked.
I hope this can help.

Categories

Resources