Results 1 to 6 of 6
  1. #1
    fritz1474 is offline Member
    Join Date
    Sep 2008
    Posts
    8
    Rep Power
    0

    Default reading csv file help

    I need some help and thought I would turn to the experts. I have an assignment that requires me to read a 3 line csv file containin 5 values (1 text, 4 numbers) and calculate a volume (1000x) off of one of the numbers. I was able to get the values read from the file, but cannot get the calculation to work. I am identifying the value as a String but it will not accept it as an integer.

    Here is the code that is causing me greif:

    //Test csv file read program

    import java.util.*;
    import javax.swing.JOptionPane;
    import java.io.*;
    import java.text.DecimalFormat;

    public class ReadCSV
    {
    public static void main(String[] args) throws IOException
    {
    DecimalFormat twoPlaces = new DecimalFormat("0.00");

    Scanner input = new Scanner (System.in);
    int i = 0;
    String[] text = new String[3];
    String[] def = new String[3];
    String[] op = new String[3];
    String[] hp = new String[3];
    String[] lp = new String[3];
    String[] cp = new String[3];
    String[] dv = new String[3];
    File myJavaCode = new File("C:\\java\\data.txt");
    BufferedReader fileInput = new BufferedReader(new FileReader(myJavaCode));
    String s = fileInput.readLine();


    StringTokenizer sT = new StringTokenizer(s,",");
    text[i] = new String(sT.nextToken().trim().toLowerCase());
    def[i] = new String(sT.nextToken().trim());
    op[i] = new String(sT.nextToken().trim());
    hp[i] = new String(sT.nextToken().trim());
    lp[i] = new String(sT.nextToken().trim());
    cp[i] = new String(sT.nextToken().trim());
    dv[i] = new String(sT.nextToken().trim());

    i++;

    String [] sA = fileInput.readLine().split(",");

    text[i] = new String(sA[0].trim().toLowerCase());
    def[i] = new String(sA[1].trim());
    op[i] = new String(sA[2].trim());
    hp[i] = new String(sA[3].trim());
    lp[i] = new String(sA[4].trim());
    cp[i] = new String(sA[5].trim());
    dv[i] = new String(sA[6].trim());

    i++;

    String [] rA = fileInput.readLine().split(",");

    text[i] = new String(rA[0].trim().toLowerCase());
    def[i] = new String(rA[1].trim());
    op[i] = new String(rA[2].trim());
    hp[i] = new String(rA[3].trim());
    lp[i] = new String(rA[4].trim());
    cp[i] = new String(rA[5].trim());
    dv[i] = new String(rA[6].trim());

    i++;



    s = fileInput.readLine();


    System.out.print("Enter a number from 1 to " + text.length + " ");
    i = Integer.parseInt(input.nextLine()) - 1;
    System.out.println( "Enter text " + def[i] + " " + op[i] + " " + hp[i] + " " + lp[i] + " " + cp[i] + " " + dv[i]);

    JOptionPane.showMessageDialog(null, "Results \n" + text[i] +"\n" + def[i] + "\n" + hp[i]); //, JOptionPane.TOP_ALIGNMENT, JOptionPane.LEFT_ALIGNMENT);

    System.exit(0);
    }
    }

    And the csv file data

    ADTN,213,3a1.35,3186,3132,315,10443
    BDTN,0213,3b1.35,3186,3132,315,10443
    CDTN,213,3c1.35,3186,3132,315,10443

    Thanks for your help

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    it will not accept it as an integer.
    Do you get error messages?
    Please copy and paste them here.

    Use Systemt.out.println() statements to display the contents of variables if you are not sure what they are.

  3. #3
    fritz1474 is offline Member
    Join Date
    Sep 2008
    Posts
    8
    Rep Power
    0

    Default

    Here is the error message:
    E:\>javac ReadCSV3.java
    ReadCSV3.java:35: '[' expected
    dv[i] = new int(sT.nextToken().trim());
    ^
    ReadCSV3.java:35: illegal start of expression
    dv[i] = new int(sT.nextToken().trim());
    ^
    ReadCSV3.java:47: '[' expected
    dv[i] = new int(sA[6].trim());
    ^
    ReadCSV3.java:47: illegal start of expression
    dv[i] = new int(sA[6].trim());
    ^
    ReadCSV3.java:59: '[' expected
    dv[i] = new int(rA[6].trim());
    ^
    ReadCSV3.java:59: illegal start of expression
    dv[i] = new int(rA[6].trim());
    ^
    6 errors


    Also, here is the code that I modified to get these errors:

    //Test csv file read program

    import java.util.*;
    import javax.swing.JOptionPane;
    import java.io.*;
    import java.text.DecimalFormat;

    public class ReadCSV3
    {
    public static void main(String[] args) throws IOException
    {
    DecimalFormat twoPlaces = new DecimalFormat("0.00");

    Scanner input = new Scanner (System.in);
    int i = 0;
    String[] text = new String[3];
    String[] def = new String[3];
    String[] op = new String[3];
    String[] hp = new String[3];
    String[] lp = new String[3];
    String[] cp = new String[3];
    int[] dv = new int[3];
    File myJavaCode = new File("C:\\java\\data.txt");
    BufferedReader fileInput = new BufferedReader(new FileReader(myJavaCode));
    String s = fileInput.readLine();


    StringTokenizer sT = new StringTokenizer(s,",");
    text[i] = new String(sT.nextToken().trim().toLowerCase());
    def[i] = new String(sT.nextToken().trim());
    op[i] = new String(sT.nextToken().trim());
    hp[i] = new String(sT.nextToken().trim());
    lp[i] = new String(sT.nextToken().trim());
    cp[i] = new String(sT.nextToken().trim());
    dv[i] = new int(sT.nextToken().trim());

    i++;

    String [] sA = fileInput.readLine().split(",");

    text[i] = new String(sA[0].trim().toLowerCase());
    def[i] = new String(sA[1].trim());
    op[i] = new String(sA[2].trim());
    hp[i] = new String(sA[3].trim());
    lp[i] = new String(sA[4].trim());
    cp[i] = new String(sA[5].trim());
    dv[i] = new int(sA[6].trim());

    i++;

    String [] rA = fileInput.readLine().split(",");

    text[i] = new String(rA[0].trim().toLowerCase());
    def[i] = new String(rA[1].trim());
    op[i] = new String(rA[2].trim());
    hp[i] = new String(rA[3].trim());
    lp[i] = new String(rA[4].trim());
    cp[i] = new String(rA[5].trim());
    dv[i] = new int(rA[6].trim());

    i++;



    s = fileInput.readLine();


    System.out.print("Enter a number from 1 to " + text.length + " ");
    i = Integer.parseInt(input.nextLine()) - 1;
    System.out.println( "Enter text " + def[i] + " " + op[i] + " " + hp[i] + " " + lp[i] + " " + cp[i] + " " + dv[i]);

    JOptionPane.showMessageDialog(null, "Results \n" + text[i] +"\n" + def[i] + "\n" + hp[i]); //, JOptionPane.TOP_ALIGNMENT, JOptionPane.LEFT_ALIGNMENT);

    System.exit(0);
    }
    }


    Thank you very much

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    ReadCSV3.java:35: '[' expected
    All the error messages given the line number where the error is.
    The above says its on line 35. When you posted the message the location of the ^ is lost. What was it above it?

    = new int(
    It is not possible to create a new int this way. new is used with classes not with primitives. The compiler got confused. It was looking for = new int[size] to create a new array of int.
    Use Integer.parseInt() to convert a string to an int

  5. #5
    fritz1474 is offline Member
    Join Date
    Sep 2008
    Posts
    8
    Rep Power
    0

    Default

    Thank you very much - that did it. I have used that before, but did not think of it for this...

  6. #6
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    And, of course, the same for 'new String(String)'. You don't need to use that, since it already is a String. Next to that... I don't think it'll work.
    I die a little on the inside...
    Every time I get shot.

Similar Threads

  1. Right use of file reading ?
    By jurka in forum New To Java
    Replies: 3
    Last Post: 08-27-2008, 08:16 PM
  2. Reading a file
    By mew in forum New To Java
    Replies: 2
    Last Post: 12-30-2007, 12:23 PM
  3. Reading a file for use
    By peachyco in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 03:49 AM
  4. Reading Data from a file
    By ramachandran in forum New To Java
    Replies: 2
    Last Post: 10-24-2007, 07:22 AM
  5. Reading from a file
    By leebee in forum New To Java
    Replies: 1
    Last Post: 07-23-2007, 12:02 PM

Posting Permissions

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