Can anyone tell me why my code never reads the 2nd line of my file? if my 2nd line in the file (for example .txt file) start at a new line and indent that line, it will not read it.But if it is in a new line and it isn't indented , it will read. also it reads 3rd line fine. Is it something with the while loop ?
Scanner keyboard = new Scanner (System.in);
System.out.println("Input the file name");
String fileName = keyboard.nextLine();
File input = new File (fileName);
BufferedReader reader = new BufferedReader(new FileReader(input));
String content = reader.readLine();
content.replaceAll("\\s+","");
while (reader.readLine() != null) {
content = content + reader.readLine();
}
System.out.println(content);
See my comments in the code below.
String content = reader.readLine(); //here you read a line
content.replaceAll("\\s+","");
while (reader.readLine() != null) //here you read a line (once per loop iteration)
{
content = content + reader.readLine(); //here you read a line (once per loop iteration)
}
As you can see, you are reading the second line in the beginning of your while loop, and you are checking if it is equal to null before moving on. However, you do nothing with that value, and it is lost. A better solution would look like this:
String content = ""
String input = reader.readLine();
while (input != null)
{
content = content + input;
input = reader.readLine();
}
This avoids the problem of reading and then throwing away every other line by storing the line in a variable and checking the variable for null instead.
Each time you call readLine() it reads the next line. The statement
while (reader.readLine() != null)
reads a line but does not do anything with it. What you want is
String line;
StringBuilder buf;
while ( (line = reader.readLine()) != null)
{
buf.append(line);
}
content = buf.toString();
Using a StringBuilder is much better as it avoids reallocating and copying the entire string each time you append.
Related
I am taking data from a text file and putting it into an ArrayList. However the first line of the text file is not being printed out.
public static void secondMain() {
BufferedReader reader;
var lines = new ArrayList<String>();
var rooms = new ArrayList<Room>();
try {
reader = new BufferedReader(new FileReader("rooms.txt"));
String line = reader.readLine();
while (line != null) {
line = reader.readLine();
lines.add(line);
}
reader.close();
for (int i = 0; i < lines.size() - 1; i++) {
String[] words = lines.get(i).split(" ");
var room = new Room();
room.RoomNumber = Integer.parseInt(words[0]);
room.Type = (words[1]);
room.Price = Double.parseDouble(words[2]);
room.Bool1 = Boolean.parseBoolean(words[3]);
room.Bool2 = Boolean.parseBoolean(words[4]);
room.Availability = (words[5]);
rooms.add(room);
}
for(int i = 0; i < rooms.size(); i++) {
System.out.println(rooms.get(i).RoomNumber);
System.out.println(rooms.get(i).Type);
System.out.println(rooms.get(i).Price);
System.out.println(rooms.get(i).Bool1);
System.out.println(rooms.get(i).Bool2);
System.out.println(rooms.get(i).Availability);
}
Apologies for the Image, it was the only way i could figure out how to show the formatting of the text file.
The current output is displaying room number 102 as the first room which obviously isn't correct.
If anyone could also help me figure out how to format my console output the same way as the text file that'd also be great. currently it is displaying each individual string/int etc. on a different line.
Thanks.
If you need any more information please just ask!
This has nothing to do with the ArrayList. You can reproduce the problem by replacing the lines.add(line) call with System.out.println(line) and you'll see that the first line is missing from the output. Look at your first call to readLine(), before the while loop. You test that the value is non-null... and that's all you do with it (comments mine):
String line = reader.readLine(); // Read the value...
while (line != null) { // Test for it being non-null
line = reader.readLine(); // Then ignore the value you've just tested,
// by reading the next line.
lines.add(line);
}
You then call readLine() again. Note that your list will always end with a null value (unless it's the first line read) because your loop effectively says "while the last entry I added to the list isn't null". The simplest fix is to swap the order of the statements within your loop:
String line = reader.readLine();
while (line != null) {
lines.add(line);
line = reader.readLine();
}
Now you're adding a line immediately after checking whether it's non-null, before reading the next line.
The problem is here:
String line = reader.readLine(); // you read the first line
while (line != null) {
line = reader.readLine(); // read second line
lines.add(line); // add second line
}
You read the first line, check if it's not null, and then read the second line right away, before you add it to the list. All you need to do is switch the order.
String line = reader.readLine();
while (line != null) {
lines.add(line);
line = reader.readLine();
}
I'm writing some code in Java to scan through a .txt file. Each line in the file contains two tokens, and there's only about 7 rows of data.
My aim is to use the while loop to scan through each line, store the tokens in a HashMap, and when the scanner reaches the end of the document I want the while loop to terminate. Here is a snippet of the code I am using:
while ((line = b.readLine()) != null) {
s = new Scanner(line);
String fileName = s.next();
String source = s.next();
sourceMap.put(fileName, source);
}
This is something I have done many times before, however there are now some issues with the while loop terminating. The while loop continues to operate even after the 'final' line has been read. When the scanners attempt to read tokens, a java.util.NoSuchElementException is returned.
From using the debugger I can see that the line immediately after the 'final' line of code is being read as: ""
I believe this is an empty string, however I am unsure if this is different to a line being null. I attempted to add code for the condition while (line != "") however this did not result in the while loop being terminated.
Has anyone experienced similar issues before, and if so how did you overcome them?
EDIT: This has now been resolved. There was a line with an empty string after the 'final' line in the .txt file. The corrected code is as follows:
while ((line != null) && (!line.isEmpty())) {
s = new Scanner(line);
String fileName = s.next();
String source = s.next();
sourceMap.put(fileName, source);
line = b.readLine();
}
can anybody please explain why there have to be exactly 3 empty rows in between sections of text (marked with number) in the txt file my code is reading from ?
I have a code that is reading lines from txt file. The structure of the file is as follows:
1
r
line of text
line of text
line of text
line of text
line of text
2
a
line of text
line of text
line of text
line of text
line of text
A number is a mark that i m using to identify which section to read. When section is identified my code then reads consecutive lines of text and returned values are assigned to String variables.
All works perfectly only and only if there is exactly 3 empty rows separating each section of text is source txt file. I d like to understand why is it so ? Thank you very much
Here's the snippet of my code:
inpStream = getClass().getResourceAsStream("/resources/myFile.txt");
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inpStream,"UTF-16BE"))){
do{
num = Integer.toString(a);
line = reader.readLine();
if(line.equals(num)){
who = reader.readLine();
what = reader.readLine();
sat = reader.readLine();
pow = reader.readLine();
satNo = reader.readLine();
cash = reader.readLine();
break;
}
} while(!line.equals(num) && (line = reader.readLine()) != null );
}
catch(IOException e){}
It is the number and order of calls to readLine().
Your code reads and checks every other line for a match. The three blank lines are not important, it's just that it's an even number since the first index number. It will also work with one, five etc.
The double read occurs once at the beginning of each iteration, and once in the condition.
line = reader.readLine();
and
while(... && (line = reader.readLine()) != null
Thereby skipping two lines each iteration.
I would recommend either moving the first read out of the loop, or change to a while-loop:
String line;
while ((line = reader.readLine() != null) {
if (line.equals(num){
...
break;
}
}
This reads each line, checking against num for each line.
Defining num can also be moved out of the loop, as it does not change inside the loop. Giving a full example of:
InputStream inpStream = getClass().getResourceAsStream("/resources/myFile.txt");
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inpStream, "UTF-8"))) {
num = Integer.toString(a);
String line;
while ((line = reader.readLine()) != null) {
if (line.equals(num)) {
who = reader.readLine();
what = reader.readLine();
sat = reader.readLine();
pow = reader.readLine();
satNo = reader.readLine();
cash = reader.readLine();
break;
}
}
} catch (IOException e) {
}
Note: This example uses UTF-8.
You have to read all the lines whether you need them or not. You can't pretend you read lines, that you didn't. You can skip the lines after the ones you needed.
Lets break up your code line by line and analyze.
Line 1.
You call the getClass() function on an InputStream object which in turn you use to call the getStreamResource() function to return a "stream" to your file data.
Now having access to this stream you enclose your BufferReader in a try/catch statement.
Line2.
next you create a BufferReader with Reader Parameter inpstream and file encoding type UFT-16.
Line 3-13
You haven't shown me where you have declared 'a' so I can't be sure, but you are converting an integer 'a' to a string called num. (This is important because it is the terminating factor of your do statement.) I am assuming a =1 in the first iteration.
Now its all about understanding File Offsets.
when you say
line=reader.readLine();
this returns a String containing the current line characters and sets the file offset to the next line.
your file offset is at 'r' (for 1st iteration).
The same technique for reading who,what,sat,pow,satNo & cash.
Now your while statement checks if both conditions are true or not. i.e line != num (which it is since you haven't redefined num anywhere after reading who,what,sat,pow,satNo & cash.) And also line=line.readLine() != null which is also true.
However here is your first mistake calling line=reader.readLine() the way you did this increments the file offset one more line. Thus in two iterations two lines are read in addition to what you expect. This messes up the order in which you envisioned your variables to be found i.e who,what,sat,pow,satNo & cash etc in your do statement.
Therefore in short you are skipping every other line after your first iteration.
You need to get rid of one of your readLine calls.
The two double counting are:
line = inpstream.readLine() and
line = reader.readLine()) != null
How can i read single line from a text file in java.
and what is the criteria of knowing that line is completed.
secondly
i read file and then for Read Line function and converting it into string will skip a lot of data? what should be the problem?
Here is my code
String data = new String();
while(infile.readLine() != null) {
data = infile.readLine();
System.out.println(data);
}
Change your code as follows
while((data = infile.readLine()) != null) { // read and store only line
System.out.println(data);
}
In your current code
while(infile.readLine() != null) { // here you are reading one and only line
data = infile.readLine(); // there is no more line to read
System.out.println(data);
}
You are reading an extra line because the first readLine() as the while condition reads a line but it is used at all. The second readLine() inside the while loop read the second line which you're assigning to data and printing.
Therefore you need to assign the line read in the while condition to data and print it, as that is the first line.
while((data = infile.readLine()) != null) { // reads the first line
// data = infile.readLine(); // Not needed, as it reads the second line
System.out.println(data); // print the first line
}
Also, since you just need to read the first line, you don't need the while at all. A simple if would do.
if((data = infile.readLine()) != null) { // reads the first line
System.out.println(data); // print the first line
}
With the BufferedReader and the code you posted in the comments, your main should now look like this.
public static void main(String[] args) {
try {
FileInputStream fstream = new FileInputStream(args[0]);
BufferedReader infile = new BufferedReader(new InputStreamReader(
fstream));
String data = new String();
while ((data = infile.readLine()) != null) { // use if for reading just 1 line
System.out.println(data);
}
} catch (IOException e) {
// Error
}
}
First thing : readLine() returns String value only so it is not converting to String.
Second thing : In your while loop, you read firstline and check whether the content of first line is null or not. But when data = infile.readLine(); executes, it will fetch second line from file and print it to Console.
Change your while loop to this :
while((data = infile.readLine()) != null){
System.out.println(data);
}
If you use toString() method, it will throw NPE when it will try to use toString method with null content read from infile.
My txt file looks like this:
data;data2;data3;data4..........up till data3146
When I open the txt file in notepad I see it in the form given above.
But when I copy paste the first few lines to another place, There is a 1 line gap b/w data1 and everything else. Because of this I am getting problems while accessing the file in Java and using the data with a bufferedreader in a loop. How can I correct this? I can't remove the empty line as it is not even visible in the original file.
You can ignore the blank line(s). Something like this -
while ((line = reader.readLine()) != null) {
if(line.trim().isEmpty()) {
continue;
}
...
you can try this way:
BufferedReader reader = new BufferedReader(new FileReader(new File("your file path")));
String str = null;
while((str = reader.readLine())!=null) {
if (str.matches("[' ']+")) {
continue;
} else {
// to do
}
}
I believe that the problem is in the line endings. Basically you can skip the empty lines:
String line;
while ((line = reader.readLine()) != null) {
if ("".equals(line.trim()) {
continue;
}
// do your stuff here
}