write a matrix to file


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: write a matrix to file

  1. #1
    Join Date
    Sep 2002
    Posts
    9

    Red face write a matrix to file

    Hi volks!

    I need a little help.
    I have a matrix which has 25 rows and 4 columns (it's defined "MiaSquadra[25][4]").
    What I wanna do is to transfer the datas from this matrix to an empty exisiting file called "juventus.txt".
    But the writing must be personalized, in fact I would like to write 25 rows (corresponding to the rows of the matrix) and each rows should be composed by the datas from the 4 columns with a symbol "%" as delimitator.

    Example:
    MiaSquadra[1][1] = Christian Buffon
    MiaSquadra[1][2] = P
    MiaSquadra[1][3] = Juventus
    MiaSquadra[1][4] = 23

    so I should open the file "juventus.txt" (which is empty) and write the first row that must appears like follow:

    Christian Buffon%P%Juventus%23

    and so on for the remaining rows till 25.
    I tried in different ways to do this but I failed
    I am new in Java and maybe I wrong just some small thing
    Can someone suggest me the code?

    Thanks in advanced!!!!
    Bye miriam

  2. #2
    Join Date
    Aug 2002
    Posts
    94
    hi,
    read documentation for StringTokenizer class - it has an array of methods and variables for convenient string manipulations as you have asked. Here is one sample code:

    String s="Christian Buffon%P%Juventus%23");
    StringTokenizer st=new StringTokenizer(s,"%");
    int num=st.countTokens();
    String str[]=new String[num];
    int count=0;
    while(st.hasMoreElements()){
    str[count]=st.nextToken();
    count++;
    }

    bye

  3. #3
    Join Date
    Sep 2002
    Posts
    9
    I have read the documentation about StringTokenizer and all its costructors to split lines in tokens... but I didn't solve my problem.
    Maybe I didn't understand your suggestion but I couldn't apply StringTokenizer class to my problem because I have a matrix and I want to write on a file, that means I have to do an output stream.
    The documentation about StringTokenizer class was relating examples of an input stream when you read from file and want to split lines in tokens....
    Sorry for my lack of knowledge, probably I didn't catch your advice... can you explain me what I have to do exactly?
    thanks again!
    Mirjam

  4. #4
    Join Date
    Aug 2002
    Posts
    94
    String s= MiaSquadra[1][1]+"%"+ MiaSquadra[1][2] +"%"+
    MiaSquadra[1][3] +"%"+
    MiaSquadra[1][4] +"%";

    String concatenation is this easy in java and the above will translate to :

    String s="Christian Buffon%P%Juventus%23");

    % is a meatcharacter and hence you may have to use +"/%" instead of "%" - just check it out.

    Hope this helps.

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