aspose file tools*
The moose likes Java in General and the fly likes vector elements asssignment to variables for storing to db Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "vector elements asssignment to variables for storing to db " Watch "vector elements asssignment to variables for storing to db " New topic
Author

vector elements asssignment to variables for storing to db

Jsandy Sandyj
Greenhorn

Joined: Oct 15, 2003
Posts: 5
Hi,
With this application I can display all the elements of the vector from a file.
how do I assighn each value to each variable?
for eg., String sFirmName = (String)v.elementAt(0);
String sNumber = (String)v.elementAt(1);
asigns only first line elements to the respective variables and displays, but how do I display next line elements also with the respective variable assignment???

How do I put them in to database elements.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;
import java.io.EOFException;

import java.util.StringTokenizer;
import java.util.Vector;
import java.util.Iterator;
public class TextRead {
private BufferedReader br;
static Vector v = new Vector();
String sFirmaName;
// constructor
public TextRead(String filepath)
{
try
{
br = new BufferedReader(new FileReader(filepath));
} catch (FileNotFoundException ex)
{
System.out.println("File "+ filepath + " not found");
System.exit(1);
}
}

public TextRead(File file)
{
try
{
br = new BufferedReader(new FileReader(file.getPath()));
}
catch (FileNotFoundException ex)
{
System.out.println("File "+ file.getPath() + " not found");
System.exit(1);
}
}


public String readLine()
{
String line = null;
try {
line = br.readLine();
} catch (EOFException ex){
System.out.println("End of file reached");
line = null;
} catch (IOException ex) {
System.out.println("Error reading next line");
line = null;
}

return line;
}

public void close()
{
try {
br.close();
} catch (IOException ex) {
System.out.println("Error closing file");

System.exit(1);
}
}


public static void main(String[] args)
{

TextRead t1 = new TextRead("c:\\jj/dump1.txt");

String str = "";
int lineCount = 0;
// TO KEEP TRACK OF LINES READ
while (str != null)
{
lineCount++;
str = t1.readLine();

if (str != null)
{
processLineTokens(str);
String sFirmName = (String)v.elementAt(0);
System.out.println("FIRMNAME = "+sFirmName);
}

}


Iterator aIterator = v.iterator();
while (aIterator.hasNext())
{
System.out.println("------------------------------------");
System.out.println(aIterator.next());
}

System.out.println("Lines read = "+lineCount);

t1.close();
}


private static void processLineTokens(String str)
{


StringTokenizer strTok = new StringTokenizer(str,";");
while (strTok.hasMoreTokens())
{
v.add(strTok.nextToken());

}
}

thanks,
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
Your post was kind of unclear. You're wanting to insert them into a database? Do you know any SQL?
Are you saying that the Vector gets filled with only the first line's elements? Do you know if your while loop is going through the whole file?
Jsandy Sandyj
Greenhorn

Joined: Oct 15, 2003
Posts: 5
Originally posted by S. C. Huey:
Your post was kind of unclear. You're wanting to insert them into a database? Do you know any SQL?
Are you saying that the Vector gets filled with only the first line's elements? Do you know if your while loop is going through the whole file?

Hi,
I want to assign all my vector elements from each line read to the corresponding variables declared then display on the screen.
for eg.,
Firmname = abc
Fnumber = 20
---------------
Firmname = bcd
Fnumber = 30
--------------
Firmname = cde
Fnumber = 30
--------------
Now I am able to display all my vector elements like this ..
abc
20
bcd
30
cde
40
Thanks,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: vector elements asssignment to variables for storing to db