Double string output from while loop - java

I am trying to make simple one-Class code to copy some definite files (photos) from one folder to another according to a list. The list is kept in separate txt file.
Finally I've got file not found error, so I divided entire code into parts and tested each of them with console output.
And that what I found in scanner while loop:
So the code:
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class Sandy2 {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
File f = new File("D:\\\\Javasorter\\List.txt");
Scanner in;
in = new Scanner(f).useDelimiter("[?! .,;:\t\r\n\f\'\"]");
String word, pathbuf1, pathbuf2;
while (in.hasNext()) {
word = in.next().toLowerCase();
System.out.println(word);
Thread.sleep(1000);
pathbuf1 = "\\IMG_" + word + ".CR2";
pathbuf2 = "\\IMG_" + word + ".CR2";
System.out.println(pathbuf1);
System.out.println(pathbuf2);
}
}
}
Expected output:
9452
\IMG_9452.CR2
\IMG_9452.CR2
9475
\IMG_9475.CR2
\IMG_9475.CR2
and so on until list has next, but
Output:
9452
\IMG_9452.CR2
\IMG_9452.CR2
\IMG_.CR2
\IMG_.CR2
9475 \IMG_9475.CR2
\IMG_9475.CR2
\IMG_.CR2
\IMG_.CR2
and so on///
Copying command is added in the same loop in full version program, where "\IMG_9452.CR2" is attached to path srting
So, after the first file copied I got error, because of course there is no "IMG_.CR2" file.
Does anybody know why \IMG_.CR2 doubles in each loop iteration?
Many thanks!

Your problem is in the .useDelimiter("[?! .,;:\t\r\n\f\'\"]"), because Scanner will read spaces between lines as string object, so you have to remove it or change it if you still need to use Delimiter.
Also, You can write the file path with forward slashes / then it will be more readable and OS independent, and try to use try-with-resource to prevent source leak.
Consider this code:
public static void main(String[] args) throws Exception {
try (Scanner in = new Scanner(new File("D:/Javasorter/List.txt"))) {
// in.useDelimiter("[?! .,;:\t\r\n\f\'\"]");
String word, pathbuf1, pathbuf2;
while (in.hasNext()) {
word = in.next().toLowerCase();
System.out.println(word);
Thread.sleep(1000);
pathbuf1 = "\\IMG_" + word + ".CR2";
pathbuf2 = "\\IMG_" + word + ".CR2";
System.out.println(pathbuf1);
System.out.println(pathbuf2);
}
}
}
If you want to use Delimiter for some cases, you can use string trim and then check if string is empty, like:
public static void main(String[] args) throws Exception {
try (Scanner in = new Scanner(new File("D:/Javasorter/List.txt"))) {
in.useDelimiter("[?! .,;:\t\r\n\f\'\"]");
String word, pathbuf1, pathbuf2;
while (in.hasNext()) {
word = in.next().trim().toLowerCase();
if (!word.isEmpty()) {
System.out.println(word);
Thread.sleep(1000);
pathbuf1 = "\\IMG_" + word + ".CR2";
pathbuf2 = "\\IMG_" + word + ".CR2";
System.out.println(pathbuf1);
System.out.println(pathbuf2);
}
}
}
}

remove the variable pathbuf2 completely and you will not have any more of the duplicates

Related

JAVA - Parsing CSV File - Change delimiter

I have a problem and wanted to ask if someone can help me. I have a Java application that processes CSV files. The files have a semi-colon as a "delimiter". Now instead of semicolons I would like to use pipe "|" as the "delimiter". What is the best way to do this?
I have already informed myself in the library or class "org.apache.commons.csv.CSVRecord". Unfortunately couldn't find anything here.
I used for parsing Spring Batch with the class FlatItemReaderBuilder.
You can also use Spring Batch classes in non Spring application.
Here you can find an example:
https://www.petrikainulainen.net/programming/spring-framework/spring-batch-tutorial-reading-information-from-a-file/
you could use Scanner or FileInputStream
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class ReadDelimited {
public static void main(String[] args) {
Scanner sc = null;
try {
sc = new Scanner(new File("D:\\acct.csv"));
// Check if there is another line of input
while(sc.hasNextLine()){
String str = sc.nextLine();
// parse each line using delimiter
parseData(str);
}
} catch (IOException exp) {
// TODO Auto-generated catch block
exp.printStackTrace();
}finally{
if(sc != null)
sc.close();
}
}
private static void parseData(String str){
String acctFrom, acctTo, amount;
Scanner lineScanner = new Scanner(str);
lineScanner.useDelimiter("|");
while(lineScanner.hasNext()){
acctFrom = lineScanner.next();
acctTo = lineScanner.next();
amount = lineScanner.next();
System.out.println("Account From- " + acctFrom + " Account To- " + acctTo +
" Amount- " + amount);
}
lineScanner.close();
}
}
reference Code original link
or if File is not too large get the read the File in a string Divide it into a String array by splitting by line break and then future splitting using the delimiter of choice something like the code below.
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream=new FileInputStream("j:\\test.csv");
String s1=new String(fileInputStream.readAllBytes());
String[] lineArray=s1.split("\n");
List<String[]> separatedValues=new ArrayList<>();
for (String line: lineArray) {
separatedValues.add(line.split("\\|"));
}
for (String[] s: separatedValues) {
for (String s2:s ) {
System.out.print(s2+" ");
}
System.out.println("");
}
fileInputStream.close();
}
Code Output Link
Original CSV

Count the number of times a set of characters appear in a file without BufferedReader

I am trying to count the number of times a string appears in a file. I want to find the number of times that "A, E, I, O, U" appears exactly in that order. Here is the text file:
AEIOU aeiou baeiboeu bbbaaaaaa beaeiou caeuoi ajejijoju aeioo
aeiOu ma me mi mo mu
take it OUT!
I want the method to then return how many times it is in the file. Any idea's on how I could go about doing this? The catch is I want to do this without using BufferedReader. I can simply just read the file using Scanner. Is there a way to do this?
I edited this and added the code I have so far. I don't think I am even close. I am pretty sure I need to use some nested loops to make this happen.
import java.util.*;
import java.io.*;
public class AEIOUCounter
{
public static final String DELIM = "\t";
public static void main(String[] args)
{
File filename = new File("aeiou.txt");
try
{
Scanner fileScanner = new Scanner(new File(filename));
while(fileScanner.hasNextLine())
{
System.out.println(fileScanner.nextLine());
}
}
catch(IOException e)
{
System.out.println(e);
}
catch(Exception e)
{
System.out.println(e);
}
fileScanner.close();
}
}
What you are doing now, is printing all the lines in the file.
fileScanner.hasNextLine()
fileScanner.nextLine()
But what you are looking for is filtering out separate words in the file:
Path path = Paths.get("/path/to/file");
Scanner sc = new Scanner(path);
int counter = 0;
while (sc.hasNext()) {
String word = sc.next();
if (word.equalsIgnoreCase("AEIOU")) {
counter += 1;
}
}
System.out.print("Number of words: " + counter);
Smurf's answer is great. It's worth mentioning that if you're using Java 8, you can avoid using a Scanner at all, and do this in a single expression:
long count = Files.lines(Paths.get("aeiou.txt"))
.flatMap(s -> Arrays.stream(s.split(" ")))
.filter(s -> s.equalsIgnoreCase("aeiou"))
.count();

Using a scanner to extract data with delimiters by line

I am extracting data from a file. I am having trouble with using the delimiters while reading through the file.
My file is ordered like so:
0 Name 0
1 Name1 1
The structure is an integer, a tab (\t), a string, a tab (\t), another integer, and then a newline (\n).
I have tried to use a compound delimiter as referenced in this question:
Java - Using multiple delimiters in a scanner
However, I am still getting an InputMismatch Exception when I run the following code:
while(readStations.hasNextLine()) {
327 tempSID = readStations.nextInt();
328 tempName = readStations.next();
329 tempLine = readStations.nextInt();
//More code here
}
It calls this error on line two of the above code...
I am not sure why, and help would be appreciated, Thanks.
The current output runs as such for the code:
Exception in thread "main" java.util.InputMismatchException
...stuff...
at Metro.declarations(Metro.java:329)
Newline is most likely causing you issues. Try this
public class TestScanner {
public static void main(String[] args) throws IOException {
try {
Scanner scanner = new Scanner(new File("data.txt"));
scanner.useDelimiter(System.getProperty("line.separator"));
while (scanner.hasNext()) {
String[] tokens = scanner.next().split("\t");
for(String token : tokens) {
System.out.print("[" + token + "]");
}
System.out.print("\n");
}
scanner.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
i think when the scanner separates input like this you can only use input.next() not next int: or keep the same type.

Identifying each word in a file

Importing a large list of words and I need to create code that will recognize each word in the file. I am using a delimiter to recognize the separation from each word but I am receiving a suppressed error stating that the value of linenumber and delimiter are not used. What do I need to do to get the program to read this file and to separate each word within that file?
public class ASCIIPrime {
public final static String LOC = "C:\\english1.txt";
#SuppressWarnings("null")
public static void main(String[] args) throws IOException {
//import list of words
#SuppressWarnings("resource")
BufferedReader File = new BufferedReader(new FileReader(LOC));
//Create a temporary ArrayList to store data
ArrayList<String> temp = new ArrayList<String>();
//Find number of lines in txt file
String line;
while ((line = File.readLine()) != null)
{
temp.add(line);
}
//Identify each word in file
int lineNumber = 0;
lineNumber++;
String delimiter = "\t";
//assess each character in the word to determine the ascii value
int total = 0;
for (int i=0; i < ((String) line).length(); i++)
{
char c = ((String) line).charAt(i);
total += c;
}
System.out.println ("The total value of " + line + " is " + total);
}
}
This smells like homework, but alright.
Importing a large list of words and I need to create code that will recognize each word in the file. What do I need to do to get the program to read this file and to separate each word within that file?
You need to...
Read the file
Separate the words from what you've read in
... I don't know what you want to do with them after that. I'll just dump them into a big list.
The contents of my main method would be...
BufferedReader File = new BufferedReader(new FileReader(LOC));//LOC is defined as class variable
//Create an ArrayList to store the words
List<String> words = new ArrayList<String>();
String line;
String delimiter = "\t";
while ((line = File.readLine()) != null)//read the file
{
String[] wordsInLine = line.split(delimiter);//separate the words
//delimiter could be a regex here, gotta watch out for that
for(int i=0, isize = wordsInLine.length(); i < isize; i++){
words.add(wordsInLine[i]);//put them in a list
}
}
You can use the split method of the String class
String[] split(String regex)
This will return an array of strings that you can handle directly of transform in to any other collection you might need.
I suggest also to remove the suppresswarning unless you are sure what you are doing. In most cases is better to remove the cause of the warning than supress the warning.
I used this great tutorial from thenewboston when I started off reading files: https://www.youtube.com/watch?v=3RNYUKxAgmw
This video seems perfect for you. It covers how to save file words of data. And just add the string data to the ArrayList. Here's what your code should look like:
import java.io.*;
import java.util.*;
public class ReadFile {
static Scanner x;
static ArrayList<String> temp = new ArrayList<String>();
public static void main(String args[]){
openFile();
readFile();
closeFile();
}
public static void openFile(){
try(
x = new Scanner(new File("yourtextfile.txt");
}catch(Exception e){
System.out.println(e);
}
}
public static void readFile(){
while(x.hasNext()){
temp.add(x.next());
}
}
public void closeFile(){
x.close();
}
}
One thing that is nice with using the java util scanner is that is automatically skips the spaces between words making it easy to use and identify words.

reading from text file to string array

So I can search for a string in my text file, however, I wanted to sort data within this ArrayList and implement an algorithm. Is it possible to read from a text file and the values [Strings] within the text file be stored in a String[] Array.
Also is it possible to separate the Strings? So instead of my Array having:
[Alice was beginning to get very tired of sitting by her sister on the, bank, and of having nothing to do:]
is it possible to an array as:
["Alice", "was" "beginning" "to" "get"...]
.
public static void main(String[]args) throws IOException
{
Scanner scan = new Scanner(System.in);
String stringSearch = scan.nextLine();
BufferedReader reader = new BufferedReader(new FileReader("File1.txt"));
List<String> words = new ArrayList<String>();
String line;
while ((line = reader.readLine()) != null) {
words.add(line);
}
for(String sLine : words)
{
if (sLine.contains(stringSearch))
{
int index = words.indexOf(sLine);
System.out.println("Got a match at line " + index);
}
}
//Collections.sort(words);
//for (String str: words)
// System.out.println(str);
int size = words.size();
System.out.println("There are " + size + " Lines of text in this text file.");
reader.close();
System.out.println(words);
}
To split a line into an array of words, use this:
String words = sentence.split("[^\\w']+");
The regex [^\w'] means "not a word char or an apostrophe"
This will capture words with embedded apostrophes like "can't" and skip over all punctuation.
Edit:
A comment has raised the edge case of parsing a quoted word such as 'this' as this.
Here's the solution for that - you have to first remove wrapping quotes:
String[] words = input.replaceAll("(^|\\s)'([\\w']+)'(\\s|$)", "$1$2$3").split("[^\\w']+");
Here's some test code with edge and corner cases:
public static void main(String[] args) throws Exception {
String input = "'I', ie \"me\", can't extract 'can't' or 'can't'";
String[] words = input.replaceAll("(^|[^\\w'])'([\\w']+)'([^\\w']|$)", "$1$2$3").split("[^\\w']+");
System.out.println(Arrays.toString(words));
}
Output:
[I, ie, me, can't, extract, can't, or, can't]
Also is it possible to separate the Strings?
Yes, You can split string by using this for white spaces.
String[] strSplit;
String str = "This is test for split";
strSplit = str.split("[\\s,;!?\"]+");
See String API
Moreover you can also read a text file word by word.
Scanner scan = null;
try {
scan = new Scanner(new BufferedReader(new FileReader("Your File Path")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while(scan.hasNext()){
System.out.println( scan.next() );
}
See Scanner API

Categories