Java throws NumberFormatException - java

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.

Related

Is there any way to let the program recognize "\n" in text files as line break code?

I've been creating a game in Java for a while and I used to write all the in-game texts directly in my code like this:
String text001 = "You're in the castle.\n\nWhere do you go next?"
But recently I decided to write all the in-game texts in a text file and tried to let the program read them and put them into a String array since the amount of the texts has increased a lot and it made my code incredibly long. The reading went well except one thing. I've inserted line break codes in dialogues and although the code worked properly when I wrote it directly in my code, they are no longer recognized as line break code when I try to read them from a text file.
It is supposed to be displayed as:
You're in the castle.
Where do you go next?
But now it is displayed as:
You're in the castle.\n\nWhere do you go next?
The code doesn't recognize "\n" as line break code any more.
Here's the code :
import java.io.File;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
Scanner sc;
StringTokenizer token;
String line;
int lineNumber = 1;
String id[] = new String[100];
String text[] = new String[100];
try {
sc = new Scanner(new File("sample.txt"));
while ((line = sc.nextLine()) != null) {
token = new StringTokenizer(line, "|");
while (token.hasMoreTokens()) {
id[lineNumber] = token.nextToken();
text[lineNumber] = token.nextToken();
lineNumber++;
}
}
} catch (Exception e) {
}
System.out.println(text[1]);
String text001 = "You're in the castle.\n\nWhere do you go next?";
System.out.println(text001);
}
}
And this is the content of the text file:
castle|You're in the castle.\n\nWhere do you go next?
inn|You're in the inn. \n\nWhere do you go next?
I would be grateful if anyone tells me how to fix this. Thank you.
Just use
text[lineNumber] = token.nextToken().replace("\\n", "\n");
There is nothing inherently special about \n in a text file. It is just a \, followed by a \n.
It is only in Java (or other languages) which define that this sequence of characters - in a char or string literal - should be interpreted as a 0x0a (ASCII newline) character.
So, you can replace the character sequence with the one you want it to be interpreted as.

How to parse a text file and create a database record from it

I'm trying to basically make a simple Test Generator. I want a button to parse a text file and add the records to my database. The questions and answers are in a text file. I have been searching the net for examples but I can't find one that matches my situation.
The text file has header information that I want to ignore up until the line that starts with "~ End of Syllabus". I want "~ End of Syllabus" to indicate the beginning of the questions. A couple of lines after that look for a line with a "(" in the seventh character position. I want that to indicate the Question Number line. The Question Number line is unique in that the "(" is in the seventh character position. I want to use that as an indicator to mark the start of a new question. In the Question Number line, the first three characters together "T1A" are the Question Group. The last part of the T1A*01* is the question number within that group.
So, as you can see I will also need to get the actual question text line and the answer lines as well. Also typically after the four Answer lines is the Question Terminator indicated by "~~". I don't know how I would be able to do this for all the questions in the text file. Do I keep adding them to an array String? How would I access this information from the file and add it to a database. This is very confusing for me and the way I feel I could learn how this works is by seeing an example that covers my situation. Here is a link to the text file I'm talking about:http://pastebin.com/3U3uwLHN
Code:
public static void main(String args[]) {
String endOfSyllabus = "~ End of Syllabus";
Path objPath = Paths.get("2014HamTechnician.txt");
String[] restOfTextFile = null;
if (Files.exists(objPath)){
File objFile = objPath.toFile();
try(BufferedReader in = new BufferedReader(
new FileReader(objFile))){
String line = in.readLine();
List<String> linesFile = new LinkedList<>();
while(line != null){
linesFile.add(line);
line = in.readLine();
}
System.out.println(linesFile);
}
catch(IOException e){
System.out.println(e);
}
}
else{
System.out.println(
objPath.toAbsolutePath() + " doesn't exist");
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new A19015_Form().setVisible(true);
}
});
}
Reading a text file in Java is straight forward (and there are sure to be other, more creative/efficient ways to do this):
try (BufferedReader reader = new BufferedReader(new FileReader(path))) { //try with resources needs JDK 7
int lineNum = 0;
String readLine;
while ((readLine = reader.readLine()) != null) { //read until end of stream
Skipping an arbitrary amount of lines can be accomplished like this:
if (lineNum == 0) {
lineNum++;
continue;
}
Your real problem is the text to split on. Had you been using CSV you could use String[] nextLine = readLine.split("\t"); to split each line into its respective cells based on tab separation. But your not, so you'll be stuck with reading each line, and than find something to split on.
It seems like you're in control of the text file format. If you are, go to an easier to consume format such as CSV, otherwise you're going to be designing a custom parser for your format.
A bonus to using CSV is it can mirror a database very effectivly. I.e. your CSV header column = database column.
As far as databases go, using JDBC is easy enough, just make sure you use prepared statements to insert your data to prevent against SQL injection:
public Connection connectToDatabase(){
String url = "jdbc:postgresql://url";
return DriverManager.getConnection(url);
}
Connection conn = connectToDatabase();
PreparedStatement pstInsert = conn.prepareStatement(cInsert);
pstInsert.setTimestamp(1, fromTS1);
pstInsert.setString(2, nextLine[1]);
pstInsert.execute();
pstInsert.close();
conn.close();
--Edit--
I didn't see your pastebin earlier on. It doesn't appear that you're in charge of the file format, so you're going to need to split on spaces ( each word ) and rely on regular expressions to determine if this is a question or not. Fortunately it seems the file is fairly consistent so you should be able to do this without too much problem.
--Edit 2--
As a possible solution you can try this untested code:
try{
BufferedReader reader = new BufferedReader(new FileReader("file.txt")); //try with resources needs JDK 7
boolean doRegex = false;
String readLine;
while ((readLine = reader.readLine()) != null) { //read until end of stream
if(readLine.startsWith("~~ End of Syllabus")){
doRegex = true;
continue; //immediately goto the next iteration
}
if(doRegex){
String[] line = readLine.split(" "); //split on spaces
if(line[0].matches("your regex here")){
//answer should be line[1]
//do logic with your answer here
}
}
}
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}

Java String.split() - NumberFormatException

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>)

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.

Categories

Resources