15
File fil = new File("Tall.txt");
FileReader inputFil = new FileReader(fil);
BufferedReader in = new BufferedReader(inputFil);

int [] tall = new int [100];

String s =in.readLine();

while(s!=null)
{
    int i = 0;
    tall[i] = Integer.parseInt(s); //this is line 19
    System.out.println(tall[i]);
    s = in.readLine();
}

in.close();

I am trying to use the file "Tall.txt" to write the integers contained in them into the array named "tall". It does this to some extent, but also when I run it, it throws the following exception (:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at BinarySok.main(BinarySok.java:19)

Why exactly does it do this, and how do I remove it? As I see it, I read the file as strings, and then convert it to ints, which isn't illegal.

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
Northener
  • 863
  • 4
  • 9
  • 14
  • BTW, you should declare "i" outside the while loop. if not you will always insert the ints at index 0 of your array. – Julien Grenier Nov 20 '08 at 01:23
  • 1
    BTW, the comment "this is line 19" is a candidate for "Best comment ever seen". What IDE your are using? – Markus Lausberg Apr 27 '09 at 08:28
  • I have absolutely no idea how that got there. I think I took parts of the code from somewhere, and apparently the comments came along. – Northener May 10 '09 at 04:12

6 Answers6

44

You might want to do something like this (if you're in java 5 & up)

Scanner scanner = new Scanner(new File("tall.txt"));
int [] tall = new int [100];
int i = 0;
while(scanner.hasNextInt()){
   tall[i++] = scanner.nextInt();
}
Julien Grenier
  • 3,364
  • 2
  • 30
  • 43
9

You must have an empty line in your file.

You may want to wrap your parseInt calls in a "try" block:

try {
  tall[i++] = Integer.parseInt(s);
}
catch (NumberFormatException ex) {
  continue;
}

Or simply check for empty strings before parsing:

if (s.length() == 0) 
  continue;

Note that by initializing your index variable i inside the loop, it is always 0. You should move the declaration before the while loop. (Or make it part of a for loop.)

erickson
  • 265,237
  • 58
  • 395
  • 493
3

For comparison, here is another way to read the file. It has one advantage that you don't need to know how many integers there are in the file.

File file = new File("Tall.txt");
byte[] bytes = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(bytes);
fis.close();
String[] valueStr = new String(bytes).trim().split("\\s+");
int[] tall = new int[valueStr.length];
for (int i = 0; i < valueStr.length; i++) 
    tall[i] = Integer.parseInt(valueStr[i]);
System.out.println(Arrays.asList(tall));
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

It looks like Java is trying to convert an empty string into a number. Do you have an empty line at the end of the series of numbers?

You could probably fix the code like this

String s = in.readLine();
int i = 0;

while (s != null) {
    // Skip empty lines.
    s = s.trim();
    if (s.length() == 0) {
        continue;
    }

    tall[i] = Integer.parseInt(s); // This is line 19.
    System.out.println(tall[i]);
    s = in.readLine();
    i++;
}

in.close();
Yojimbo
  • 23,288
  • 5
  • 44
  • 48
Jan Aagaard
  • 10,940
  • 8
  • 45
  • 80
1

You might have confusions between the different line endings. A Windows file will end each line with a carriage return and a line feed. Some programs on Unix will read that file as if it had an extra blank line between each line, because it will see the carriage return as an end of line, and then see the line feed as another end of line.

Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
  • 1
    No, it doesn't matter what platform the app is running on, BufferedReader always checks for all three kinds of line separator: \r\n (carriage-return + linfeed), \n (linefeed only), and \r (carriage-return only). – Alan Moore Nov 20 '08 at 00:46
0
File file = new File("E:/Responsibility.txt");  
    Scanner scanner = new Scanner(file);
    List<Integer> integers = new ArrayList<>();
    while (scanner.hasNext()) {
        if (scanner.hasNextInt()) {
            integers.add(scanner.nextInt());
        } else {
            scanner.next();
        }
    }
    System.out.println(integers);
Ban
  • 339
  • 1
  • 5
  • 16