I am trying to read information from a file but for each line it just returns a null
String[] quotes = new String[numberOfLines];
String myLine;
for (int i = 0; i < numberOfLines; i++)
{
myLine = readFile.readLine();
System.out.println(myLine);
quotes[i] = myLine;
}
numberOfLines in the number of lines that actually has characters on them in the file
BufferedReader.readLine returns null if and only if you have read to the end of the file / stream: See javadoc.
Therefore, you have reached the end of file.
Therefore the problem is somewhere else in your code:
how readFile is instantiated / used (e.g. have you opened the right file?), or
how you get the value for numberOfLines.
Unfortunately, we can't go further without seeing the code that does those things. Or better still, an MCVE.
UPDATE
One possibility: the code you use to count the lines has read the file via readFile and left the BufferedReader positioned at the end of file.
Related
So I wrote some code for a vocabulary trainer for my german class and want to write the content of my ArrayLists to a file. However it only writes the first of the 3 ArrayLists into the file when saving. Does anyone know what causes this, or better yet, how to fix it? Thanks for you help!
I have already reset all the ArrayLists and re-implemented the file it should write into, but nothing helped.
These are all just sequences of Code, not the whole program. It is over 400 lines Long so I didnt want to paste the whole thing. The Code runs flawlessly until I open the file I wrote into.
static ArrayList<String> vokabel = new ArrayList<String>();
static ArrayList<String> uebersetzung = new ArrayList<String>();
static ArrayList<Integer> kasten = new ArrayList<Integer>();
static void beenden() {
for(int m = 0; m < groesse; m++) {
String str = vokabel.get(m).toString();
textWriter.write(str);
textWriter.write(" ");
}
textWriter.close();
textWriter.println();
for(int n = 0; n < groesse; n++) {
String str = uebersetzung.get(n).toString();
textWriter.write(str);
textWriter.write(" ");
}
textWriter.close();
textWriter.println();
for(int o = 0; o < groesse; o++) {
String str = kasten.get(o).toString();
textWriter.write(str);
textWriter.write(" ");
}
textWriter.close();
textWriter.println();
System.exit(0);
}
I expect it to write the content of all 3 ArrayLists into the file, though it didn't work up until now.
This is what ends up in the file after entering 3 words with their translations and their corresponding case number. Only the words themselves make it into the file:
Hund Nein Hallo
The reason it only writes the first ArrayList into the file is because you're closing the TextWriter immediately after writing it (and when the TextWriter is closed, it doesn't write stuff). Just remove all the
textWriter.close();
lines, and then put just one right before System.exit(0), and it should work properly.
You write all 3 lists one after the other. AND: Inbetween you close the writer! So you are "lucky" not to get an IOException.
I guess you would like the entries of the 3 lists together (vokabel + uebersetzung+ kasten). Therefore I suggest you create a class taking 3 fields with the information. Give this class a sensible toString() and simply write those objects one line at a time.
Ah and btw: Don't ever call System.exit! It makes your program unusable in a larger context and prevents proper cleanup of resources.
Use Flush.when you have text longer than buffer size it will not write.
System.IO.TextWriter writeFile = new StreamWriter("c:\\textwriter.txt");
writeFile.WriteLine("csharp.net-informations.com");
writeFile.Flush();
writeFile.Close();
I am assigned to write to another document using data from an input file, and calculating the mean and standard deviation from that file. I have an issue where my code is compiling correctly (according to Dr.Java) but is not giving an output to the output file. I have the code attached that I think is the problem area. It may either be the loops used or reading the file before the loops. Could anyone tell me if those places are the places of error?
// Create a File object passing it the filename
File readFile = new File(filename);
// Create a Scanner object passing File object
Scanner inputFile = new Scanner(filename);
// Perform a priming read to read the first line of the file;
line = inputFile.nextLine();
while (inputFile.hasNext()) //create a loop that continues until you are at the end of the file
{
while( Double.parseDouble(line) != -1)
{
sum += Double.parseDouble(line); //convert the line into a double value and add the value to the sum
count++; //increment the counter
line = inputFile.nextLine(); //read a new line from the file
}
mean = sum/count;
}
inputFile.close();
The output file code:
// Create a FileWriter object using "OutputFileProcess.txt"
File file = new File("OutputFileProcess.txt");
FileWriter fw = new FileWriter(file);
// Create a PrintWriter object passing the FileWriter object
PrintWriter outputFile = new PrintWriter("OutputFileProcess.txt");
// Print the results to the output file
outputFile.println(mean);
outputFile.println(stdDev);
// Close the output file
outputFile.close();
The code needs two loops, one to continue until the ned, and one to exclude any negative numbers in the text file. I should also introduce myself. I am Joe.
This might not be the core of your problem, but one of your while loops is obsolete.
If the inner condition
Double.parseDouble(line) != -1
becomes false, the outer while loop will not change the value of line and the inner while loop will never be entered anymore.
So at the moment the whole algorithm stops at the first -1.
A better way to write this down would be
while (inputFile.hasNext())
{
final String line = inputFile.nextLine();
final double lineAsDouble = Double.parseDouble(line);
if (lineAsDouble != -1D) {
sum += lineAsDouble;
count++;
}
}
final double mean = (double) sum / (double) count;
You should also consider parsing and comparing the values as Integers (if they all are), since comparing doubles with == is always dangerous.
Below are two functions in my class, I want to first read the number of lines from a text file, then store the contents in an array. The problem I am having is that if I do not comment out int aNumber = numOfObjects(newInput); the array does not get stored and printed, it's as if numOfObjects function got to the end of the text file, and I can no longer access it. If I comment it out it works fine. I tried adding a second Scanner object but it didn't help. What can I do to make it work?
public void correctListItems(FileInputStream inputFile,FileOutputStream outputFile){
newInput = new Scanner(inputFile);
forCapturing = new Scanner(inputFile);
int aNumber = numOfObjects(newInput);
System.out.println(aNumber);
for(int i=0; forCapturing.hasNextLine(); i++){
publicationArray[i] = new Publication();
publicationArray[i].publication_code = forCapturing.nextLong();
publicationArray[i].publication_name = forCapturing.next();
publicationArray[i].publication_year = forCapturing.nextInt();
publicationArray[i].publication_authorname = forCapturing.next();
publicationArray[i].publication_cost = forCapturing.nextDouble();
publicationArray[i].publication_nbpages = forCapturing.nextInt();
System.out.println(publicationArray[i]);
System.out.println("-----------------------------------\n");
}
}
private int numOfObjects(Scanner aScanner){
int count = 0;
while (aScanner.hasNextLine()){
count++;
aScanner.nextLine(); //if this isn't included you'll experience an infinite loop
}
System.out.println(count);
return count;
}
}
There is a way to do this as you want. i.e. by reading through the file 2 times. First to count and then to capture.
Just add below lines after your line int aNumber = numOfObjects(newInput); in correctListItems function.
public void correctListItems(FileInputStream inputFile,FileOutputStream outputFile){
newInput = new Scanner(inputFile);
int aNumber = numOfObjects(newInput);
newInput.close();
inputFile.close();
inputFile = new FileInputStream(
new File(
"inputfile.txt"));
System.out.println(aNumber);
forCapturing = new Scanner(inputFile);
for(int i=0; forCapturing.hasNextLine(); i++){
....
....
So basically closing the scanner as well as file is important. And then creating the fileinputreader stream again will reset the file pointer to the beginning of the file. As you might already know, If the input file is not from the project folder, you have to give the complete path.
As a good process, its always better to close both the scanner object and file object after you're done, And then reinitialize the objects to start working on them again.
Hope this helps.
It looks like the scanner class uses an iterator internally. This means that it needs to be closed at some point, which I cant find in your code. Therefore I would (1) add following line to the numOfObects function before the return: “aScanner.close()”.
(2) I would create the second Scanner instance after you called the function, just to be sure. Hope it works.
Cheers!
The scanner doesn't move to the next line unless you call nextLine. So the loop is infinite since you're always on the first line.
But why do you need to know the number of objects in advance? Why not simply use a list instead of publicationArray?
Well, I am not exactly sure but I can be quite certain that the FileInputStream object once the bytes has been read by Scanner once, the scanner will have a token to indicate that a particular line has been read.
How about you change your code to:
public void correctListItems(FileInputStream inputFile,FileOutputStream outputFile){
forCapturing = new Scanner(inputFile);
for(int i=0; forCapturing.hasNextLine(); i++){
publicationArray[i] = new Publication();
publicationArray[i].publication_code = forCapturing.nextLong();
publicationArray[i].publication_name = forCapturing.next();
publicationArray[i].publication_year = forCapturing.nextInt();
publicationArray[i].publication_authorname = forCapturing.next();
publicationArray[i].publication_cost = forCapturing.nextDouble();
publicationArray[i].publication_nbpages = forCapturing.nextInt();
System.out.println(publicationArray[i]);
System.out.println("-----------------------------------\n");
}
System.out.println("Number of lines: "+ i);
}
At least with this, you would not have to run 2 loops to the same set of data. better performance too and get the thing you need done in 1 round of a loop
For example, in my code, I have something like this:
for (int i = 0; i < arraylist.size(); i++) {
while (file.hasNext())
When the for loop goes to i = 1, will it start reading the file at the beginning again, or will the file not be read at all since it was read through at i = 0?
For i > 0, file.hasNext() will always return false since you have already read through the file at i = 0 (leaving the file pointer at the end of the file). So, nothing will happen for i > 0.
To read through the file on each iteration of the for loop, you must seek back to the start of the file after the while loop.
I am not sure what the type of file is in your question, but take a look at java.io.RandomAccessFile and its seek(long pos) method for how to accomplish this (seek(0) rewinds to the start of the file).
It will read from beginning. Does not matter whatever i is. Because you are not telling scanner to not read until some value of i is reached.
Infact, I honestly don't see this loop's significance since you are using while loop after this as well. It will just print same file's all lines once.
For example, if your file has these contents:
con1
con2
con3
and lets say your array size is 4. Then by your loop, this will be the output:
con1
con2
con3
If you want to say, start printing from second line, then use some integer for iteration reference and start printing once its 2. Something like:
int x = 0;
while(scanner.hasNext())
if(x >= 2)
System.out.println(scanner.next());
x++;
}
I don't really get what you want to do, do you want to add each word in the file to an arraylist? And then, didn't you use Scanner to read?
Scanner scanner = new Scanner(new File("path.ext"));
ArrayList<String> strings = new ArrayList<String>();
while(scanner.hasNext()) {
strings.add(scanner.next());
}
scanner.close();
was that your problem?
In this case you read each word in the file from start to end
Your code example is incomplete. If file is an instance of Scanner, hasNext only indicates that there is another token to read, but it does not advance the position in the input stream. Assuming you make a call to next in your block, then THAT call will advance, but it won't start again from the beginning unless you reinitialize your file variable to point to a new Scanner instance.
I have a program which searches through a mini-database I have created in a text file, everything works fine, except the fact that whatever is the very last line in the last line of the data is not recognized and whenever I attempt to search for it, it will fail and I am informed that the data does not exist in the database, although it very clearly does when I look at the txt file.
I managed to get a work-around for this, but adding a fake line at the very end that didn't have data, and something I would use such as "xxxxxxxxxx", and then it was able to read the data on top, which originally was the last line before. The problem with this is, I also have to be able to add data to the function straight from my java program, and it would be extremely tedious to have to first remove that fake line, add my information, then put the fake line in again, so I am trying to figure out why it is not searching the last line of my data.
Here is the code for one of my searches:
BufferedReader i = new BufferedReader (new FileReader ("Elements.txt"));
String data=i.readLine();
while (data!=null)
{
String database[]=data.split(",");
data=i.readLine();
if (data!=null)
{
for (int x=0;x<data.length(); ++x)
{
if (database[0].equalsIgnoreCase(elementName))
{
element=database[0];
symbol=database[1];
atomicNumber=database[2];
atomicMass=database[3];
valence=database[4];
found=true;
break;
}
}
}
}
Here is the data that it is searching through:
http://i.imgur.com/GeXQhTh.png
Any idea on how to fix this?
Simply remove your if(data==null) check after you get your last line. You are getting the next line, then checking if that is valid, then continuing, which is unecessary given your while loop. In essence, you are filtering out the last line (since the next line should be null)
Also, you can just get rid of the for loop which is completely superfluous because nothing changes between each invocation (you only interact with your database[] object and x is never used; i.e., you are just doing the same thing data.length() times).
Modify your code like this:
BufferedReader i = new BufferedReader(new FileReader("Elements.txt"));
String data;
while ((data = i.readLine()) != null) {
String database[] = data.split(",");
if (database[0].equalsIgnoreCase(elementName)) {
for (int x = 0; x < data.length(); ++x) {
element = database[0];
symbol = database[1];
atomicNumber = database[2];
atomicMass = database[3];
valence = database[4];
found = true;
break;
}
}
}
for reading text files, put readLine into while condition, so you
only have to write it once and no need for extra null checks and breaks
you do not have to compare database[0]
for each token, so move "if" outside of "for"