from file to class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: from file to class

  1. #1
    Join Date
    Sep 2002
    Posts
    9

    Unhappy from file to class

    Hi ,

    I'm just a beginner in java programming and I would like to ask how to manage the reading from file to class.
    I mean, I have a file containing this kind of information:

    Abbiati%goalkeeper%MILAN%2
    ....
    ....

    the 1st string before the % is a name of the player (in a soccer team) the 2nd is his role, the 3rd is his team and the last is the credits of his quotation.
    Well, what I want to do is to read these infos from file and save them in a class by using some arrays.
    For example I will create:

    String name[]
    String role[]
    String team[]
    String credits[]

    and I want that :

    String name[0] = Abbiati
    String role[0] = goalkeeper
    String team [0] = MILAN
    String credits[0] = 2

    And so on for each rows of my file which contains all the players of MILAN team.
    I simply don't know how realize this thing in java...

    Can someone suggest me the java code?
    Thanks in advance for your cooperation!!
    bye!

  2. #2
    Join Date
    Aug 2002
    Posts
    94
    Hi,
    thats quite a task you have taken for a java beginner

    First clarify if you are using java application or applet. By default applications have more priviliges in terms of reading/writing to local disks as compared to applets.

    You will have to use java.io.* package that contains various classes for input/output streams.

    Given belowis one example of java code that read its own file:

    import java.io.*;

    public class readsource{
    public static void main(String args[]){
    try{
    FileReader file=new FileReader ("readsource.java");
    BufferedReader buff=new BufferedReader(file);
    boolean eof=false;
    while(!eof){
    String line=buff.readLine();
    if(line==null)
    eof=true;
    else
    System.out.println(line);
    }
    buff.close();
    }catch(IOException e){
    System.out.println("Error Pankaj:"+e.toString());
    }
    try{
    Thread.currentThread().sleep(15*1000);
    }catch(InterruptedException e){}
    }
    }

    For the remaining part of your desired functionality you need to get hands on string parsing, vectors or suitable data set for holding the data.
    bye
    Pankaj

  3. #3
    Join Date
    Sep 2002
    Posts
    9
    Hi!
    Many thanks for your support!!
    You're right, I didn't specify if I have to do an application or an Applet ....
    I have to do an application!
    Well, I've read your code and I think to have caught the meaning of everything a part of the last few lines:

    try{
    Thread.currentThread().sleep(15*1000);
    }catch(InterruptedException e){}
    }

    what does it mean?

    I have also to say that I know only C language so I can't use the Vector class you suggested me. With C language I would have used a struct, but I think that Java doesn't support this kind of data... I'm a VERY beginner you know
    Other thing: can I use a similar code to do the opposite task? that means to write infos from array to file?
    many thanks again!
    bye!!

  4. #4
    Join Date
    Aug 2002
    Posts
    94
    Hi,

    Sorry that piece of code was a copy and paste problem. There is no thread defined in this code but for your information that code when executed will stop the thread for specified milliseconds (15 in the given case) thus if you are designing an animation of images and you want each image to be displayed for specified time then you will need this kind of code with sleep() method. But in the example I posted it was not required and it is a mistake on my part - this is good observation

    Yes you can write the array back to the file with the only difference that istead of FileReader class you will have to create instance of FileWriter class

    For your code you will have to create one generic class (data type ) like this:
    public class player(){
    String name;
    String role;
    String team;
    String credits;
    public player(String s1,String s2,String s3,String s4)
    name=s1;
    role=s2;
    teams3;
    credits=s4;
    }
    }
    The above class only contains a constructor that assigns values to four variables as defined above.

    Now in your main code or application you will have to create an instance for each line of data in your file thus an example can be:

    player player1= new player("Abbiati","goalkeeper","Milan","2");

    You will have to create as many "player" objects as the number of players in your data file - you can also create an array of players like
    teamplayer[] =new player[team_size];

    This is a static method of creating an array and you will have to ensure that the size does not exceed the array limit. Alternately you can also create a dynamic array but "vectors" have the advantage of expanding/shrinking as required and you do not have to worry about the size of the array required or for cleaning up allocated memory when the object is no longer in use. All these things are automaticlly taken care of in vectors.

    BTW java has mutitude of data structures to suit your requirements.
    bye
    Pankaj

  5. #5
    Join Date
    Sep 2002
    Posts
    9
    Hi!

    The idea to create a class (like the player class you have suggested) that simulates the struct data of C language is very great. I think I can manage it quite good!

    Well, I used the code you gave me few times ago to read datas from file and I also added a piece of code to share my line in tokens. I used the symbol of percentage (%) as delimit's mark since the blank spaces were not usefull to my purpose.
    Here below you see the code:

    while(!eof)
    {
    String line = buff.readLine();
    if(line==null)
    eof=true;
    else
    {
    StringTokenizer st=newStringTokenizerline, "%");

    int i=0;
    while (st.hasMoreTokens())
    {
    vector[i] = st.nextToken("%");
    System.out.println(vector[i]);
    i++;
    }
    }
    }

    Notice that it works!!!

    Ok, after this I run into another problem...
    Instead of print these tokens with a System.out.println I should visualize them into a table consists in 30 rows (number of player per team) and 5 columns (name and surname, role, team, credists.. ).
    I try to use a JTable to accomplish this task, but I must say that it's appeared to me a little bit complicate....
    Can you suggest me some about it?
    Thanks again for your availability!!!

    PS: don't worry for your copy/paste error, as a matter of fact I learned more about java!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center