using bufferedreader to insert into a vector
I am trying to read from a text file in an incremental fashion back into a form. I have tried it only reading the first record off the textfile and it works great; however, I run into problems when I try to read the entire file into a Vector or Array. Code is as follows:
Vector vLine =new vLine(10,1);
String tCardFile =new String("timecard.txt");
File file =new File(tCardFile);
BufferedReader br =new BufferedReader(new FileReader(file));
String lineRead =null;
lineRead =new String();
while((lineRead = br.readLine()) !=null){ vLine.addElement(lineRead);}
When I compile, I get this funky error that reads:
Note: D:\javafiles\TimeCard.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:uncheckedfor details.
Now, I have taken the vector back out and tried using an array with 100 elements and I no longer get the above error but I run into two issues:
First...the "timecard.txt" fill is meant to keep growing so I need a dynamic array like the vector class
Second...I get a null value as being entered in either the array or the vector.
Any thoughts ?
The compiler option -Xlint:unchecked enables "unchecked" warnings, the option -Xlint:-unchecked disables all unchecked warnings.
"unchecked" warnings are by default disabled. If you compile a program with no particular compiler options then the compiler will not report any "unchecked" warnings. If the compiler finds source code for which it would want to report an "unchecked" warning it only gives a general hint. You will find the following note at the end of the list of all other errors and warnings:
Note: util/Wrapper.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
If you want to see the "unchecked" warnings you must start the compiler with the -Xlint:unchecked option.
Example (of globally enabling unchecked warnings):
javac -Xlint:unchecked util/Wrapper.java
The option -Xlint:unchecked en ables the "unchecked" warnings. The "unchecked" warnings are also enabled when you use the -Xlint:all option.
The option -Xlint:-unchecked disables the "unchecked" warnings. This is useful to suppress all "unchecked" warnings, while other types of warnings remain enabled.
Example (of globally disabling unchecked warnings):
javac -g -source 1.5 -Xlint:all -Xlint:-unchecked util/Wrapper.java
In this example, using -Xlint:all all warnings (such as "unchecked", "deprecated", "fallthrough", etc.) are enabled and subsequently the "unchecked" warnings are disabled using -Xlint:-unchecked . As a result all warnings except "unchecked" warnings will be reported.
just curious what happens if you use an ArrayList and why are you preferring vector over an arraylist
don't forget to catch the exception
List lines = new ArrayList();
Bufferedreader in = null;
try{
in = new BufferedReader(new FileReader("timecard.txt"));
String line;
while ((line = in.readLine()) != null)
lines.add(line);
// do something with the list
}
catch (IOException ioe){
e.printStackTrace();// don't have to catch it if yuo wanna throws it instead
}
finally{
if (in != null){
try{ in.close(); }
catch (Exception e){};
}
}
> just curious what happens if you use an ArrayList and
> why are you preferring vector over an arraylist
I realized that after I posted, I am attempting the use of ArrayList now...
On another note, I use JGrasp ( as my editor ) and not really sure how to implement the "-Xlint:unchecked" with it.... do any of you happen to use JGrasp?
you going to get the same compiler warning about "unchecked or unsafe operations"...
the reason is because the compiler is not able to guarantee the type safety of your raw ( non-generified ) vector/arraylist operations...
i.e.
Vector v = new Vector(); // raw vector
String s = "a string";
Integer i = new Integer(1);
v.add(s); // able to add arbitrary types like a String
v.add(i); // or an Integer
String k = (String) v.element(1); // class cast exception
to fix this, you use generics... then the compiler will ensure that only the data type that the vector is declared to accept via generics will be allowed
Vector<String> v = new Vector<String>(); // raw vector
String s = "a string";
Integer i = new Integer(1);
v.add(s); // works because v is a Vector of Strings
v.add(i); // will not work because a Vector of String cannot accept Integer objects
String k = v.element(1); // no cast necessary because the Vector on handles Strings
it's the same with an ArrayList...
[code]ArrayList<String> al = new ArrayList<String>();
have fun...
- MaxxDmg...
- ' He who never sleeps... '
> you going to get the same compiler warning about
> "unchecked or unsafe operations"...
>
> the reason is because the compiler is not able to
> guarantee the type safety of your raw (
> non-generified ) vector/arraylist operations...
>
> i.e.
Vector v = new Vector(); // raw vector
that seemed to take care of the warning messag I was getting...
and...
For anyone that use JGrasp, you have to change the compiler settings for Workspace and file. It seems to create settings file for different compiler settings. Editing it and added the "-Xlint:unchecked" statement takes care of error....
Thanks again...
I made the necessary change and it doesnt error out.
I added println statement at the end to test the lines being added and I am only getting one line entered. I know that my file has more than one line. Here's the code.
private String tCardFile = new String("timecard.txt");
private ArrayList<String> lines = new ArrayList<String>();
....
//Opens the TimeCardFile and reads the first line
public void openTimeCardForReading(){
//List lines = new ArrayList();
BufferedReader in = null;
try{
in = new BufferedReader(new FileReader(tCardFile));
String line;
while ((line = in.readLine()) != null)
lines.add(line);
i+=1;
}
catch (IOException ioe){ ioe.printStackTrace(); }
finally{if (in != null){try { in.close();}catch (Exception e){};}
}
System.out.println(i);
}//End of openTimeCardForReading() Method
any ideas?