NoSuchElementExcpetion when using Java scanner to compare two files - java

I am trying to see the possibilities of automating localization testing of web application(L10N). Firstly, we decided to see if without opening an application, if we can gather HTML plain text(native language)in a file & compare this with the glossary we have.
I am able to get plain text using jsoup. Now I am trying to compare these two files with below code:
import java.io.*;
import java.util.*;
class CompareFiles{
public static void main(String args[]) throws Exception{
Scanner kb = new Scanner(System.in);
String name;
String curr;
java.io.File dictionary = new java.io.File("./src/main/resources/Google_JP.txt");
Scanner dictScanner = new Scanner(dictionary);
java.io.File list = new java.io.File("./src/main/resources/Google_JP_HTML.txt");
Scanner listScanner = new Scanner(list);
try
{
while(dictScanner.hasNextLine()){
System.out.println("inside dictonary scanner");
curr=dictScanner.next();
while(listScanner.hasNextLine()){
System.out.println("inside list scanner");
name=listScanner.next();
if(curr.contains(name)) System.out.println(name);
}
}
}
catch(NoSuchElementException e)
{
e.printStackTrace();
}
}
}
Now problem with above code is, since I was getting NoSuchElementException on name=listScanner.next(); I tried to handle exception and close the scanner. With this, it is only comparing the first word of html file. How I should make it work, so that it will display all matching words?
Also I am not sure if I am following the right approach to meet the requirement.

looks to me as if you would need to reinitialize the scanner on every loop iteration as you exhaust it on the first iteration
what happens seems to be:
curr is the first line from dictScanner
loop over nameScanner
curr compared with all names
name scanner is now empty
curr is set to second line
loop over nameScanner which is already exhausted
proposed change:
import java.io.*;
import java.util.*;
class CompareFiles{
public static void main(String args[]) throws Exception{
Scanner kb = new Scanner(System.in);
String name;
String curr;
java.io.File dictionary = new java.io.File("./src/main/resources/Google_JP.txt");
Scanner dictScanner = new Scanner(dictionary);
java.io.File list = new java.io.File("./src/main/resources/Google_JP_HTML.txt");
//Scanner listScanner = new Scanner(list);
try
{
while(dictScanner.hasNextLine()){
System.out.println("inside dictonary scanner");
curr=dictScanner.next();
try (Scanner listScanner = new Scanner(list);){
while(listScanner.hasNextLine()){
System.out.println("inside list scanner");
name=listScanner.next();
if(curr.contains(name)) System.out.println(name);
}
}
}
} catch(NoSuchElementException e) {
e.printStackTrace();
}
}
}

Related

How to correctly identify words when reading from a file with java Scanner?

I'm trying to do an exercise where I need to create a class to read the words from a .txt put the words in an HashSet. The thing is, if the text read "I am Daniel, Daniel I am." I'll have a word for "am" , "am." and "Daniel," and "Daniel". How do I fix this?
Here's my code. (I tried to use regex, but I'm getting an exception):
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
public class WordCount {
public static void main(String[] args) {
try {
File file = new File(args[0]);
HashSet<String> set = readFromFile(file);
set.forEach(word -> System.out.println(word));
}
catch(FileNotFoundException e) {
System.err.println("File Not Found!");
}
}
private static HashSet<String> readFromFile(File file) throws FileNotFoundException {
HashSet<String> set = new HashSet<String>();
Scanner scanner = new Scanner(file);
while(scanner.hasNext()) {
String s = scanner.next("[a-zA-Z]");
set.add(s.toUpperCase());
}
scanner.close();
return set;
}
}
Error is thrown when the Scanner try to read a string not matching with the regex.
String s = scanner.next("[a-zA-Z]");
Instead of passing the regex in the Scanner. Read the word and remove the special characters as shown below.
String s = scanner.next();
s = s.replaceAll("[^a-zA-Z]", "");

How to import CSV data into an array as an object?

I currently need to import data from a CSV file into an array as an object. The data is not all of the same type however and one line of the data is formatted like this "Tom, Jones, 95846, 657.45". I am able to parse the file but cannot seem to figure out how to store this data into an array. I will need to sort this data later on based on different requirements like Name and Number.
import java.io.*;
import java.util.*;
public class People {
public static void main(String[] args) {
File file = new File("People.csv");
People peopleArr[] = new People[100];
try{
Scanner inputFile = new Scanner(file);
inputFile.useDelimiter(",");
while(inputFile.hasNext()){
// Store the data into array
}
inputFile.close();
}catch (FileNotFoundException e){
System.out.println("Check file");
}
}
}
Maybee smth like this:
public static People[] readPeople(File file) {
List<People> people = new ArrayList<>(100);
try (Scanner inputFile = new Scanner(file)) {
inputFile.useDelimiter(",");
while (inputFile.hasNext()) {
People obj = new People();
// e.g. line is equals to John,Done
// obj.setFirstName(inputFile.next());
// obj.setLastName(inputFile.next());
people.add(obj);
}
} catch(FileNotFoundException e) {
System.out.println("Check file");
}
return people.toArray(new People[people.size()]);
}

Write to files using Java

I am trying to use lists for my first time, I have a txt file that I am searching in it about string then I must write the result of searching in new file.
Check the image attached
My task is to retrieve the two checked lines of the input file to the output files.
And this is my code:
import java.io.*;
import java.util.Scanner;
public class TestingReport1 {
public static void main(String[] args) throws Exception {
File test = new File("E:\\test2.txt");
File Result = new File("E:\\Result.txt");
Scanner scanner = new Scanner(test);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if(line.contains("Visit Count")|| line.contains("Title")) {
System.out.println(line);
}
}
}
}
What should I do?!
Edit: How can I write the result of this code into text file?
Edit2:
Now using the following code:
public static void main(String[] args) throws Exception {
// TODO code application logic here
File test = new File("E:\\test2.txt");
FileOutputStream Result = new FileOutputStream("E:\\Result.txt");
Scanner scanner = new Scanner(test);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if(line.contains("Visit Count")|| line.contains("Title")) {
System.out.println(line);
Files.write(Paths.get("E:\\Result.txt"), line.getBytes(), StandardOpenOption.APPEND);
}
}
}
I got the result back as Visit Count:1 , and I want to get this number back as integer, Is it possible?
Have a look at Files, especially readAllLines as well as write. Filter the input between those two method calls, that's it:
// Read.
List<String> input = Files.readAllLines(Paths.get("E:\\test2.txt"));
// Filter.
String output = input.stream()
.filter(line -> line.matches("^(Title.*|Visit Count.*)"))
.collect(Collectors.joining("\n"));
// Write.
Files.write(Paths.get("E:\\Result.txt"), output.getBytes());

Java - Using multiple PrintWriter but saves only last println

I'm designing a program to split data stored in a text file into two separate files based on the label of that data.
Here is a small version of that data.
0,1,2,normal.
5,5,5,strange.
2,1,3,normal.
I use a class to store each line as a sample. The class parses the line to store the last value as the label. I encapsulated each line as an object, because I intend to add features later.
Here is code for the Sample class
import java.util.Scanner;
public class Sample {
String[]str_vals = new String[3];
String label;
Sample(Scanner line) {
for (int i=0; i<3; i++) {
str_vals[i] = line.next();
}
label = line.next();
}
String getValsForCSV() {
StringBuilder retval = new StringBuilder();
for (int i=0; i<3; i++) {
retval.append(str_vals[i]).append(",");
}
retval.append(label).append(".");
return retval+"";
}
String getLabel() {
return label;
}
}
Below is the code in question. My Separator class.
import java.io.*;
import java.util.Scanner;
public class Separator {
public static final String DATAFILE = "src/etc/test.txt";
public static void main(String[] args) throws FileNotFoundException {
runData();
}
public static void runData() throws FileNotFoundException {
try (Scanner in = new Scanner(new File(DATAFILE))) {
// kddcup file uses '.\n' at end of each line
// setting this as delimiter which will consume the period
in.useDelimiter("[.]\r\n|[.]\n|\n");
Sample curr;
while(in.hasNext()) {
// line will hold all fields for a single sample
Scanner line = new Scanner(in.next());
line.useDelimiter(", *");
curr = new Sample(line);
try (
PrintWriter positive = new PrintWriter(new File(DATAFILE+"-pos"));
PrintWriter negative = new PrintWriter(new File(DATAFILE+"-neg"));
) {
if (curr.getLabel().equals("normal")) {
positive.println("GOOD");
} else {
negative.println("BAD");
}
}
}
}
}
}
This issue that I am experiencing is that the code only saves the last Sample seen to its respective file. So with above data the test.txt-neg will be empty and test.txt-pos will have a single line GOOD; it does not have two GOOD's as expected.
If I modify the test.txt data to include only the first two lines, then the files states are reversed (i.e. test.txt-neg has BAD and test.txt-pos is empty). Could someone please explain to me what is going on, and how to fix this error?
Because the error was pointed out in a comment. I wanted to give credit to KevinO and Elliott Frisch for the solution.
As mentioned, I'm creating a new PrintWriter each time and creating the PrintWriter in it's default mode of overwriting a file. As a result it always saves both files based on a single sample.
To correct this error, I have pulled out the instantiations of the PrintWriter to be in the try-with-resource block of the Scanner object
import java.io.*;
import java.util.Scanner;
public class Separator {
public static final String DATAFILE = "src/etc/test.txt";
public static void main(String[] args) throws FileNotFoundException {
runData();
}
public static void runData() throws FileNotFoundException {
try (
Scanner in = new Scanner(new File(DATAFILE));
PrintWriter positive = new PrintWriter(new File(DATAFILE+"-pos"));
PrintWriter negative = new PrintWriter(new File(DATAFILE+"-neg"));
) {
// kddcup file uses '.\n' at end of each line
// setting this as delimiter which will consume the period
in.useDelimiter("[.]\r\n|[.]\n|\n");
Sample curr;
while(in.hasNext()) {
// line will hold all fields for a single sample
Scanner line = new Scanner(in.next());
line.useDelimiter(", *");
curr = new Sample(line);
if (curr.getLabel().equals("normal")) {
positive.println("GOOD");
} else {
negative.println("BAD");
}
}
}
}
}

JAVA String Reversing order of string in file io

I have to write code that will reverse the order of the string and write it in a new file. For example :
Hi my name is Bob.
I am ten years old.
The reversed will be :
I am ten years old.
Hi my name is Bob.
This is what I have so far. Not sure what to write for the outWriter print statement. Any help will be appreciated. Thanks!
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class FileRewinder {
public static void main(String[] args) {
File inputFile = new File("ascii.txt");
ArrayList<String> list1 = new ArrayList<String>();
Scanner inputScanner;
try {
inputScanner = new Scanner(inputFile);
} catch (FileNotFoundException f) {
System.out.println("File not found :" + f);
return;
}
while (inputScanner.hasNextLine()) {
String curLine = inputScanner .nextLine();
System.out.println(curLine );
}
inputScanner.close();
File outputFile = new File("hi.txt");
PrintWriter outWriter = null;
try {
outWriter = new PrintWriter(outputFile);
} catch (FileNotFoundException e) {
System.out.println("File not found :" + e);
return;
}
outWriter.println(???);
outWriter.close();
}
}
My suggestion is read entire file first and store sentences(you can split by .) in a LinkedList<String>(this will keep insertion order)
Then use Iterator and get sentences in reverse order. and write them into a file. make sure to put . just after each sentence.
After System.out.println(curLine ); add list1.add(curline); that will place your lines of text into your list.
At the end create a loop over list1 backwards:
for(int i = list1.size() - 1 , i > 0, --i) {
outWriter.println(list1[i]);
}
If the file contains an amount of lines which can be loaded into the memory. You can read all lines into a list, reverse the order of the list and write the list back to the disk.
public class Reverse {
static final Charset FILE_ENCODING = StandardCharsets.UTF_8;
public static void main(String[] args) throws IOException {
List<String> inLines = Files.readAllLines(Paths.get("ascii.txt"), FILE_ENCODING);
Collections.reverse(inLines);
Files.write(Paths.get("hi.txt"), inLines, FILE_ENCODING);
}
}

Categories