Reading numbers from text file!
Hey guys,
I have a project in one of my classes that has to do with sorting an array of unsorted numbers. First, I have to read the numbers, which are student ID #s from a separate text file. I have created a text file with the following info:
Code:
Mark, 25, 3.5, 0789126
Jessica, 21, 3.9, 0769821
Ashley, 19, 3.1, 0791582
Tom, 18, 3.7, 0698751
Brittney, 22, 3.2, 0581943
Kevin, 25, 3.4, 0968242
Joseph, 25, 3.1, 0181793
John, 19, 2.8, 0908716
Dimitri, 23, 3.0, 0891769
Kimberly, 24, 3.8, 0981913
The first item is the student name, the second is the age, the third is the gpa, and finally the fourth is the student ID. All I want to do is import the ID into my java program. How can I do this so that I can go ahead and sort it once I import it?
Thank you so much for any help! :)
Re: Reading numbers from text file!
You could try the BufferedReader, FileReader and File classes for reading your text file. The BufferedReader class has a readLine if I remember correctly...
You might also will find the
sResult = String.split(...)
sResult = String.trim()
methods most useful.
Sorry for not giving out all at once, but that would be half the fun for you, right? ;)
Re: Reading numbers from text file!
Thanks for your input! :)
I looked at each of those classes, but I can't decide which one to use. Given my requirements and the data in the text file, can you please tell me which class will do the trick?
Thanks! :)
Re: Reading numbers from text file!
You need to create the "File", after that you create a "new FileReader(...)" for it and after that you create a "new BufferedReader(...)" from the FileReader object...
oFile = new File("myfile.txt");
oReader = new FileReader(oFile);
...
Re: Reading numbers from text file!
Is this correct?
Code:
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;
public class BinaryInsertionSort {
public static void main(String[] args) {
firstFile = new File("StudentInformation");
firstReader = new FileReader(firstFile);
firstBufferedReader = new BufferedReader(firstReader);
}
}
Re: Reading numbers from text file!
Does it compile? Does it work? Better for you to test this first before asking us, and then if you run into problems, show us the error messages or describe the problem.
Re: Reading numbers from text file!
It does not compile. I get the following errors when I try to compile:
Code:
BinaryInsertionSort.java:9: error: cannot find symbol
firstFile = new File("StudentInformation");
^
symbol: variable firstFile
location: class BinaryInsertionSort
BinaryInsertionSort.java:10: error: cannot find symbol
firstReader = new FileReader(firstFile);
^
symbol: variable firstReader
location: class BinaryInsertionSort
BinaryInsertionSort.java:10: error: cannot find symbol
firstReader = new FileReader(firstFile);
^
symbol: variable firstFile
location: class BinaryInsertionSort
BinaryInsertionSort.java:11: error: cannot find symbol
firstBufferedReader = new BufferedReader(firstReader);
^
symbol: variable firstBufferedReader
location: class BinaryInsertionSort
BinaryInsertionSort.java:11: error: cannot find symbol
firstBufferedReader = new BufferedReader(firstReader);
^
symbol: variable firstReader
location: class BinaryInsertionSort
5 errors
Press any key to continue . . .
Re: Reading numbers from text file!
The compiler is telling you that you're trying to use variables without declaring them, similar to doing
The solution is: don't so that. Declare your variables before using them:
Re: Reading numbers from text file!
So, should I create an array that will stream the ID #s from the text file? How can I do that?
Re: Reading numbers from text file!
Quote:
Originally Posted by
Asvin
So, should I create an array that will stream the ID #s from the text file? How can I do that?
I don't recall anyone suggesting that you do this in the answers to be found here. Where do you see this suggestion here? If this were my project, I'd be working on one thing at a time, and first I'd be making sure that I am able to read from the file. Only when that is working would I worry about putting the data into an array. And no you won't be having any array "stream". You'll be using your readers to extract the data which you will put into your array.
Re: Reading numbers from text file!
Well isn't that what I should do? Didn't YOU tell me to create variables? I was just asking if arrays is the way to do it? How else can I do it?
Re: Reading numbers from text file!
Quote:
Originally Posted by
Asvin
Well isn't that what I should do? Didn't YOU tell me to create variables? I was just asking if arrays is the way to do it? How else can I do it?
I did tell you to create variables -- create the variables that the compiler is telling you you've not declared -- your File variable, FileReader, and BufferedReader. Start there, then try to read the file and println the results. Again, one step at a time.
Re: Reading numbers from text file!
Now my code looks like the following:
Code:
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;
public class BinaryInsertionSort {
public static void main(String[] args) {
int[] dataArray;
try {
File firstFile = new File("StudentInformation");
FileReader firstReader = new FileReader(firstFile);
BufferedReader firstBufferedReader = new BufferedReader(firstReader);
}
catch(Exception e) {
System.out.println("Error!");
}
}
}
It compiles fine, but when I run it, it shows the message "Error!," which is what I specified in the code. What next?
Re: Reading numbers from text file!
Your catch block needs to be more informative as it's only telling you what you've programmed it to do. Consider instead having it print out the entire stack trice via
Code:
catch(Exception e) {
e.printStackTrace();
}
Note that it's usually best to catch individual exceptions rather than Exception, but this can remain unchanged for now.
Re: Reading numbers from text file!
In the JDK 7, using the new java.nio API, you can simplify how to read a file. You can do it like the code snippet below:
Code:
List<String> lines = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
This code above will read all lines from your text file and return it as a List of strings. This method also ensure that the file will be closed when all lines are read or if an I/O exception occurs. But it will be not good if you have a very large file to read.
Re: Reading numbers from text file!
I suspect your file is not being found - which is the usual Exception - in the catch block you may call:
e.printStackTrace();
to show the exact error message and type of exception on your console.
wsaryada: Nice one and it is true, I am still old school in some aspects... though I also like the old more object orientated approach. :)
Re: Reading numbers from text file!
Would I replace all my code so far with the one you provided or just get rid of the try and catch block and insert the code you listed?
Re: Reading numbers from text file!
I disposed the code I provided in the OP. This is what I have right now:
Code:
import java.io.*;
import java.util.*;
public class BinaryInsertionSort {
private Scanner x;
public void openTextFile() {
try {
x = new Scanner(new File("StudentInformation.txt"));
}
catch (Exception e) {
System.out.println("Error! Could not find file!");
}
}
public void readTextFile() {
while(x.hasNext()) {
String a = x.next();
String b = x.next();
String c = x.next();
String d = x.next();
System.out.printf("%s %s %s %s\n", a,b,c,d);
}
}
public void closeTextFile() {
x.close();
}
}
This is the output:
Code:
Mark 25 3.5 0789126
Jessica 21 3.9 0769821
Ashley 19 3.1 0791582
Tom 18 3.7 0698751
Brittney 22 3.2 0581943
Kevin 25 3.4 0968242
Joseph 25 3.1 0181793
John 19 2.8 0908716
Dimitri 23 3.0 0891769
Kimberly 24 3.8 0981913
Press any key to continue . . .
Can someone please tell me how I can have all the ID #s, which are the last numbers in each line, add to an array?
Re: Reading numbers from text file!
It would be better to use List (ArrayList) in this case. Because you don't know how many lines are read from the file. To add the IDs into this list first you need to create the ArrayList object. Add the IDs to this list in your while loop in the readTextFile method. In the end you can convert this List into an array.
Re: Reading numbers from text file!
Thank you so much for your reply! :)
I am gonna play with it and see what I come up with!