Results 1 to 9 of 9
  1. #1
    fogus is offline Member
    Join Date
    Mar 2008
    Posts
    43
    Rep Power
    0

    Default [SOLVED] Help with getting open CSV to work

    Hey guys,

    I posed this on the sourceforge.net site forum, but I think my problem may not be specific to the open CSV code.

    I am having a problem with the code samples provided on: Java CSV - java code samples

    I assume that these should work with the following files in the same directory:

    CsvReader.java
    CsvWriter.java
    csv_test.java
    products.csv


    my csv_test.java file contains:
    --------------
    public class cvs_test {
    CsvReader reader = new CsvReader("products.csv");

    reader.readHeaders();

    while (reader.readRecord())
    {
    String productID = reader.get("ProductID");
    String productName = reader.get("ProductName");
    String supplierID = reader.get("SupplierID");
    String categoryID = reader.get("CategoryID");
    String quantityPerUnit = reader.get("QuantityPerUnit");
    String unitPrice = reader.get("UnitPrice");
    String unitsInStock = reader.get("UnitsInStock");
    String unitsOnOrder = reader.get("UnitsOnOrder");
    String reorderLevel = reader.get("ReorderLevel");
    String discontinued = reader.get("Discontinued");

    // perform program logic here

    }

    reader.close();
    }
    --------------





    The response I get is:

    --------------
    >javac csv_test.java
    csv_test.java:4: <identifier> expected
    reader.readHeaders();
    ^
    csv_test.java:6: illegal start of type
    while (reader.readRecord())
    ^
    csv_test.java:6: <identifier> expected
    while (reader.readRecord())
    ^
    csv_test.java:6: ';' expected
    while (reader.readRecord())
    ^
    csv_test.java:6: illegal start of type
    while (reader.readRecord())
    ^
    csv_test.java:6: <identifier> expected
    while (reader.readRecord())
    ^
    csv_test.java:7: ';' expected
    {
    ^
    csv_test.java:8: <identifier> expected
    String productID = reader.get("ProductID");
    ^
    csv_test.java:23: class, interface, or enum expected
    reader.close();
    ^
    csv_test.java:24: class, interface, or enum expected
    }
    ^
    10 errors
    >Exit code: 1
    --------------

    Any ideas on getting this code to work?

    I am using windows XP and the SciTE IDE.

    Eventually I will need to be writing to about 10 different CSV tables.
    ~fogus

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Unless you're cutting out critical code, you appear to be making method calls outside of a constructor or method.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    OK, I looked at your link and it appears that you blindly pasted the code into a class without putting it into a method. Try putting it into a method or constructor.

  4. #4
    fogus is offline Member
    Join Date
    Mar 2008
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Try putting it into a method or constructor.
    That's just the thing: you don't understand how much of a beginner I am.

    I've made constructors for my own code before, but you are going to have to point me to an example of what you mean. I don't understand everything in the open CSV code.

    One more thing, does anyone know how to take a new constructed data type, for example a "date" variable, and turn it into something that could be written to a CSV file?

    I will have variables called "start_date" and "end_date" and I will need to save them to a CSV eventually. I would rather not make methods like "to_string" and "from_string" if there is a way to go straight from a variable of type "date" to some kind of binary or string sequence that I coudl write to a file.

    Accordingly, is there a way to turn an entire object into a binary type that could be stored in a file without resorting to making my own "to_binary" method that does this operation?

    Thanks,
    ~fogus

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Just create a very simple constructor for your class and put this code in it. If you don't know how to do that, you need to back up a bit, forget this current project and review your text / tutorials a bit. The "trial-and-error" method doesn't work when trying to learn programming. You have to start from the beginning of a text or tutorial and work your way through it.
    Last edited by Fubarable; 03-20-2009 at 01:37 AM.

  6. #6
    fogus is offline Member
    Join Date
    Mar 2008
    Posts
    43
    Rep Power
    0

    Default

    OK, my understanding of a constructor is that you can make a new construct by doing something like:
    Java Code:
    Scanner contents = new Scanner(new File(products.csv));
    within a class. I have certainly done that before and it works.

    I have made my own constructors. For example, I have made a binary tree construct:
    Java Code:
    binarytree dictionary_tree = new binarytree(dictionary);
    which makes a construct from a class called binarytree (dictionary was an ArrayList<String>), another class that I wrote.

    NOW, I come to this new code, I have a line that runs:
    Java Code:
    CsvReader reader = new CsvReader("products.csv");
    and you tell me, "create a very simple constructor for your class and put this code in it".

    I thought I just constructed a CsvReader using a constructor. Am I wrong?

    How is the CsvReader construct different from every other construct that I have made?
    ~fogus

  7. #7
    fogus is offline Member
    Join Date
    Mar 2008
    Posts
    43
    Rep Power
    0

    Default

    OK, I forgot the
    Java Code:
    public static void main (String[] args){
    It still doesn't work though:
    Java Code:
    csv_test.java:3: cannot access CsvReader
    bad class file: .\CsvReader.java
    file does not contain class CsvReader
    Please remove or make sure it appears in the correct subdirectory of the classpath.
          CsvReader reader = new CsvReader("products.csv");
          ^
    1 error
    >Exit code: 1
    ~fogus

  8. #8
    fogus is offline Member
    Join Date
    Mar 2008
    Posts
    43
    Rep Power
    0

    Default Something is still very wrong.

    Even when I download the whole package of javacsv code from sourceforge.net ( https://sourceforge.net/project/show...ease_id=470782 ) and just run the "AllTests.java" that is given, I get about 100 errors:
    Java Code:
    AllTests.java:20: package org.junit does not exist
    import org.junit.After;
                    ^
    AllTests.java:21: package org.junit does not exist
    import org.junit.Assert;
                    ^
    AllTests.java:22: package org.junit does not exist
    import org.junit.Before;
                    ^
    AllTests.java:23: package org.junit does not exist
    import org.junit.Ignore;
                    ^
    AllTests.java:24: package org.junit does not exist
    import org.junit.Test;
                    ^
    AllTests.java:136: cannot find symbol
    symbol  : class Test
    location: class AllTests
    	@Test
    ...
    I tried to get my code to the right spot on my hard disk and checked out the "bad class file" error I was getting at this page: compile time error messages : Java Glossary but after checking and re-checking the location I still cannot see what I am doing wrong.

    I have these files in the same directory (as before):
    CsvReader.java
    CsvWriter.java
    csv_test.java
    products.csv

    My code is now:
    Java Code:
    import CsvReader;
    public class csv_test{
       public static void main (String[] args){
          CsvReader reader = new CsvReader("products.csv");
          reader.readHeaders();
          while (reader.readRecord()){
             String productID = reader.get("ProductID");
             System.out.println("productID = " + productID);
             String productName = reader.get("ProductName");
             System.out.println("productName = " + productName);
             String supplierID = reader.get("SupplierID");
             String categoryID = reader.get("CategoryID");
             String quantityPerUnit = reader.get("QuantityPerUnit");
             String unitPrice = reader.get("UnitPrice");
             String unitsInStock = reader.get("UnitsInStock");
             String unitsOnOrder = reader.get("UnitsOnOrder");
             String reorderLevel = reader.get("ReorderLevel");
             String discontinued = reader.get("Discontinued");
             // perform program logic here
          }
          reader.close();
       }
    }
    The error I get from that is:
    Java Code:
    >javac csv_test.java
    csv_test.java:1: '.' expected
    import CsvReader;
                    ^
    csv_test.java:1: ';' expected
    import CsvReader;
                     ^
    2 errors
    >Exit code: 1

    But if I run this code (note commented out line):
    Java Code:
    //~ import CsvReader;
    public class csv_test{
       public static void main (String[] args){
          CsvReader reader = new CsvReader("products.csv");
          reader.readHeaders();
          while (reader.readRecord()){
             String productID = reader.get("ProductID");
             System.out.println("productID = " + productID);
             String productName = reader.get("ProductName");
             System.out.println("productName = " + productName);
             String supplierID = reader.get("SupplierID");
             String categoryID = reader.get("CategoryID");
             String quantityPerUnit = reader.get("QuantityPerUnit");
             String unitPrice = reader.get("UnitPrice");
             String unitsInStock = reader.get("UnitsInStock");
             String unitsOnOrder = reader.get("UnitsOnOrder");
             String reorderLevel = reader.get("ReorderLevel");
             String discontinued = reader.get("Discontinued");
             // perform program logic here
          }
          reader.close();
       }
    }
    gives:
    Java Code:
    >javac csv_test.java
    csv_test.java:4: cannot access CsvReader
    bad class file: .\CsvReader.java
    file does not contain class CsvReader
    Please remove or make sure it appears in the correct subdirectory of the classpath.
          CsvReader reader = new CsvReader("products.csv");
          ^
    1 error
    >Exit code: 1
    Does anyone know how to fix this? Where should I have put those files? Do I have to use an import if my code files are all in the same directory? How can I specify a different directory to the CsvReader.java and CsvWriter.java?

    Cheers,
    ~fogus

  9. #9
    fogus is offline Member
    Join Date
    Mar 2008
    Posts
    43
    Rep Power
    0

    Default

    The code in the two files:
    Java Code:
    CsvReader.java
    CsvWriter.java
    had:
    Java Code:
    package com.csvreader;
    which confused my efforts.
    ~fogus

Similar Threads

  1. cipher.. Won't work
    By jgonzalez14 in forum New To Java
    Replies: 1
    Last Post: 11-18-2008, 02:22 PM
  2. Why doesn't my loop work?
    By d0nmin0 in forum Advanced Java
    Replies: 8
    Last Post: 05-26-2008, 07:56 PM
  3. Will this applet ever work?
    By willemjav in forum Java Applets
    Replies: 4
    Last Post: 04-20-2008, 06:40 PM
  4. Pass by ref. A work around?
    By diRisig in forum New To Java
    Replies: 0
    Last Post: 02-05-2008, 08:25 PM
  5. how would i get this to work...?
    By deeadeed in forum New To Java
    Replies: 6
    Last Post: 12-06-2007, 03:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •