may i know why i am unable to read next line in a .txt file when i upload it.
it happens to all the .txt file that i try to upload and then system.out.println() with.
in my text file it contains : cats,dogs,monkey ( each in one line ) .. but the value out put is:
[Monkey][Monkey, null][Monkey, null, null][Monkey, null, null, null][Monkey, null, null, null, null][Monkey, null, null, null, null, null][Monkey, null, null, null, null, null, null]
needs help on this.
thank you.
and wondering why i can read .txt file and not .doc. need advise on this as well.
import java.awt.List;
import java.io.*;
import java.util.ArrayList;
import javax.imageio.IIOException;
import javax.swing.JFileChooser;
public class searchforCapitalLetter {
public static void main(String[] args) throws IOException{
try {
// file chooser
JFileChooser chooser=new JFileChooser();
int returnVal = chooser.showOpenDialog(null);{
if(returnVal == JFileChooser.APPROVE_OPTION)
{ File f = chooser.getSelectedFile();}
FileInputStream fin = new FileInputStream(chooser.getSelectedFile());
DataInputStream din = new DataInputStream(fin);
BufferedReader br = new BufferedReader(new InputStreamReader(din));
ArrayList<String>list =new ArrayList<String> ();
if ((br.readLine()) != null) {
while (br.readLine() != " ") {
list.add(br.readLine());
System.out.print (list);
} br.close() ;
}//closes if statement
} // closes method dialog
} // closes try method
catch (FileNotFoundException e) {
e.printStackTrace();
} // closes catch method
} // closes method body
} // closes class method
Per the bufferedreader api:
Note that your code is making a few errors:
1) it is calling readline() once to check a return value and again to use the return value. The first call (in the while condition) removes the current value from the buffer, so your are dropping every other line.
2) you are using the wrong comparison to see if there’s data remaining. x != y, where both x and y are objects, checks for pointer equality – is the memroy location where x is allocated the same as the memory location where y is allocated. what you really want is to store the value in a variable.
For example: