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

Recommended Answers

All 7 Replies

Store them where? Because you are already storing them in varables:

int frow = Integer.parseInt(st.nextToken());
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

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.

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.

Teacher wants us to use tokenize. I tried the split thing though and it worked, but he won't accept it.

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.

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.

Tell me about it? Thanks for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.