Results 1 to 6 of 6
Thread: reading csv file help
- 09-04-2008, 07:24 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 8
- Rep Power
- 0
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
- 09-04-2008, 07:40 PM #2it will not accept it as an integer.
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.
- 09-04-2008, 07:49 PM #3
Member
- Join Date
- Sep 2008
- Posts
- 8
- Rep Power
- 0
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
- 09-04-2008, 09:02 PM #4ReadCSV3.java:35: '[' expected
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(
Use Integer.parseInt() to convert a string to an int
- 09-04-2008, 09:04 PM #5
Member
- Join Date
- Sep 2008
- Posts
- 8
- Rep Power
- 0
Thank you very much - that did it. I have used that before, but did not think of it for this...
- 09-04-2008, 09:41 PM #6
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 12
Similar Threads
-
Right use of file reading ?
By jurka in forum New To JavaReplies: 3Last Post: 08-27-2008, 09:16 PM -
Reading a file
By mew in forum New To JavaReplies: 2Last Post: 12-30-2007, 01:23 PM -
Reading a file for use
By peachyco in forum New To JavaReplies: 2Last Post: 11-27-2007, 04:49 AM -
Reading Data from a file
By ramachandran in forum New To JavaReplies: 2Last Post: 10-24-2007, 08:22 AM -
Reading from a file
By leebee in forum New To JavaReplies: 1Last Post: 07-23-2007, 01:02 PM
Bookmarks