Java String.split() - NumberFormatException - java

So, here's the thing, I've got this code:
public static void main(String[] args) {
try {
FileInputStream fstream = new FileInputStream("test.txt");
// Use DataInputStream to read binary NOT text.
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine = br.readLine();
String[] split = strLine.split(" ");
System.out.println(Integer.parseInt(split[0]));
in.close();
}
catch (Exception e) {//Catch exception if any
System.err.println("Error: " + e);
}
}
let's say I have a file named test.txt, with just "6 6". So, it reads first line and splits that line into two strings. The problem is that I can use Integer.parseInt for the split[1], but I can't use that method for split[0]
(System.out.println(split[0]) prints "6"), which outputs me an error of:
Error: java.lang.NumberFormatException: For input string: "6"
UPDATE:
It might be problem of eclipse, because if I compile my .java files in terminal with javac, I don't get any exceptions!:))
UPDATE2:
solved. something went wrong while saving with Kate. Don't know what, but gedit works better:D
Thank you all.

Just try with: hexdump -C test.txt if you have linux, you can see the non-printable chars you have.
Also the trim() answer it's fine.

I'd try the following to rule out spurious/unexpected characters:
.. setup/read code in main method...
String[] split = strLine.split(" ");
for (String s : split) {
System.out.println(String.format("[%s] => integer? %b", s, isInteger(s)));
}
... the rest of the main method....
private static boolean isInteger(String n) {
try {
Integer.parseInt(n);
} catch(NumberFormatException e) {
return false;
}
return true;
}
If you see anything inbetween the square brackets that isnt a number, or where the integer? returns false thats a likely the problem

This problem at the start of the file is usually due to a BOM, which some software (mainly Notepad in fact) like to put at the start of Unicode files.
Open the file in a good text editor and configure it to save the files without the BOM.
If you can't change the file, skip the first char when reading it.

If you are able to determine the actual encoding of the file you are reading, you can set it explicitly, so that any extra bytes are converted correctly into characters.
new InputStreamReader(new FileInputStream(...), <encoding>)

Related

How to neglect blank spaces in a string search in html file using java?

I have a html file which I have to search line by line and look for a particular string and then take some actions accordingly.
The problem is that the string is being matched to the entire line of the each line of the html file.
So if there are some spaces before the actual string in a given line, the match turns out to be false, even though it should be positive.
package read_txt;
import java.io.*;
class FileRead
{
public static void main(String args[])
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("textfile.html");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
//String a = "media query";
switch (strLine) {
case "#media query" :
System.out.println("media query found");
System.out.println("html file responsive");
break;
// default :
// System.out.println("html file unresponsive");
//break;
}
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
In my code above, I am searching for a String "media query". Now suppose this is the html file being searched :
The codes works fine for this html file, but now suppose we have this html file :
The string match does not work although a media query string is present, but if I change the matched string to " media query" instead of "media query", it works again.
Any idea how can I ignore the blank spaced occurring before appearance of any text in a line?
In this case, I would think that using "switch" is not the right way to go.
You might use
if (strLine.contains("media query"))
but that will fail if the line has "media query" (two spaces instead of one).
So, you best bet might be to use a regular expression.
You could use endsWith, e.g.
if (strLine.endsWith("media query")) { ...
In cases, where the searched string could be somewhere in the middle of line you could use indexOf, e.g.
if (strLine.indexOf("medial quera") >= 0) { ...

Why am I getting a NumberFormatException trying to parse an integer from string in Android?

I am looking for an update function for a custom ROM based tool. I used this code http://www.androidsnippets.com/check-for-updates-once-a-day as a base and soon realized that I couldn't use Dropbox for deployment with that code. So I modified the code to download a version.txt file from dropbox. The file contains a single number (the latest Version Code, in this case 11). I need to read that line and parse the string as an integer to compare with the current versionCode and trigger a download if an update exists.
All the code works, except for parsing the int from the txt file.
Heres my code:
private Thread checkUpdate = new Thread() {
public void run() {
try {
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"BPversion.txt");
StringBuilder text = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
int curVersion = getPackageManager().getPackageInfo("com.JB15613.BPcolorTool", 0).versionCode;
System.out.println(line);
int newVersion = 0;
System.out.println(line);
try {
newVersion = Integer.parseInt(line);
System.out.println(line);
} catch(NumberFormatException nfe) {
System.out.println("Exception " + nfe);
}
if (newVersion > curVersion) {
mHandler.post(showUpdate);
}
}
} catch (Exception e) {
Log.d("ANDRO_ASYNC", "Caught exception");
}
}
And I get this exception:
Exception java.lang.NumberFormatException: Invalid int: "11"
When I apply breakpoints and run the debugger, it crashes at the parseInt line.
11 looks like a valid int to me!
Any help is greatly appreciated!
line ends CRLF ,so , need call function trim() in String ..
int newVersion = Integer.parseInt(line.trim());
I just want to add to what has already been stated:
Your problem is that your 11 is not able to parse to an int, the underlying root of which is that you are parsing white space as part of your string.
As already stated the trim() method should remove the problem but If you wanted to know what was causing the problem and why this fixes it then that is the reason.
You are not trying to parse "11", you are parsing something like "11 " or " 11" which is a different thing. The spaces from the file you are reading from are being included in your string as it is passed into the parseInt method.
Again, I know this has been answered but I thought this additional information would still be useful.
Try to print the value of line in the logcat or screen or ui.
Check what will be coming.A valid int is coming or any other is coming.
i think line does not cotains valid int.
Exception java.lang.NumberFormatException
The above exception is thrown due to invalid int format or number format.
Check the values of line

Parse a text file into multiple text file

I want to get multiple file by parsing a input file Through Java.
The Input file contains many fasta format of thousands of protein sequence and I want to generate raw format(i.e., without any comma semicolon and without any extra symbol like ">", "[", "]" etc) of each protein sequence.
A fasta sequence starts form ">" symbol followed by description of protein and then sequence of protein.
For example ► >lcl|NC_000001.10_cdsid_XP_003403591.1 [gene=LOC100652771]
[protein=hypothetical protein LOC100652771] [protein_id=XP_003403591.1] [location=join(12190..12227,12595..12721,13403..13639)]
MSESINFSHNLGQLLSPPRCVVMPGMPFPSIRSPELQKTTADLDHTLVSVPSVAESLHHPEITFLTAFCL
PSFTRSRPLPDRQLHHCLALCPSFALPAGDGVCHGPGLQGSCYKGETQESVESRVLPGPRHRH
Like above formate the input file contains 1000s of protein sequence. I have to generate thousands of raw file containing only individual protein sequence without any special symbol or gaps.
I have developed the code for it in Java but out put is : Cannot open a file followed by cannot find file.
Please help me to solve my problem.
Regards
Vijay Kumar Garg
Varanasi
Bharat (India)
The code is
/*Java code to convert FASTA format to a raw format*/
import java.io.*;
import java.util.*;
import java.util.regex.*;
import java.io.FileInputStream;
// java package for using regular expression
public class Arrayren
{
public static void main(String args[]) throws IOException
{
String a[]=new String[1000];
String b[][] =new String[1000][1000];
/*open the id file*/
try
{
File f = new File ("input.txt");
//opening the text document containing genbank ids
FileInputStream fis = new FileInputStream("input.txt");
//Reading the file contents through inputstream
BufferedInputStream bis = new BufferedInputStream(fis);
// Writing the contents to a buffered stream
DataInputStream dis = new DataInputStream(bis);
//Method for reading Java Standard data types
String inputline;
String line;
String separator = System.getProperty("line.separator");
// reads a line till next line operator is found
int i=0;
while ((inputline=dis.readLine()) != null)
{
i++;
a[i]=inputline;
a[i]=a[i].replaceAll(separator,"");
//replaces unwanted patterns like /n with space
a[i]=a[i].trim();
// trims out if any space is available
a[i]=a[i]+".txt";
//takes the file name into an array
try
// to handle run time error
/*take the sequence in to an array*/
{
BufferedReader in = new BufferedReader (new FileReader(a[i]));
String inline = null;
int j=0;
while((inline=in.readLine()) != null)
{
j++;
b[i][j]=inline;
Pattern q=Pattern.compile(">");
//Compiling the regular expression
Matcher n=q.matcher(inline);
//creates the matcher for the above pattern
if(n.find())
{
/*appending the comment line*/
b[i][j]=b[i][j].replaceAll(">gi","");
//identify the pattern and replace it with a space
b[i][j]=b[i][j].replaceAll("[a-zA-Z]","");
b[i][j]=b[i][j].replaceAll("|","");
b[i][j]=b[i][j].replaceAll("\\d{1,15}","");
b[i][j]=b[i][j].replaceAll(".","");
b[i][j]=b[i][j].replaceAll("_","");
b[i][j]=b[i][j].replaceAll("\\(","");
b[i][j]=b[i][j].replaceAll("\\)","");
}
/*printing the sequence in to a text file*/
b[i][j]=b[i][j].replaceAll(separator,"");
b[i][j]=b[i][j].trim();
// trims out if any space is available
File create = new File(inputline+"R.txt");
try
{
if(!create.exists())
{
create.createNewFile();
// creates a new file
}
else
{
System.out.println("file already exists");
}
}
catch(IOException e)
// to catch the exception and print the error if cannot open a file
{
System.err.println("cannot create a file");
}
BufferedWriter outt = new BufferedWriter(new FileWriter(inputline+"R.txt", true));
outt.write(b[i][j]);
// printing the contents to a text file
outt.close();
// closing the text file
System.out.println(b[i][j]);
}
}
catch(Exception e)
{
System.out.println("cannot open a file");
}
}
}
catch(Exception ex)
// catch the exception and prints the error if cannot find file
{
System.out.println("cannot find file ");
}
}
}
If you provide me correct it will be much easier to understand.
This code will not win prices, due to missing java expertice. For instance I would expect OutOfMemory even if it is correct.
Best would be a rewrite. Nevertheless we all began small.
Give full path to file. Also on the output the directory is probably missing from the file.
Better use BufferedReader etc. i.o. DateInputStream.
Initialize i with -1. Better use for (int i = 0; i < a.length; ++i).
Best compile the Pattern outside the loop. But remove the Matcher. You can do if (s.contains(">") as well.
. One does not need to create a new file.
Code:
const String encoding = "Windows-1252"; // Or "UTF-8" or leave away.
File f = new File("C:/input.txt");
BufferedReader dis = new BufferedReader(new InputStreamReader(
new FileInputStream(f), encoding));
...
int i= -1; // So i++ starts with 0.
while ((inputline=dis.readLine()) != null)
{
i++;
a[i]=inputline.trim();
//replaces unwanted patterns like /n with space
// Not needed a[i]=a[i].replaceAll(separator,"");
Your code contains the following two catch blocks:
catch(Exception e)
{
System.out.println("cannot open a file");
}
catch(Exception ex)
// catch the exception and prints the error if cannot find file
{
System.out.println("cannot find file ");
}
Both of these swallow the exception and print a generic "it didn't work" message, which tells you that the catch block was entered, but nothing more than that.
Exceptions often contain useful information that would help you track down where the real problem is. By ignoring them, you're making it much harder to diagnose your problem. Worse still, you're catching Exception, which is the superclass of a lot of exceptions, so these catch blocks are catching lots of different types of exceptions and ignoring them all.
The simplest way to get information out of an exception is to call its printStackTrace() method, which prints the exception type, exception message and stack trace. Add a call to this within both of these catch blocks, and that will help you see more clearly what exception is being thrown and from where.

Strings written to file do not preserve line breaks

I am trying to write a String(lengthy but wrapped), which is from JTextArea. When the string printed to console, formatting is same as it was in Text Area, but when I write them to file using BufferedWriter, it is writing that String in single line.
Following snippet can reproduce it:
public class BufferedWriterTest {
public static void main(String[] args) throws IOException {
String string = "This is lengthy string that contains many words. So\nI am wrapping it.";
System.out.println(string);
File file = new File("C:/Users/User/Desktop/text.txt");
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(string);
bufferedWriter.close();
}
}
What went wrong? How to resolve this? Thanks for any help!
Text from a JTextArea will have \n characters for newlines, regardless of the platform it is running on. You will want to replace those characters with the platform-specific newline as you write it to the file (for Windows, this is \r\n, as others have mentioned).
I think the best way to do that is to wrap the text into a BufferedReader, which can be used to iterate over the lines, and then use a PrintWriter to write each line out to a file using the platform-specific newline. There is a shorter solution involving string.replace(...) (see comment by Unbeli), but it is slower and requires more memory.
Here is my solution - now made even simpler thanks to new features in Java 8:
public static void main(String[] args) throws IOException {
String string = "This is lengthy string that contains many words. So\nI am wrapping it.";
System.out.println(string);
File file = new File("C:/Users/User/Desktop/text.txt");
writeToFile(string, file);
}
private static void writeToFile(String string, File file) throws IOException {
try (
BufferedReader reader = new BufferedReader(new StringReader(string));
PrintWriter writer = new PrintWriter(new FileWriter(file));
) {
reader.lines().forEach(line -> writer.println(line));
}
}
Please see the following question on how to appropriately handle newlines.
How do I get a platform-dependent new line character?
Basically you want to use
String newLineChar = System.getProperty("line.separator");
and then use the newLineChar instead of "\n"
I just ran your program, and adding a carriage return (\r) before your newline (\n) did the trick for me.
If you want to get a system independent line separator, one can be found in the system propery line.separator
String separator = System.getProperty("line.separator");
String string = "This is lengthy string that contains many words. So" + separator
+ "I am wrapping it.";
If you wish to keep the carriage return characters from a Java string into a file. Just replace each break line character (which is recognized in java as: \n) as per the following statement:
TempHtml = TempHtml.replaceAll("\n", "\r\n");
Here is an code example,
// When Execute button is pressed
String TempHtml = textArea.getText();
TempHtml = TempHtml.replaceAll("\n", "\r\n");
try (PrintStream out = new PrintStream(new FileOutputStream("C:/Temp/temp.html"))) {
out.print(TempHtml);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(TempHtml);
If you are using a BufferedWriter, you could also use the .newline() method to re-add the newline based on your platform.
See this related question: Strings written to file do not preserve line breaks

Java throws NumberFormatException

I have a text file where first two lines are integers m and n, then there are m lines that each has n pipe-delimited values. I wrote a program that reads the file and creates m*n array with the values from the file, and it worked fine for bajillion times, and then out of sudden, with the same code, with the same file, it threw NumberFormatException while reading the integer from the first line. The whole code is here:
public class Thegame extends JFrame {
public Integer st;
public Integer el;
public String[][] tab;
public Thegame(String pth)
{
setSize(640,480);
setTitle(pth);
File file = new File(pth);
try
{
BufferedReader rdr = new BufferedReader(new FileReader(file));
st = Integer.valueOf(rdr.readLine());
el = Integer.valueOf(rdr.readLine());
tab = new String[st][el];
for(Integer i=0; i<st; i++)
{
String lin = rdr.readLine();
StringTokenizer spl = new StringTokenizer(lin,"|");
for(Integer j=0; j<el; j++)
{
tab[i][j] = spl.nextToken();
}
}
rdr.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex);
}
}
}
What really worries me is that the same code worked okay before and out of nowhere it turned out to be bad, so I can't even tell what exactly is wrong...
Something must've changed, otherwise it's magic. Possible suspects:
the content of the input file has changed. Have you checked it with some kind of hex editor. Does the file start with digits indeed?
Java/system locale have changed. What previously was treated as a number, no longer is a number in the new locale. Typical catches with locale are thousands separators (,).
The two possible problem lines are:
st = Integer.valueOf(rdr.readLine());
el = Integer.valueOf(rdr.readLine());
You need to make sure that what the reader is reading is actually an Integer.
Try the following small modification:
st = Integer.valueOf(rdr.readLine().trim());
el = Integer.valueOf(rdr.readLine().trim());
If that doesn't fix it, you need to make sure that you are capturing the correct input.
To help debug the problem, it may be helpful to save the input to a string and print it to see what it looks like before trying to parse it:
String stStr = rdr.readLine().trim()
System.out.println(stStr);
st = Integer.valueOf(stStr);
System.out.println(stStr);
String elStr = rdr.readLine().trim();
el = Integer.valueOf(elStr);
If the values that are printed aren't integers, then you are getting the input incorrectly.
You didn't happen to somehow get some special characters placed before the first line in the file did you? I know I had that issue once and it took me forever to figure out what was going on. I think notepad++ will let you see if that's the case.
If the behavior changed, something did change.
If the code hasn't changed then either:
The file being read changed: look for special (non readable) characters with a hex editor. If you have an input file that doesn’t cause the exception, then compare it with the one that does (again, hex comparison).
The java environment changed: check for changes in your Java virtual machine configuration, specially the locale.
Change:
st = Integer.valueOf(rdr.readLine());
el = Integer.valueOf(rdr.readLine());
to be
String input = rdr.readLine();
try {
st = Integer.valueOf( input);
} catch( NumberFormatException e){
System.out.println( "exception reading " + input );
}
input = rdr.readLine();
try {
el = Integer.valueOf(input);
} catch( NumberFormatException e){
System.out.println( "exception reading " + input );
}
and you'll get your answer of what the problem string is printed out.

Categories

Resources