0

My code should read file and store each line(each record) into a String array.

My txt file is it:

FName Lname Number
second secondsecond 22
thired thithird 33
fourth fourfourr 44
fifth fiffif 55

but, when i run my code, my program do not display first character of each line! Show like this:

econd secondsecond 22
hired thithird 33
ourth fourfourr 44
ifth fiffif 55

My code:

public class ReadfileIntoArray {

String[] columns=new String[]  {"FName","Lname","Number"};
String[] data=new String[100];

public void read() throws IOException{
FileReader fr=new FileReader("D:\\AllUserRecords.txt");
BufferedReader br=new BufferedReader(fr);
String line;  

while((line=br.readLine())!=null){

    for(int i=0;i<=br.read();i++){
        data[i]=br.readLine();
        System.out.println(data[i]);
    }
    }  
br.close();
System.out.println("Data length: "+data.length);
}

public static void main(String[] args) throws IOException{
    ReadfileIntoArray rfta=new ReadfileIntoArray();
    rfta.read();
}
}

And i want see the data length:5 (because i have five line), But i see 100 !

(I want this information for abstract table model)

Thank You.

6 Answers 6

2

Because you have declared array size as 100 on the second line. So how you have basically two options, if count of the lines in file won't change, then declare the size of the array as 5. If it is going to vary, then I suggest you to use for example ArrayList.

List<String> data = new ArrayList<String>();
//in the while loop
data.add(br.readLine());
Sign up to request clarification or add additional context in comments.

5 Comments

The lines are going to more
Then use an ArrayList as I suggested, it's a collection which means that you don't have to care about it's size, it will change dynamicaly as you need
Can i use ArrayList in table model constructor? How?
Use .toArray() to get a String array once you're done reading. (i.e. Use data = (String[]) myList.toArray(); after the loop.)
@user1945649: what is table model constructor?You can use it wherever you want, just create a list and then use its method add to new strings - just as I wrote above
1

Your code modified:

public class ReadfileIntoArray {

    String[] columns = new String[] { "FName", "Lname", "Number" };
    String[] data = new String[100];

    public void read() throws IOException {
        FileReader fr = new FileReader("D:\\AllUserRecords.txt");
        BufferedReader br = new BufferedReader(fr);
        String line;

        int i = 0;
        while ((line = br.readLine()) != null) {
            data[i] = line;
            System.out.println(data[i]);
            i++;
        }
        br.close();
        // This is for resize the data array (and data.length reflect new size)
        String[] dataNew = new String[i];
        System.arraycopy(data, 0, dataNew, 0, i);
        data = dataNew;
        System.out.println("Data length: " + data.length);
    }

    public static void main(String[] args) throws IOException {
        ReadfileIntoArray rfta = new ReadfileIntoArray();
        rfta.read();
    }
}

Comments

1

Your data array will always be the size of 100, because when you instance it (String[] data = new String[100]) creates a blank array with 100 indexes. Instead of using a String[], you could use a List<String>

3 Comments

Is list that Arraylist? Or it is different?
Yes. List<String> is an interface. It is good practice to use a variable of this type. It can point at an ArrayList<String> object.
Can i use ArrayList in table model constructor? How?
0

Inside of the loop that reads the lines, use the String method split to process each line. Do not draw from the reader, as it moves the file pointer forward as it reads. You can split on whitespace by using

String [] parts = stringName.split("\\s");

Then you have complete access to all three items in each line.

Comments

0

br.read() is reading one character from the start of each line, leaving br.readLine() to read the rest.

This inner loop makes little sense.

for(int i=0;i<=br.read();i++){
    data[i] = br.readLine();
    System.out.println(data[i]);
}

This should be all you need. If you don't want the first line, add an additional call to br.readLine() before the loop.

int i = 0;
while((line=br.readLine())!=null){
    data[i] = line;
    System.out.println(line);
    i++;
}  

You should also try using a dynamicaly-sized data structure to store the strings (e.g. ArrayList<String>) if you don't know how many lines to expect. You can then use myList.size() to get the line count.

List myList = new ArrayList<String>();
while((line=br.readLine())!=null){
    myLine.add(line);
    System.out.println(line);
}  

System.out.println(myList.size());

//Retrieve the data as a String[].
String[] data = (String[]) myList.toArray();

2 Comments

Can i use ArrayList in table model constructor?
I don't believe so, but List does have .toArray(...). Collect your Strings in the ArrayList, then use .toArray(...) to get a String[] when needed.
0

why so complicat? below is my solution, for reference.

String line;  
int cnt;

cnt = 0;
while((line = br.readLine()) != null){
    System.out.println(line);
    cnt++;
}

br.close();
System.out.println("Data length: "+cnt);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.