0

I'm trying to create a program which takes input from a CSV-file and writes it to a Java-created Access database and table. The program uses a while-loop to go through the CSV-file. The first line of the file is written perfectly to the database, but it crashes at the second line, while trying to write the same kind of input to the table. How can I solve this? Here's my code so far:

public void GPXtoAccess() {
    try {
        Access = new Scanner(DummyCSV);
        Scanner = new Scanner(DummyCSV);

        while (Access.hasNextLine()) {
            Scanner.useDelimiter(";");
            GPXlat = Scanner.next();
            GPXlon = Scanner.next();
            GPXtime = Scanner.next();
            GPXname = Scanner.next();
            GPXdesc = Scanner.next();

            try {
                GPXTable.addRow(Column.AUTO_NUMBER, GPXlat, GPXlon, GPXtime, GPXname, GPXdesc);
            } catch (IOException T) {
                System.out.println("Error: " + T);
                System.out.println("Error is thrown while writing data to table");
            }
        }
    } catch (FileNotFoundException M) {
        System.out.println("Error: " + M);
    }
}

2 Answers2

2

In your code you have use Scanner.next() which returns String value. Most of the time your table column values not match with String that is why you getting Numberformatexception

Nisal Edu
  • 7,237
  • 4
  • 28
  • 34
  • I've set the datatype of that field to String so that it wouldn't be a problem. In the first line of the CSV it says "2016-11-28T11:57:38", which is properly written to the database. The second line reads "2016-11-28T11:36:34" and at that point the program throws the numberformatexception. – HaroldFinch Dec 17 '17 at 22:21
  • @HaroldFinch Try to set the field type to Object – Nisal Edu Dec 18 '17 at 03:51
  • I can choose from Binary, boolean, byte, complex_type, double, float, guid, int, long, memo, money, numeric, ole, short_date_time, text, unknown_od, unknown_11, unsupported_fixedlen and unsuppported_varlen... – HaroldFinch Dec 18 '17 at 08:57
  • @HaroldFinch what is the IDE you use ? – Nisal Edu Dec 18 '17 at 10:11
  • NetBeans 8.2, jackcess 2.1.8 & 2.1.9 – HaroldFinch Dec 18 '17 at 10:22
  • @HaroldFinch Class[] types = new Class [] {} can you find this on code – Nisal Edu Dec 18 '17 at 10:30
  • Should I be using that as datatype? – HaroldFinch Dec 18 '17 at 10:32
  • @HaroldFinch in side this you can see the data types map to your column – Nisal Edu Dec 18 '17 at 10:36
  • Ok I've found Object, where should I put that? I'm kinda new to Java... – HaroldFinch Dec 18 '17 at 10:39
  • Class[] types = new Class [] {java.lang.Object.class,java.lang.Object.class} – Nisal Edu Dec 18 '17 at 10:42
  • This is what I got now: .addColumn(new ColumnBuilder(" – HaroldFinch Dec 18 '17 at 10:47
  • Nothing, it just gives a ton of errors like 'class expected' ';' expected and so on. Is there a way I can send you my code? – HaroldFinch Dec 18 '17 at 10:51
  • try java.lang.String.class instead of java.lang.Object.class – Nisal Edu Dec 18 '17 at 10:57
  • When I add your code, it says that I try to run uncompilable sourcecode. This is the line of code: .addColumn(new ColumnBuilder(" – HaroldFinch Dec 18 '17 at 11:02
  • Okay I'm trying to write a CSV-file into an Access Database. The datatype of the field which causes the problem is TEXT. The first line of the CSV-file is this: 52.341435;6.383002;2016-11-28T11:57:38;Foto 7;Wal + greppel, with the delimiter being ";". So far so good. The next line of the CSV-file contains similar data, only different numbers. But the program crashes when it tries to write 2016-11-28T11:57:38 into the database as being TEXT, throwing the numberformatexception. So in the first line, it works fine, but it crashes in the second line, trying to write almost the same data. – HaroldFinch Dec 18 '17 at 11:14
2

Most probably you may try to convert a String type one to a numeric type. Please check data types of your data that you are trying to use.

WCM
  • 595
  • 3
  • 11
  • 27
  • But it works fine in the first line! In the second line all the '-' and ':' are in the same place, only the numbers are slightly different – HaroldFinch Dec 18 '17 at 11:34