Page 1 of 2 12 LastLast
Results 1 to 20 of 28
Like Tree1Likes

Thread: Reading numbers from text file!

  1. #1
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default 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:

    Java 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! :)

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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? ;)
    I like likes!

  3. #3
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default 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! :)

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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);
    ...
    I like likes!

  5. #5
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default Re: Reading numbers from text file!

    Is this correct?

    Java 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);
    	
    	
    	}
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  7. #7
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default Re: Reading numbers from text file!

    It does not compile. I get the following errors when I try to compile:

    Java 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 . . .

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Reading numbers from text file!

    The compiler is telling you that you're trying to use variables without declaring them, similar to doing

    Java Code:
    i = 3;
    The solution is: don't so that. Declare your variables before using them:

    Java Code:
    int i = 3;

  9. #9
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default 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?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Reading numbers from text file!

    Quote Originally Posted by Asvin View Post
    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.

  11. #11
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default 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?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Reading numbers from text file!

    Quote Originally Posted by Asvin View Post
    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.

  13. #13
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default Re: Reading numbers from text file!

    Now my code looks like the following:

    Java 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?

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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

    Java 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.

  15. #15
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default 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:

    Java 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.
    Sierra likes this.

  16. #16
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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. :)
    I like likes!

  17. #17
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default 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?

  18. #18
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default Re: Reading numbers from text file!

    I disposed the code I provided in the OP. This is what I have right now:

    Java 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:

    Java 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?

  19. #19
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default 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.

  20. #20
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default 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!

Page 1 of 2 12 LastLast

Similar Threads

  1. Reading a text file
    By dougie1809 in forum New To Java
    Replies: 23
    Last Post: 03-13-2012, 12:22 AM
  2. Reading file full of numbers, cannot add them together
    By quinnvanorder in forum New To Java
    Replies: 8
    Last Post: 02-26-2012, 04:28 AM
  3. Seaching a text file for credit card numbers
    By javamann in forum New To Java
    Replies: 9
    Last Post: 11-21-2011, 01:44 AM
  4. Replies: 8
    Last Post: 07-17-2011, 03:38 PM
  5. Reading a text file
    By diegosened in forum New To Java
    Replies: 4
    Last Post: 01-16-2010, 12:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •