Get length of "EMPTY SPACE" in the string - java

I have to read a flat file which is not properly structured and I need to read it by the size of the indent in a line.
Element TestData*
Content Particle Particle_3*
Element TestData1*
Content Particle Particle_62*
Above is my structure of the flat file. I need to read the empty leading spaces before the text.
The expected result to be:
Length of Empty space of 1st line = 2
Length of Empty space of 2nd line = 5
Length of Empty space of 3rd line = 8
Length of Empty space of 4th line = 11
Any help would be great...!!!
Thanks.

Something like this might work:
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args){
try (BufferedReader reader = Files.newBufferedReader(Paths.get("./testfile.txt"), StandardCharsets.UTF_8)){
int lineNr = 0;
String line;
while((line = reader.readLine()) != null){
lineNr++;
int spaces = 0;
for (int i=0;i<line.length();i++){
if (line.charAt(i) == ' '){
spaces++;
}
else{
break;
}
}
System.out.println("line "+lineNr+" has "+spaces+" leading spaces:"+line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
line 1 has 2 leading spaces: Element TestData*
line 2 has 5 leading spaces: Content Particle Particle_3*
line 3 has 8 leading spaces: Element TestData1*
line 4 has 11 leading spaces: Content Particle Particle_62*

Related

Find Integers and float numbers in txt file

I have a problem with a simple code nd don't know how to do it;
I have 3 txt files.
First txt file looks like this:
1 2 3 4 5 4.5 4,6 6.8 8,9
1 3 4 5 8 9,2 6,3 6,7 8.9
I would like to read numbers from this txt file and save integers to one txt file and floats to another.
You can do it with the following easy steps:
When you read a line, split it on whitespace and get an array of tokens.
While processing each token,
Trim any leading and trailing whitespace and then replace , with .
First check if the token can be parsed into an int. If yes, write it into outInt (the writer for integers). Otherwise, check if the token can be parsed into float. If yes, write it into outFloat (the writer for floats). Otherwise, ignore it.
Demo:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
BufferedReader in = new BufferedReader(new FileReader("t.txt"));
BufferedWriter outInt = new BufferedWriter(new FileWriter("t2.txt"));
BufferedWriter outFloat = new BufferedWriter(new FileWriter("t3.txt"));
String line = "";
while ((line = in.readLine()) != null) {// Read until EOF is reached
// Split the line on whitespace and get an array of tokens
String[] tokens = line.split("\\s+");
// Process each token
for (String s : tokens) {
// Trim any leading and trailing whitespace and then replace , with .
s = s.trim().replace(',', '.');
// First check if the token can be parsed into an int
try {
Integer.parseInt(s);
// If yes, write it into outInt
outInt.write(s + " ");
} catch (NumberFormatException e) {
// Otherwise, check if token can be parsed into float
try {
Float.parseFloat(s);
// If yes, write it into outFloat
outFloat.write(s + " ");
} catch (NumberFormatException ex) {
// Otherwise, ignore it
}
}
}
}
in.close();
outInt.close();
outFloat.close();
}
}
Assuming that , is also a decimal separator . it may be possible to unify this characters (replace , with .).
static void readAndWriteNumbers(String inputFile, String intNums, String dblNums) throws IOException {
// Use StringBuilder to collect the int and double numbers separately
StringBuilder ints = new StringBuilder();
StringBuilder dbls = new StringBuilder();
Files.lines(Paths.get(inputFile)) // stream of string
.map(str -> str.replace(',', '.')) // unify decimal separators
.map(str -> {
Arrays.stream(str.split("\\s+")).forEach(v -> { // split each line into tokens
if (v.contains(".")) {
if (dbls.length() > 0 && !dbls.toString().endsWith(System.lineSeparator())) {
dbls.append(" ");
}
dbls.append(v);
}
else {
if (ints.length() > 0 && !ints.toString().endsWith(System.lineSeparator())) {
ints.append(" ");
}
ints.append(v);
}
});
return System.lineSeparator(); // return new-line
})
.forEach(s -> { ints.append(s); dbls.append(s); }); // keep lines in the results
// write the files using the contents from the string builders
try (
FileWriter intWriter = new FileWriter(intNums);
FileWriter dblWriter = new FileWriter(dblNums);
) {
intWriter.write(ints.toString());
dblWriter.write(dbls.toString());
}
}
// test
readAndWriteNumbers("test.dat", "ints.dat", "dbls.dat");
Output
//ints.dat
1 2 3 4 5
1 3 4 5 8
// dbls.dat
4.5 4.6 6.8 8.9
9.2 6.3 6.7 8.9

First and last name buffered reader giving me troubles

I've fixed many other issues but after fixing one that I thought was the last error, I came a Null pointer exception.
package com.Text.Scanner.java;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class TextScanner {
public static void main(String ...args) throws IOException{
Scanner sc = new Scanner(System.in);
System.out.println("Enter names for parsing");
String input = sc.nextLine();
ArrayList<String> names = new ArrayList<String>();
for (int i = 0;i<=input.length();i++) {
names.add(input.substring(0, input.indexOf(",")));
input = input.substring(input.indexOf(",")+1);
}
System.out.println(names);
// handles the string import to arraylist
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("sample.txt"));
//finds file
String line = reader.readLine();
//reads line
while (line != null) {
for (int i = 0; i <= line.length(); i++) {
if (line.contains(names.get(i))) {
//gets name from array to scan line for
System.out.println(line.substring(4, line.indexOf(names.get(i)) + names.get(i).length()));
//controls length
line = reader.readLine();
}
}
}
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
The goal here is to scan each line for a name (first and last) then print the number aside their name on the text file.
first line: 1234 Billy Smith
second line: 5678 John Smith
what you input: Billy Smith,John Smith,
The point of it is to scan the file for the list of first and last names in an arraylist and then output the numbers and name. The program works if I go in order in the whole list, but if I don't then it gives me this. I have tried to put reader.reset() if the if statement finds a match and then if it doesn't it reads another line, but that doesn't work
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:373)
at java.base/java.util.ArrayList.get(ArrayList.java:426)
at com.Text.Scanner.java.TextScanner.main(TextScanner.java:32)
for (int i = 0; i <= line.length(); i++) {
if (line.contains(names.get(i))) {
So basically i goes from O to line.length() (number of char in the line) and you use it to accces your names with name.get(i) that absolutely no reason to be the same size (it is more the number of lines on the first line)
But why don't you debug your code ? The error you got says the error is in TestScanner line 32 calling the method get. It already say where the error !
You could even rerun that program with the debugger, and ask it to stop when an exception is raised so it would stop exactly at the right place and you be able to look at the various variables.

Concatenation of Strings in new line in JAva

I'm trying to concatenate strings in new lines when a condition is met. This is my input:
Concept
soft top cove
tonneau cove
interior persennin
Concept
Innen
Innenraum
Platz im Inneren
All I want to do is to concatenate all the strings after the string concept and to get the following output:
lemma, surface
soft top cove, tonneau cove|interior persennin
Innen, Innenraum|Platz im Inneren
I know if a string value is equal concept I want to go to the other line and write the string of the next line before a comma, than the strings from the other lines delimited by "|" e.g. soft top cove, tonneau cove|interior persenning
This is my code so far. Any suggestions are welcome!
Thank you:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class Converter {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader inputcsv = null;
List <String> zeilencsv = new ArrayList<String>();
try {
inputcsv = Files.newBufferedReader(Paths.get("ErsteDatei.csv"));
String content;
while ((content = inputcsv.readLine()) != null) {
zeilencsv.add(content);
System.out.println(content);
}
File outputcsv = new File("TwoColumnsResult.csv");
//creates new file
outputcsv.createNewFile();
FileWriter csvFilewriter = new FileWriter(outputcsv);
//arraylist loop
int counter_a=0;
int counter = 1;
for (String zeile:zeilencsv){
String concept = "Concept";
//check string value =concept?
if(zeile.toString().equals(concept)){
zeile="lemma,surface";
for(String zeile2:zeilencsv){
//here I don't know how to say give me the next line, write it as a word , put comma and than concatenate with a |
}
}
else {
counter++;
}
csvFilewriter.write(zeile+"\n");
counter++;
}
//write
csvFilewriter.flush();
//closes the file
csvFilewriter.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
so if I understood correctly you want that after finding "Concept" save the next line as text, concatenate a comma, then save the next line and concatenate a | and then save the next?
Well the code that you're using is kind of bizarre but what I would do is the next:
zeile="lemma,surface";
// I'll put a counter to distinguish where to put "," and "|"
int counter = 0;
// Then I need a string variable to save the line
String line = "";
for(String zeile2:zeilencsv){
if (count == 0){ //First iteration
line = zeile2.toString();
}
if(count == 1){ . //Second iteration add the comma
line = line + "," + zeile2.toString();
}
if(count == 2){ . //Second iteration add the pipe
line = line + "|" + zeile2.toString();
}
count++;
}
I hope that this is what you're looking for. If you would like to optimize the code, feel free to send me a message and we could work together.

Wrong Number of Characters (String Munipulation from File) -- Java

I am currently testing out a program who's purpose is to import a file and find the number of characters in that file using string manipulation.
I am currently using System.out.println(fileone.length()); to do so. However each time I try and run the program it reads the number of characters wrong, in fact it gives the same wrong number every time. Down below is the output of the program.
My name is Sam Tyler 54
As you can see the words in the file are displayed and next to that is the character number which is obviously wrong, as I counted myself, it should be 19, including spaces.
Here is the code that is reading the file and giving the character number.
Can you see any problems?
Any help is much appreciated.
Scanner ourScanner = new Scanner(new File(fileone));
System.out.println();
while(ourScanner.hasNextLine())
{
System.out.print (ourScanner.nextLine() + "\t");
count++;
if (count%4 == 0)
System.out.println();
}
}
//System.out.println ("\n\n" + count + " \ntotal strings found.");
System.out.println(fileone.length());
You are checking the length of the String representing the file path (fileone), not the actual file scanned, so it will be invariant given the context.
If you're counting the number of lines, just print your count variable.
You also have a number of ways to check for file length from a File object , or for specific character length (since the file system may allocate more space than actually used for the file), you can count them through iteration with a FileReader, etc.
fileone.length() gives you the length of the path of file.
But new File(fileone).length() will give you the file size in number of bytes (assuming one character is one byte)
Try this example:
1: read all the lines of the file
2: convert to a string
3: remove the white spaces
4: display the length in byte for the resulting string
package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class test {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File("C:\\Users\\xxx\\test.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
StringBuffer fileContents = new StringBuffer();
String line = br.readLine();
while (line != null) {
fileContents.append(line);
line = br.readLine();
}
String sValue = fileContents.toString();
//remove all whitespace
sValue = sValue.replaceAll("\\s+","");
System.out.println(sValue.length());
}
}
Test:
test.txt content: My name is Sam Tyler 54 --> 18 characters
result:
18

Result of Java split() is varies when working with string of numbers

Why does Java String.split() generate different results when working with string defined in code versus string read from a file when numbers are involved? Specifically I have a file called "test.txt" that contains chars and numbers separated by spaces:
G H 5 4
The split method does not split on spaces as expected. But if a string variable is created within code with same chars and numbers separated by spaces then the result of split() is four individual strings, one for char and number. The code below demonstrates this difference:
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
public class SplitNumber {
//Read first line of text file
public static void main(String[] args) {
try {
File file = new File("test.txt");
FileReader fr = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fr);
String firstLine;
if ((firstLine = bufferedReader.readLine()) != null) {
String[] firstLineNumbers = firstLine.split("\\s+");
System.out.println("First line array length: " + firstLineNumbers.length);
for (int i=0; i<firstLineNumbers.length; i++) {
System.out.println(firstLineNumbers[i]);
}
}
bufferedReader.close();
String numberString = "G H 5 4";
String[] numbers = numberString.split("\\s+");
System.out.println("Numbers array length: " + numbers.length);
for (int i=0; i<numbers.length; i++) {
System.out.println(numbers[i]);
}
} catch(Exception exception) {
System.out.println("IOException occured");
exception.printStackTrace();
}
}
}
The result is:
First line array length: 3
G
H
5 4
Numbers array length: 4
G
H
5
4
Why do the numbers from the file not get parsed the same as the same string defined within code?
Based on feedback I changed the regex to split("[\\s\\h]+") which resolved the issue; the numbers for the file were properly split which clearly indicated that I had a different whitespace-like character in the text file that I was using. I then replaced the contents of the file (using notepad) and reverted back to split("\\s+") and found that it worked correctly this time. So at some point I must have introduced different white-space like characters in the file (maybe a copy/paste issue). In the end the take away is I should use split("[\\s\\h]+") when reading from a file where I want to split on spaces as it will cover more scenarios that may not be immediately obvious.
Thanks to all for helping me find the root cause of my issue.

Categories