Java - Regex error when using "\\" as a delimeter [duplicate] - java

This question already has answers here:
Why String.replaceAll() in java requires 4 slashes "\\\\" in regex to actually replace "\"?
(6 answers)
Closed 7 years ago.
The following code is from a method in a class that I am making to modify a list of file directories in a program folder. However I am trying to use "\" as a delimiter for a scanner as I only need the start of the directory "S:\" and the last part which is just name of a sub folder. So for example it looks like this:
F:\Data\Subfolder\Another
The code complies but when I run the method i get this following run time error:
java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
And was just wondering if anyone knows what it means and how I can stop it from happening. Is it because of using the \ for a delimeter?
Note: newFolder class is a nested class
public void scanFiles() throws IOException{
try
{
System.out.println("Sage 2015 is Installed on this machine");
File companyFile = new File(sageFolders[8] + "\\COMPANY");
Scanner input = new Scanner(new BufferedReader(new FileReader(companyFile)));
input.useDelimiter("\\");
while(input.hasNextLine())
{
if(line.contains("F"))
{
String drive = input.next();
String dataFolder = input.next();
String sageFolder = input.next();
String clientFolder = input.next();
newFolders.add(new newFolder(drive, clientFolder));
}
}
//Close the Readers
fileReader.close();
bufferedReader.close();
//fileWriter = new FileWriter(companyFile);
//bufferedWriter = new BufferedWriter(fileWriter);
//Write back to file
//fileWriter.flush();
//bufferedWriter.close();
}
catch(FileNotFoundException e)
{
System.out.println("File not Found: Moving onto next Version");
}
}
class newFolder
{
private String driveLetter;
private String clientFolder;
public newFolder(String driveLetter, String clientFolder)
{
this.driveLetter = driveLetter;
this.clientFolder = clientFolder;
}
}

Since \ is regex special character and also special in Java, you have to escape your java backslash with \\ and also your regex backslash with \\, hence... you have four backslashes \\\\
You have to use:
input.useDelimiter("\\\\");

Related

I was writing a program for finding a file's characteristics. Why is it showing "unclosed character literal" in s.lastIndexOf String function?

I was writing a program to find a file's path, name and extension. Here is the program:
import java.util.Scanner;
class File_characteristics
{
public static void main()
{
System.out.println("\f"); // clearing the screen
Scanner sc = new Scanner(System.in);
System.out.println("Enter the file's path");
String s = sc.nextLine();
int a = s.lastIndexOf('\'); // Error of "unclosed character literal" is showing up
String b = s.substring(0,a+1); // finding the file's path
int c = s.lastIndexOf('.'); // Error isn't showing here though
String d = s.substring(a+1,c); // finding the file's name
String e = s.substring(c+1); // finding the file's extension
System.out.println("Path: "+b);
System.out.println("File name: "+d);
System.out.println("Extension: "+e);
} // method ends
} // class ends
For some reason, it shows "unclosed character literal" error on the line int a = s.lastIndexOf('\');and I have no idea why. I have added the single quotation marks around the character as such '' but it's showing the error. How do I fix this?
The \ is an escape character, which marks the beginning of an escape sequence. The compiler understands that you are beginning a character literal (with ') then attempting to write a single quote (with \'), so the closing ' is missing, and hence the error. To refer to "\" literally, you need to escape it as well:
int a = s.lastIndexOf('\\')
You can use the file class to extract the details. Take a look at the code below:
File file = new File("filePath");
String path = file.getParent();
String fileNameWithExtension = file.getName();
String[] split = fileNameWithExtension.split("\\.");
String fileName = split[0];
String extension = split[1];
System.out.println("Path: "+path);
System.out.println("File name: "+fileName);
System.out.println("Extension: "+extension);

Exception read data from file while split character "|"

public void loadDataFromFile(ArrayList<Book> list, String fileName) {
File f = new File(fileName);
try {
Scanner sc = new Scanner(f);
while (sc.hasNext()) {
String perLine = sc.nextLine(); //get date per line
String txt[] = perLine.split("|");
list.add(new Book(txt[0], txt[1], Integer.parseInt(txt[2]), Double.parseDouble(txt[3])));
}
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
}
}
My function read data from file and add data to arraylist. But when I run this function, it have bug like this picture
My file is book.txt and data of this file is
A2|Hoa|22|50.3. If i try split at character "|", it will have bug like this picture. But if i change data of file to A2 Hoa 22 50.3 and split at " " it working.
In java String split() method work's with regex as argument since '|' is special character in regex you need to escape it with \\ as folowing perLine.split("\\|");

What am I missing? NumberFormatException error

I want to read from a txt file which contains just numbers. Such file is in UTF-8, and the numbers are separated only by new lines (no spaces or any other things) just that. Whenever i call Integer.valueOf(myString), i get the exception.
This exception is really strange, because if i create a predefined string, such as "56\n", and use .trim(), it works perfectly. But in my code, not only that is not the case, but the exception texts says that what it couldn't convert was "54856". I have tried to introduce a new line there, and then the error text says it couldn't convert "54856
"
With that out of the question, what am I missing?
File ficheroEntrada = new File("C:\\in.txt");
FileReader entrada =new FileReader(ficheroEntrada);
BufferedReader input = new BufferedReader(entrada);
String s = input.readLine();
System.out.println(s);
Integer in;
in = Integer.valueOf(s.trim());
System.out.println(in);
The exception text reads as follows:
Exception in thread "main" java.lang.NumberFormatException: For input string: "54856"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Integer.parseInt(Integer.java:658)
at java.base/java.lang.Integer.valueOf(Integer.java:989)
at Quicksort.main(Quicksort.java:170)
The file in.txt consists of:
54856
896
54
53
2
5634
Well, aparently it had to do with Windows and those \r that it uses... I just tried executing it on a Linux VM and it worked. Thanks to everyone that answered!!
Try reading the file with Scanner class has use it's hasNextInt() method to identify what you are reading is Integer or not. This will help you find out what String/character is causing the issue
public static void main(String[] args) throws Exception {
File ficheroEntrada = new File(
"C:\\in.txt");
Scanner scan = new Scanner(ficheroEntrada);
while (scan.hasNext()) {
if (scan.hasNextInt()) {
System.out.println("found integer" + scan.nextInt());
} else {
System.out.println("not integer" + scan.next());
}
}
}
If you want to ensure parsability of a string, you could use a Pattern and Regex that.
Pattern intPattern = Pattern.compile("\\-?\\d+");
Matcher matcher = intPattern.matcher(input);
if (matcher.find()) {
int value = Integer.parseInt(matcher.group(0));
// ... do something with the result.
} else {
// ... handle unparsable line.
}
This pattern allows any numbers and optionally a minus before (without whitespace). It should definetly parse, unless it is too long. I don't know how it handles that, but your example seems to contain mostly short integers, so this should not matter.
Most probably you have a leading/trailing whitespaces in your input, something like:
String s = " 5436";
System.out.println(s);
Integer in;
in = Integer.valueOf(s.trim());
System.out.println(in);
Use trim() on string to get rid of it.
UPDATE 2:
If your file contains something like:
54856\n
896
54\n
53
2\n
5634
then use following code for it:
....your code
FileReader enter = new FileReader(file);
BufferedReader input = new BufferedReader(enter);
String currentLine;
while ((currentLine = input.readLine()) != null) {
Integer in;
//get rid of non-numbers
in = Integer.valueOf(currentLine.replaceAll("\\D+",""));
System.out.println(in);
...your code

java.lang.NumberFormatException: For input string: "22"

public void loadFromFile(String filename) {
File file = new File(filename);
BufferedReader br;
try {
br = new BufferedReader(new FileReader(file));
numberOfAttributes = Integer.parseInt(br.readLine());
}
...
}
Above is my program: I am trying to read from a txt file where the first line is the number 22 and nothing more. I don't know why the program gives me an exception.
Try stripping any whitespace from the string:
numberOfAttributes = Integer.parseInt(br.readLine().trim());
I think you might have a UTF-8 BOM (byte-order mark) at the start of your file.
Here's a class that reproduces the error:
import java.io.*;
public class BomTest {
public static void main(String[] args) throws Exception {
File file = new File("example.txt");
// Write out UTF-8 BOM, followed by the number 22 and a newline.
byte[] bs = { (byte)0xef, (byte)0xbb, (byte)0xbf, (byte)'2', (byte)'2', 10 };
FileOutputStream fos = new FileOutputStream(file);
fos.write(bs);
fos.close();
BufferedReader r = new BufferedReader(new FileReader(file));
String s = r.readLine();
System.out.println(Integer.parseInt(s));
}
}
When I run this class, I get the following output:
luke#computer:~$ java BomTest
Exception in thread "main" java.lang.NumberFormatException: For input string: "22"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:481)
at java.lang.Integer.parseInt(Integer.java:514)
at BomTest.main(BomTest.java:15)
There isn't really an easy way to deal with UTF-8 BOMs in Java; it's best not to generate them in the first place. See also this answer.
br.readLine() reads the entire line including the new line special character.Apart, form the solution suggested by James, you can use Scanner#nextInt().
try with numberOfAttributes = Integer.parseInt(br.readLine().trim());
public String trim()
Returns a copy of the string, with leading and trailing whitespace
omitted. If this String object represents an empty character sequence,
or the first and last characters of character sequence represented by
this String object both have codes greater than '\u0020' (the space
character), then a reference to this String object is returned.
Otherwise, if there is no character with a code greater than '\u0020'
in the string, then a new String object representing an empty string
is created and returned.
This happens because you have a space in the input line. Look at these:
int i1 = Integer.parseInt("22 ");
int i2 = Integer.parseInt("22a");
int i3 = Integer.parseInt("2 2");
int i4 = Integer.parseInt("22\n");
All of them generate exception. I suggest you to trim, tokenize or substitute. But in general, it doesn't sound to me a good solution to read a number from a file in that way.
If you really need to store data, why don't you create an object ad hoc and serialize/deserialize it?
You might have a null character in your string. Remove it using a regEx "\d+".
NumberFormatException is raised because the input string is not in expected number format. Generally, you can see 'the wrong string input' in the error message and can easily identify the bug. But in your case, the catch is that the error message does not display the string input completely (because it does not displays the null character).
Check the below output and the code.
public class TestParseInt{
private static final Pattern pattern = Pattern.compile("\\d+");
public static void main(String []args){
String a = "22\0";
try {
System.out.println("Successfull parse a: " + Integer.parseInt(a));
} catch(NumberFormatException e) {
System.out.println("Error:" +e.getMessage());
}
try {
Matcher matcher = pattern.matcher(a);
if(matcher.find()) {
System.out.println("Succesfull parse a: " +
Integer.parseInt(matcher.group(0)));
}
} catch(NumberFormatException e) {
System.out.println("Error" + e.getMessage());
}
}
}
Output:
Error:For input string: "22"
Succesfull parse a: 22

Java remove complete line from file [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java - Find a line in a file and remove
I am trying to remove a complete line from a text file, and have managed to remove the line if there is only one single unbroken line of text without spaces. If i have a space delimiter between strings it fails to remove anything.
Code as follows:
import java.io.*;
import java.util.Scanner;
public class removebooks {
// construct temporary file
public static void main(String[]args)throws IOException {
String title;
Scanner titlerem= new Scanner (System.in);
System.out.println("Enter Title to remove from file");
title = titlerem.next ();
// construct temporary file
File inputFile = new File("books.txt");
File tempFile = new File(inputFile + "temp.txt");
BufferedReader br = new BufferedReader (new FileReader("books.txt"));
PrintWriter Pwr = new PrintWriter(new FileWriter (tempFile));
String line = null;
//read from original, write to temporary and trim space, while title not found
while((line = br.readLine()) !=null) {
if(line.trim().equals(title)){
continue; }
else{
Pwr.println(line);
Pwr.flush();
}
}
// close readers and writers
br.close();
Pwr.close();
titlerem.close();
// delete book file before renaming temp
inputFile.delete();
// rename temp file back to books.txt
if(tempFile.renameTo(inputFile)){
System.out.println("Update succesful");
}else{
System.out.println("Update failed");
}
}
}
the text file is called books.txt and contents simply should look like:
bookone author1 subject1
booktwo author2 subject2
bookthree author3 subject3
bookfour author4 subject4
thank you any help would be appreciated
Why don't you use
if(line.trim().startsWith(title))
instead of
if(line.trim().equals(title))
because equals() is only true if both strings are equal, and startsWith() is true if line.trim() starts with title ;)
As you are reading the file line by line. You can make use of following
if(line.contains(title)){
// do something
}
In this case you will not be limited by title only.
String API
br.readLine() sets the value of variable line to "bookone author1 subject1".
Scanner.next() delimits by whitespace. You need to consolidate all your calls to Scanner.next() to a single String before checking against the lines in the file, if that is your intent.
In your case, if you typed "bookone author1 subject1", value of variable title would be "bookone" after your call to Scanner.next().

Categories