reading from file

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2008
Posts: 8
Reputation: dakrous is an unknown quantity at this point 
Solved Threads: 0
dakrous dakrous is offline Offline
Newbie Poster

reading from file

 
0
  #1
Apr 17th, 2008
I'm really knew to this programming thing, and need a little help. My objective is to read data from a file like

2 2
0 0 wwdd
0 1 wwxd
1 0 ddwd
1 1 ddww

I have read the file line by line, but I do not know how to store the individual values. The first two numbers represent the length and width of the block. The rest are just coordinates.
Any ideas? The code should be flexible in case the row and columns change.

import java.io.*;
import java.util.*;
class FileRead 
{
   public static void main(String args[])
  {
       String record = null;
      try{
    // Open the file that is the first 
    // command line parameter
    FileInputStream fstream = new FileInputStream("h.txt");
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;

    
            
    while ((strLine = br.readLine()) != null)
    {
             String delims =" " ; // DEFINES CHARACTER BEING USED
                
    StringTokenizer st = new StringTokenizer(strLine, delims);
     int frow = Integer.parseInt(st.nextToken());  
     
     int fcol = Integer.parseInt(st.nextToken());  
    System.err.println(frow);
        
      System.out.println(strLine);
      
  
    }
    
    }catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }
  }
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,678
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: reading from file

 
0
  #2
Apr 18th, 2008
Store them where? Because you are already storing them in varables:
  1. int frow = Integer.parseInt(st.nextToken());
  2. int fcol = Integer.parseInt(st.nextToken());
Where you want them to be stored? You can put these variables wherever you want. Perhaps create a new class for each line and put those values in the class and then store each class in a Vector
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 8
Reputation: dakrous is an unknown quantity at this point 
Solved Threads: 0
dakrous dakrous is offline Offline
Newbie Poster

Re: reading from file

 
0
  #3
Apr 18th, 2008
The problem i am having is getting the letters. I tried char at, but it won't give me any output. I want each letter to be stored separately.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: reading from file

 
0
  #4
Apr 18th, 2008
Use String.split(), which will return an array of Strings that result from the split on your expression. You can then work with the characters in the string from that portion separately.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 8
Reputation: dakrous is an unknown quantity at this point 
Solved Threads: 0
dakrous dakrous is offline Offline
Newbie Poster

Re: reading from file

 
0
  #5
Apr 19th, 2008
Teacher wants us to use tokenize. I tried the split thing though and it worked, but he won't accept it.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 5
Reputation: reena22 is an unknown quantity at this point 
Solved Threads: 0
reena22 reena22 is offline Offline
Newbie Poster

Re: reading from file

 
0
  #6
Apr 20th, 2008
hi dakrous

i read you problem but there's something that i don't understand.
2 2
0 0 wwdd
0 1 wwxd
1 0 ddwd
1 1 ddww


Q. The 2 2 is the length and width of your block and the remaining is the input. Do you have only to print the remaining output or stored it in an array?????

well you started the code well but in the middle you mess it up.
first thing: while getting the frow and fcol, why did you put it in a loop, only once you have to get this, you have to put it outside the loop.

second thing: if you have only to print the output, then print it in the while loop or if u have to save it and then print it, create an array of the size of your block and do it there

some changes:

String delims =" " ; // DEFINES CHARACTER BEING USED
strLine = br.readLine();
StringTokenizer st = new StringTokenizer(strLine, delims);
int frow = Integer.parseInt(st.nextToken());
int fcol = Integer.parseInt(st.nextToken());
System.out.println("Length" +frow+"Width:"+fcol);
while ((strLine = br.readLine()) != null)
{
System.out.println(strLine); //or save it in the array here
}

hope that this helps.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: reading from file

 
0
  #7
Apr 20th, 2008
Originally Posted by dakrous View Post
Teacher wants us to use tokenize. I tried the split thing though and it worked, but he won't accept it.
Perhaps you should share this bit of info with your teacher. Directly from the API doc for StringTokenizer (bolding mine):
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
Forcing students to learn the old way of doing something and refusing to allow them to use the new API instead is not helpful. StringTokenizer is one of the original 1.0 classes. String.split() has been in the API since 1.4, which has been out for about 5 years now. Your teacher needs to get with the times.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 8
Reputation: dakrous is an unknown quantity at this point 
Solved Threads: 0
dakrous dakrous is offline Offline
Newbie Poster

Re: reading from file

 
0
  #8
Apr 20th, 2008
Tell me about it? Thanks for the help
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC