This question already has an answer here:
BufferedReader skipping each second line
(1 answer)
Closed 8 years ago.
I'm trying to make a program which randomly get a line from a file, I have finished my code but I get weird results, for example if I get the line 0 it says "null" same with line 6, 7, 8, 9... but when I get number 5 I get last line (Pink), when I get number 4 I get line 7 (Brown), number 2 line 3 (Navy) and so on. My file "archivo.txt" has 10 lines like this:
Red
Orange
Blue
Navy
Lime
Green
Olive
Brown
Yellow
Pink
And this is my code:
public static String getLinea(){
File archivo = null;
FileReader fr = null;
BufferedReader br = null;
String linea = null;
int numeroRandom = (int)(Math.random() * 10);
System.out.println(numeroRandom);
try{
archivo = new File("C:\\archivo.txt");
fr = new FileReader (archivo);
br = new BufferedReader(fr);
for(int i = 0; i < numeroRandom; i++){
br.readLine();
linea = br.readLine();
}
}
catch(Exception e){
e.printStackTrace();
}finally{
try{
if(null != fr){
fr.close();
}
}catch(Exception e2){
e2.printStackTrace();
}
}
return linea;
}
public static void main(String[] args){
String linea;
linea = getLinea();
System.out.println(linea);
}
You are trying to readLine from the buffer two times in sequence.
for(int i = 0; i < numeroRandom; i++){
br.readLine();
linea = br.readLine();
}
instead just read once. Correct way :
for(int i = 0; i < numeroRandom; i++){
linea = br.readLine();
}
Also you should check if BufferedReader has any more data left before reading from the buffered reader. This can be done as follows:
while(((linea = br.readLine()) != null) && i < numeroRandom)
{
// do something
}
You are calling readLine() twice in your loop, effectively doubling the number of lines you are skipping.
I assume you want to read only the nth line, skipping the previous ones. Then you should update this piece of code:
br = new BufferedReader(fr);
for(int i = 0; i < numeroRandom; i++){
br.readLine();
linea = br.readLine();
}
Into this:
br = new BufferedReader(fr);
for(int i = 0; i < numeroRandom; i++){
br.readLine();
}
linea = br.readLine(); // move this line
You were getting null when numeroRandom was 0 because linea wasn't being updated. The other strange results happened because you were reading 2 lines in each cycle. There will be a point where getLine() will simply return null from not having more lines to read.
Related
So I'm trying to make a file reader to read from x line to y line but when i execute the program it reads all the lines of the file and not the lines that should have started and ended, For example if i'm looking an ID in the file it should print de ID, The name of the holder(the next line of the ID Line), and his/her address(Next line of the name Line), but instead of print just that it prints all the ID'S, Names and Addresses of everyone in the file.
System.out.println("Escriba el ID Del Cliente");
CL.setID(reader.next());
String line2;
int count = 0;
try {
BufferedReader input = new BufferedReader(new FileReader(file));
Scanner input2 = new Scanner(file);
PrintWriter output = new PrintWriter(new FileOutputStream(file, true));
LineNumberReader readers = new LineNumberReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
while((line2 = input.readLine()) != null)
{
if(line2.contains(CL.getID()))
{
while(((line2 = readers.readLine()) != null) && readers.getLineNumber() <= count + 3)
{
count++;
System.out.println(line2);
}
input.close();
input2.close();
output.close();
readers.close();
break;
}
}
}catch(IOException ex)
{
System.out.println("ERRORR!!!!!!");
}
I've modified your code because the problem was at the count++ which will eventually led to reading all the lines from your files, and at the line2 = readers.readLine() which will read from the first line of the file again ( the program works half correct because it reads only 3 lines and only if line2 contains your ID ). Now, to make your program work correctly, you need to either use the BufferedReader or the LineNumberReader.
public static void main(String[] args) {
System.out.println("Escriba el ID Del Cliente");
String line2;
File file = new File(yourpathhere);
int lineCount = 0;
try {
PrintWriter output = new PrintWriter(new FileOutputStream(file, true));
LineNumberReader readers = new LineNumberReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
while ((line2 = readers.readLine()) != null) {
lineCount++;
if (line2.contains(CL.getId())) {
while (line2 != null && readers.getLineNumber() <= lineCount + 3) {
System.out.println(line2);
line2 = readers.readLine();
}
output.close();
readers.close();
break;
}
}
} catch (IOException ex) {
System.out.println("ERRORR!!!!!!");
}
}
PS : pay attention for the getLineNumber() method because it increments the lines read until the moment you're calling it. It means that if we didn't had the lineCount and the ID we're trying to find was at the 6th line, the getLineNumber() value at the moment when line2.contains(CL.getId()) == true was 6, so the readers.getLineNumber() <= 3 will be FALSE and the program won't work correctly anymore. ( We need to keep track for the lines read until the moment we check for the id )
Assuming you have a file with 100 lines and you want to check and print out line 5 to 10 you could try this:
System.out.println("Escriba el ID Del Cliente");
CL.setID(reader.next());
String line;
int count = 0;
int xLine = 5;
int yLine = 10;
try (BufferedReader input = new BufferedReader(new FileReader(file)))
{
while((line = input.readLine()) != null)
{
if(count < xLine)
{
// skip all lines lower then start
continue;
}
else if(count >= xLine && count <= yLine && line.contains(CL.getID()))
{
// print line if line is between lines to read
// and if line contains ID
System.out.println(line);
}
else
{
// break if count is bigger then yLine
break;
}
count++;
}
}
catch(IOException ex)
{
System.out.println("ERRORR!!!!!!");
}
You read all lines till the BufferedReader reaches null. You check if the line contains your ID. Then you check if the count is between the linenumbers you want to check / print. Then you increment the count for the lines processed.
I simplified the try-catch-Block with a try-with-ressources Statement. As for now I don't see what your plans with output and scanner were, so I removed them.
Sorry for the noob question, but I feel like my code is correct but I can't see why the read won't go to the next line:
This is my code so far:
BufferedReader buffer = null;
try {
FileReader file = new FileReader(filename);
buffer = new BufferedReader(file);
String line = buffer.readLine();
String[] separations = line.split(", ");
System.out.println(separations[0]);
while(line!= null) {
this.times.add(separations[0]);
Double number = Double.parseDouble(separations[1]);
allNumbers.add(number);
line = buffer.readLine();
}
The issue was not that the buffer was only reading one line but I was tokenizing the line outside of the loop, so the same information got stored for x number of lines:
Solution:
while (line != null) {
String[] separations = line.split(", "); // this should be inside the loop
if (separations.length > 1) {
this.times.add(separations[0]);
Double number = Double.parseDouble(separations[1]);
allNumbers.add(number);
}
line = buffer.readLine();
}
I was trying to read adjMatrix graph from text it only read first line but I encounter this error. Is there any suggestion please ?
java.lang.NumberFormatException: For input string: "0 1 1 1"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
public static void main(String[] args)throws IOException {
int row_num = 0;
//InputGraph gr = new InputGraph("input.txt");
// Cells w= gr.copyMatrix();
// w.printAdjMatrix();
try {
String fileName = ("input.txt");
File file = new File(fileName);
BufferedReader br = new BufferedReader(new FileReader(file));
//get number of vertices from first line
String line = br.readLine();
System.out.println("hi");
//InputGraph.size = Integer.parseInt(line.trim());
InputGraph.size=Integer.parseInt(line);
InputGraph.adMatrix = new int[InputGraph.size][InputGraph.size];
br.readLine();
for (int i = 0; i < InputGraph.size; i++) {
line = br.readLine();
String[] strArry = line.split(" ");
for (int j = 0; j < InputGraph.size; j++) {
InputGraph.adMatrix[i][j] = Integer.parseInt(strArry[j]);
if (Integer.parseInt(strArry[j]) == 1)
row_num++;
}
}
} catch (Exception e4) {
e4.printStackTrace();
}
input text file
0 1 1 1
0 0 0 0
1 1 0 1
0 1 0 0
0 1 1 1 : This is one line. You are trying to parse it with Integer but this line is not completely integer as it contains spaces also which are characters.
To get the size, you can do something like this:
String line = br.readLine();
InputGraph.size = (line.size()+1)/2;
Because if a line has x integers it will have x-1 spaces(considering that there is only one space b/w two integers) So, 2x -1 = size of line.
You don't have a line specifying the number of vertices in the graph in the input file. This is why
InputGraph.size=Integer.parseInt(line);
does not work. line contains "0 1 1 1" here, which is more than a single integer.
You need to find the size from the number of ints in the first line instead. Furthermore I do recommend closing the input file after reading it, best using try-with-resources:
try (FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr)) {
String line = br.readLine();
String[] elements = line.trim().split(" +");
InputGraph.size=elements.length;
InputGraph.adMatrix = new int[InputGraph.size][InputGraph.size];
for (int i = 0; i < InputGraph.size; i++, line = br.readLine()) {
String[] strArry = line.trim().split(" +");
for (int j = 0; j < InputGraph.size; j++) {
InputGraph.adMatrix[i][j] = Integer.parseInt(strArry[j]);
if (InputGraph.adMatrix[i][j] == 1)
row_num++;
}
}
} catch (Exception e4) {
e4.printStackTrace();
}
I'm trying to convert a CSV file into a 2D array however I'm having problems. The code seems to only print out first name in the CSV file 10 times and then gives me an out of bounds exception. E.g if Player name was Rob, it will just print out Rob over and over again.
If any more clarification needed just ask
try{
int col = 0, row = 0;
System.out.println("Reading " + fileName + " ...");
fr = new FileReader(fileName);
bf = new BufferedReader(fr);
String line = bf.readLine();
while (line != null) {
StringTokenizer st = new StringTokenizer(line,",");
while(st.hasMoreTokens()){
System.out.println(st.nextToken());
data[row][col] = st.nextToken();
col++;
}
col = 0;
row++;
}
}catch(IOException e){
System.err.println("Cannot find: "+fileName);
}
I think your code reads just one line. Continue the while loop using the following code snippet -
String line = bf.readLine();
while ((line = br.readLine()) != null) {
//place your code here
}
In the above code BufferedReader - br is now ready to read the next line and it continues until the br.readline() returns a null.
Hope it will help.
Thanks a lot.
I'm trying to make a method that a random line from lol.txt (which has 113 lines) would be chosen and sent out as message box. How it should work:
Generates random number from 0 till 112
a for loop should go over the random number of lines
output the randomly generated line as a message box
In my case step 2 doesn't work so I was hoping that someone could suggest on that. Here's the code:
public void close(){
try{
Random random = new Random();
int randomInt = random.nextInt(112);
FileReader fr = new FileReader("lol.txt");
BufferedReader reader = new BufferedReader(fr);
String line = reader.readLine();
Scanner scan = null;
for (int i = 0; i < randomInt + 1; i++) {
scan = new Scanner(line);
line = scan.nextLine();
}
JOptionPane.showMessageDialog(null,line);
}catch (IOException e){
JOptionPane.showMessageDialog(null,e.getMessage()+" for lol.txt","File Error",JOptionPane.ERROR_MESSAGE);
}
}
If you want to send me the solution with an array list that's fine but I would really like it to be how I planned it initially.
It's best to use a list for this purpose, as well as make the random size dynamic to adjust to the size of your file. In case you wanted to add more lines without having to change code.
BufferedReader reader = new BufferedReader(new FileReader("lol.txt"));
String line = reader.readLine();
List<String> lines = new ArrayList<String>();
while (line != null) {
lines.add(line);
line = reader.readLine();
}
Random r = new Random();
String randomLine = lines.get(r.nextInt(lines.size()));
JOptionPane.showMessageDialog(null,randomLine);
You only read the first line, and that's why you only get the first line. Try this..
String line = reader.readLine();
for (int i = 0; i < randomInt + 1; i++) {
line = reader.readLine();
}
What you are doing is reading a line from file, using that line to create a new Scanner with every iteration of loop and then read it back into line