Reading files

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1475 Views - Last Post: 05 November 2010 - 08:05 AM Rate Topic: -----

#1 xahtep9563   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 07-October 10

Reading files

Posted 04 November 2010 - 03:31 PM

Hello,

I have a program that suppose to display the content of this file:
http://rapidshare.co...StudentData.txt

I was able to display the student number and the last name but now I'm not able to display the test of the text's content properly.
If you'll run it, you'll see what I mean.
So can you tell me what an I doing wrong? I know it has to do something with the substring of "firstname" but don't know what.
Here's my code:



import java.io.*;

public class DisplayFile
{
   public static void main(String[] args) throws IOException
   {
      
      // Open the file.
      FileReader freader = new FileReader("StudentData.txt");
      BufferedReader inputFile = new BufferedReader(freader);

      // Read the first line from the file.
      String str = inputFile.readLine();

      while (str != null) // display & read lines
      {
         // get student number
         String stno = str.substring(0,7);
         // get last name
         str = str.substring(8);
         int i = str.indexOf(',');
         String lastname = str.substring(0, i);
         
         //get first name
         str = str.substring(7);
         int j = str.indexOf(',');
         String firstname = str.substring(0, '\t');
         
         // display information 
         System.out.println("\t"+stno+"\t\t"+lastname+"\t\t"+firstname);
         str = inputFile.readLine();
      }

      // Close the file.
      inputFile.close();

   }

public static int larger(int mark1, int mark2)
{
return 0;
}}


This post has been edited by xahtep9563: 04 November 2010 - 03:35 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Reading files

#2 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Reading files

Posted 04 November 2010 - 03:46 PM

Can you use a string array or do you have to use substring()?
Was This Post Helpful? 0
  • +
  • -

#3 xahtep9563   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 07-October 10

Re: Reading files

Posted 04 November 2010 - 03:53 PM

I can but I'm not asked to.
If I'm not mistaken, string array involves the for loop?
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Reading files

Posted 04 November 2010 - 04:02 PM

No loop required. :)

By splitting the text line read from file on the commas the output of the split() method is a string array containing each of the individual strings.
Was This Post Helpful? 0
  • +
  • -

#5 xahtep9563   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 07-October 10

Re: Reading files

Posted 04 November 2010 - 04:39 PM

I dont seem to be able to get it...
I've used the split method only once in the past so I'm not familiar with that method...
My statement should look like:?
String[] Name1 = firstname.split(",");
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Reading files

Posted 04 November 2010 - 04:48 PM

Can you post a couple lines from your file format into a post for reference?
Was This Post Helpful? 0
  • +
  • -

#7 xahtep9563   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 07-October 10

Re: Reading files

Posted 04 November 2010 - 04:50 PM

That is the content of StudentData.txt

1112345,Jones,Smith,100,90,
3012345,Date,Christopher,88,79,
1154321,Dann,Wanda,55,55,
3054321,Cooper,Stephen,90,100,
Was This Post Helpful? 0
  • +
  • -

#8 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Reading files

Posted 04 November 2010 - 04:52 PM

Quote

My statement should look like:?
String[] Name1 = firstname.split(",");


No,

like:

...
while (str != null) // display & read lines
{
    String [] tokens = str.split(",");
...



See split()

This post has been edited by n8wxs: 04 November 2010 - 04:53 PM

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Reading files

Posted 04 November 2010 - 04:54 PM

Your split() statement looks good to me. The only thing I want to note is that you will have an extra element at the end of the array because of the comma at the end. What is your problem? Can you post revised, relevant code?
Was This Post Helpful? 0
  • +
  • -

#10 xahtep9563   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 07-October 10

Re: Reading files

Posted 04 November 2010 - 05:00 PM

Ok well, now I'm completely lost...
Like I said, I'm able to display the student number and the last name but I'm not able to display the rest. In most cases I was missing letters.

Here is what my code looks like at the moment:

    import java.io.*;
    import java.util.*;
    import javax.swing.*;

/**
   This program reads lines from a file.
   Each line represents a student in a comma-separated format.
   Each line is parsed to extract & display the student number and last name
*/

public class DisplayFile
{
   public static void main(String[] args) throws IOException
   {
      
      // Open the file.
      FileReader freader = new FileReader("StudentData.txt");
      BufferedReader inputFile = new BufferedReader(freader);

      // Read the first line from the file.
      String str = inputFile.readLine();

      while (str != null) // display & read lines
      {
          
   
         // get student number
         String stno = str.substring(0,7);
         // get last name
         str = str.substring(8);
         int i = str.indexOf(',');
         String lastname = str.substring(0, i);
         
        
         //get first name
         String [] firstname = str.split(",");
         
         // display information 
         System.out.println("\t"+stno+"\t\t"+lastname+"\t\t"+firstname);
         str = inputFile.readLine();
      }

      // Close the file.
      inputFile.close();

   }

public static int larger(int mark1, int mark2)
{
return 0;
}}






And here is another case where I was missing letters from my "firstname"

/get first name
         str = str.substring(9);
         int j = str.indexOf(',');
         String firstname = str.substring(0, j);


This post has been edited by xahtep9563: 04 November 2010 - 05:02 PM

Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Reading files

Posted 04 November 2010 - 05:06 PM

Don't even use substring(). Just read in the entire line as n8wxs suggested, and use split() on it. The resulting array will contain the tokens in the order {"1112345","Jones","Smith","100","90",""}. Note that the commas in the array are *not* part of the String.
String temp = inputFile.readLine();
while(temp != null){
   String[] splt = temp.split(",");
   //other code
}


Was This Post Helpful? 0
  • +
  • -

#12 xahtep9563   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 07-October 10

Re: Reading files

Posted 04 November 2010 - 05:09 PM

Alright, thanks guys!
I'm gonna try it now.
Was This Post Helpful? 0
  • +
  • -

#13 xahtep9563   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 07-October 10

Re: Reading files

Posted 04 November 2010 - 05:36 PM

Well, I've spent too much time on this code and my brain won't work properly.

I get something weird when I run the program.

 String temp = inputFile.readLine();

      
         while(temp != null)
         {   
	   String[] line1 = temp.split(",");
 
         System.out.println(line1);
         temp = inputFile.readLine();
      }



I think I missed something?
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Reading files

Posted 04 November 2010 - 05:46 PM

As arrays do not override the Object toString() method, you will get their memory address when you pass them to the print() or println() methods, as you have done. Instead, you will need to loop through the array and print out each element individually.
Was This Post Helpful? 0
  • +
  • -

#15 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Reading files

Posted 04 November 2010 - 06:16 PM

Here's a bit of your code modified a bit :) to show you how to use the split() method.

// Read the first line from the file.
String str;

while ((str  = inputFile.readLine())!= null) // display & read lines
{
    String [] tokens = str.split(",");

    for (int i = 0; i < tokens.length; i++)
        System.out.println("tokens[" + i + "] = " + tokens[i]);

    // get student number
    String stno = tokens[0];
    // get last name
    String lastname = tokens[1];

    //get first name
    String firstname = tokens[2];

    // display information
    System.out.println("\t" + stno + "\t\t" + lastname + "\t\t" + firstname);
}

// Close the file.
inputFile.close();



Output:

tokens[0] = 1112345
tokens[1] = Jones
tokens[2] = Smith
tokens[3] = 100
tokens[4] = 90
        1112345                Jones                Smith
tokens[0] = 3012345
tokens[1] = Date
tokens[2] = Christopher
tokens[3] = 88
tokens[4] = 79
        3012345                Date                Christopher
tokens[0] = 1154321
tokens[1] = Dann
tokens[2] = Wanda
tokens[3] = 55
tokens[4] = 55
        1154321                Dann                Wanda
tokens[0] = 3054321
tokens[1] = Cooper
tokens[2] = Stephen
tokens[3] = 90
tokens[4] = 100
        3054321                Cooper                Stephen


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2