I am trying to figure out how to read a multi line txt file with a data like ranking, athlete name and Athletic sports name with two different year records like 2007 and 2008

like
1. JOHN CARTER in Sprint Race
19. JOHN CARTER in Sprint Race

I am trying to strip three things a part ranking in 2d array, 2d name array with first name and last name separated and event name.

lets say first 50 athletes ranking for 2007 and 2008 goes like this
1.
2.
3.
4.
.
.
.
.
50.
1.
2.
3.
4.
.
.
.
.
.50
I am totally lost I read couple of articles dealing with reading from from txt file articles and creating arrays but could not do extract anything fruitful.

BufferedReader reader = null;
try {
    reader = new BufferedReader(new FileReader("fileName"));
    String line = reader.readLine();
    while (line!=null) {
        System.out.println("The Line is: "+line);

        // do calculations with line

         line = reader.readLine();
    }
} catch (Exception e) {

} finally {
  if (reader!=null) reader.close();
}

If you are unfamiliar with using arrays, I would suggest to concentrate on that first, before going to write the code for reading from the file. Once you have understood the arrays very well, you may proceed.
Also it is best to have your file formatted like this:

1;JOHN CARTER;Sprint Race
19;JOHN CARTER;Sprint Race

Because of the split method. Try to run this example:

String s = "1;JOHN CARTER;Sprint Race";
String [] array = s.split(";");
for (int i=0;i<array.length;i++) {
  System.out.println(array[i]);
}

I have never used arrays in Java, although I used them in Python but whatever u wrote up there i understand some of it because I have reading lots of blogs and online articles about java arrays.
and about the line problem my file is formatted in this way
1. JOHN CARTER in Sprint Race.

I have never used arrays in Java, although I used them in Python but whatever u wrote up there i understand some of it because I have reading lots of blogs and online articles about java arrays.
and about the line problem my file is formatted in this way
1. JOHN CARTER in Sprint Race.

Then forget about the split method and check the String API for the methods:
indexOf
subString

Find the index Of the first '.' dot and take the number using the subString
Then find the index Of 'in' and use the subString to get the name and the sport

BufferedReader reader = null;
try {
    reader = new BufferedReader(new FileReader("fileName"));
    String line = reader.readLine();
    while (line!=null) {
        System.out.println("The Line is: "+line);

        // do calculations with line

         line = reader.readLine();
    }
} catch (Exception e) {

} finally {
  if (reader!=null) reader.close();
}

If you are unfamiliar with using arrays, I would suggest to concentrate on that first, before going to write the code for reading from the file. Once you have understood the arrays very well, you may proceed.

I got this error when I tried the code
MainTest.java:31: unreported exception java.io.IOException; must be caught or declared to be thrown
read.close();

Well, since you have copied that code directly to main, you need to add this as well:

finally {

    try {
       if (reader!=null) reader.close();
   } catch(Exception e) {
   }

}

I was expecting for you to put that code in a method that throws an IOException. But putting it in main might be enough for you now.

You got that error because the reader.close(); throws an Exception. But it is best to put it in the finally block in order to make sure that it is always executed.

[code=java]
import java.io.*;
public class MainTest{
	
	public static void main(String[] args){
		BufferedReader read=null;
        String[] line=new String[201];
		try{
			
			read 
			= new BufferedReader(new FileReader("filename"));
			String test = read.readLine();
            //line[0]=test;
            //System.out.println(line[0]);
            int i=0;
			while (test!=null ){//&&(i<line.length)){//do{
				
                    test=read.readLine();
                    line[i]=test;
					i++;
                    System.out.println(line[i]);
			}//while (in.readLine()!=null);
		}catch (IOException e)
        {
             // Some problem reading the data from the input file.
            System.out.println("Input Error: " + e.getMessage());
        }finally{
			try{
				if (read!=null) read.close();
			}catch (IOException e){
				System.out.println(e.getMessage());}
		}
        
		
	}

}

I have tried couple of things now I am dealing two types of error one is out of bounds and other one is its giving me only nulls.

well I took your Idea and just doing an extra thing that it reads the line and add it to array now I cleaned up my code here it is

[code=java]
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import java.io.*;
public class Main{

	public static void main(String[] args){
		BufferedReader read=null;
        String[] line=new String[205];
		try{

			read
			= new BufferedReader(new FileReader("filename"));
			String test= read.readLine();
            
            int i=0;
            
			while (line != null ){
                //System.out.println(test);
                if (test.equals("")){
                    test=read.readLine();
                }
                else{
                    line[i]=test;
					i++;
                    System.out.println(line[i]);
                }

                    
			}//while (in.readLine()!=null);
		}catch (IOException e)
        {
             // Some problem reading the data from the input file.
            System.out.println("Input Error: " + e.getMessage());
        }finally {

            try {
                if (read!=null) read.close();
            }catch(Exception e) {
                System.out.println("Input Error: " + e.getMessage());
        }

        }


	}

}

Let me explain this code a little and then I will tell you your mistake:

BufferedReader reader = null;
try {
    reader = new BufferedReader(new FileReader("fileName"));
    String line = reader.readLine();
    while (line!=null) {
        System.out.println("The Line is: "+line);

        // do calculations with line

         line = reader.readLine();
    }
} catch (Exception e) {
} finally {
}

Read the first line: String line = reader.readLine(); Checks if the line is null or not. If it is not continue: while (line!=null) { Then inside the loop you do the calculations.

AT THE END OF THE LOOP you read the next line, and check if it is null or not: while (line!=null) { . If it is not null continue.

This code must not be altered. The LAST command of the while loop must be the readLine. Because after that you go to the top, check the line and continue with the loop only if it is not null. If you read the line in the while you must not do any more calculations, because you haven't checked the new line. You need to go to the top. That's why it is the last command. And it must always executed.

while (line!=null) {
        //ALL CALCULATIONS ARE DONE HERE WITH THE LINE        

         
         //YOU GET THE NEXT LINE ONLY HERE AND GO TO THE TOP TO CHECK IT
         line = reader.readLine();
    }

With this code:

while (line != null ){
                //System.out.println(test);
                if (test.equals("")){
                    test=read.readLine();
                }
                else{
                    line[i]=test;
					i++;
                    System.out.println(line[i]);
                }
			}

The new line is read only inside the if statement. So if it is not "" you will put it inside the array, but not read again the next one. So you will not have a new value and the while will be executed with the old value. So since the "test" value hasn't changed again you will not inside the "if" and you will never read a new line meaning the while loop will never end.

Also another mistake is that at the while-loop you check the array not the line "test" that you read: while (line != null ) test=read.readLine(); So the while will always return true and it will never end even if you had the code inside correct.

Also you need to check if the "i" index exceeds the length of the array:

line[i]=test;
i++;

Because if you have too many lines the "i" will have value greater than 205.

And finally since you did this: while (line != null ) test=read.readLine(); it shows that you made no effort to understand the code provided since you completely ruined it. In my example it was clear that at the while you put the line to make sure that what you read is not null and the file hasn't finished. You went and put the array. Where in my example did I declare an array and put it at the while? It was the line read at the while. Can't you even copy correctly that code?

String line = reader.readLine();
    while (line!=null) {

Thank you very much sir I really appreciate your help but I was trying to play with it i know i made some mistake but I did not wanted to copy your code so I tried to write a similar one I am so confused I have a exam coming and professor said he will ask us to write a code to manipulate arrays selection sort and I dont know anything neither he taught anything I know what I did was stupid but I am learning and again really appreciate your help...

[code=java]package Test;
import java.io.*;
public class MainTest{

	public static void main(String[] args){

		String [] LineArray=new String[201];
		BufferedReader read = null;
		try
		{
			read = new BufferedReader(new FileReader("filename"));
			String lineRead=read.readLine();
            //lineRead=lineRead.split(".");
			int i=0;

			while (!(lineRead.equals(null)) && (i<LineArray.length))//lineRead !=null did not work
			{
				//System.out.println(lineRead);
				LineArray[i]=lineRead;
				i++;
				lineRead=read.readLine();
			}for(int counter=0;counter<LineArray.length;counter++)
				System.out.println(LineArray[counter]);
		}catch (IOException e)
		{
			System.out.println(e.getMessage());
		}finally
		{
			try
			{
				if (!(read.equals (null))) read.close();
			}catch (IOException e)
			{
				System.out.println(e.getMessage());
			}
		}
	}
}

here I fixed it now, now I have to break each entry in three parts ranking name and sports

Why do you say that this didn't work:

while (!(lineRead.equals(null)) && (i<LineArray.length))
//lineRead !=null did not

If lineRead is null you will get an Exception.
Why this thing doesn't work and what problems do you have?

while ( (lineRead!=null) && (i<LineArray.length) ) {

}

Also the same applies to this:

finally {
....
   if ( read!=null) read.close();
....
}

By the way, this was smart:
while ( (lineRead!=null) && (i<LineArray.length) )

Now you will never exceed the length of the array. But I would like to suggest that at the for-loop to use the i as max index:

Instead of this: for(int counter=0;counter<LineArray.length;counter++) Use this:

for(int counter=0; counter<i; counter++)

Because if the file has less lines let's say 10, only the first 10 elements of the array will have values. The rest will be empty (null).
So you will need to loop only till you reach the number of lines that were inserted in the array, which is the "i" value.

Now that you have the array ready, use the suggestions in my previous post on how to use the "subString" and "indexOf" methods on order to get what you want

well about the counter and for loop I was just checking if everything is going in the array or not I will remove it when my actual goal is achieved.

now can you help me determining how to split this array and save the ranking and name and sports in separate arrays.

First of all loop through the existing array like you are already doing and try to "extract" the 3 values from each element. Write separate methods for that.After you are successful you can create 3 arrays and put these inside with each loop:

String [] name = new String[i]; //remember the "i" ?
....
for(int counter=0;counter<i;counter++) {
 System.out.println(LineArray[counter]);
 
  name[i] = getName(LineArray[counter]);
 ...
}

The getName(LineArray[counter]); will take as argument the element of the array and return the name. You will have other methods for the "sport" and the "rank".

The best way is to create an object "Athlete" with attributes name, rank, sport and have a method that takes as argument the element of the array and return that object. Then you will put that in an array of "Athlete" objects. If you are not familiar with that leave for now

First of all loop through the existing array like you are already doing and try to "extract" the 3 values from each element. Write separate methods for that.After you are successful you can create 3 arrays and put these inside with each loop:

String [] name = new String[i]; //remember the "i" ?
....
for(int counter=0;counter<i;counter++) {
 System.out.println(LineArray[counter]);
 
  name[i] = getName(LineArray[counter]);
 ...
}

The getName(LineArray[counter]); will take as argument the element of the array and return the name. You will have other methods for the "sport" and the "rank".

The best way is to create an object "Athlete" with attributes name, rank, sport and have a method that takes as argument the element of the array and return that object. Then you will put that in an array of "Athlete" objects. If you are not familiar with that leave for now

It certainly looks difficult to me but I can give it a try and if I had any problem I will post here ... by the ways thanks for helping so far I really appreciate what I am learning through you...

public int Rank(array[counter])
	{
		String ArrayEntry=line[counter];
		int lineCounter=0;
		String tempRank="";

		while(!ArrayEntry[lineCounter].equals("."))
		{
			tempRank+=ArrayEntry[lineCounter];
			lineCounter++;
		}
		int rank = Integer.valueOf(tempRank).intValue();
		return rank;
	}

I tried this as a method to retrieve rank from the line but it is giving me bunch of errors
help me out i know it wont work without array but tell me if my syntax and logic is correct

ok I admit its wrong because according to my problem it wont work for name and sports
it will become complicated but do comment on this method

This is how you declare methods:

public static int getRank(String line) {
  int rank;
......
  return rank;
}

And call it:

String [] name = new String[i]; //remember the "i" ?
int [] rank= new rank[i]; //remember the "i" ?
....
for(int counter=0;counter<i;counter++) {
 System.out.println(LineArray[counter]);
 
  rank[i] = getRank(LineArray[counter]);
 ...
}

The LineArray[counter] is a String that has the current line from the for-loop. It is that line that you enter as a parameter to the method in order to get the rank. Inside the method you will deal only with the argument, because it has the value that you pass it when you call the method:

The argument "line" has the value of the argument when you call it. When you call it like this: getRank(LineArray[counter]); "line" has the value of: "LineArray[counter]" . So inside the method you will deal only with local "line" variable of the method.

public static int getRank(String line) {
  int rank;
......
  return rank;
}

I have already told you which methods to use from the String class and how to use them

public static int Rank(String line)
	{
        String [] ArrayEntry=new String[200];
		ArrayEntry[0]=line;
		int lineCounter=0;
		String tempRank="";

		while(!ArrayEntry[lineCounter].equals("."))
		{
			tempRank+=ArrayEntry[lineCounter];
			lineCounter++;
		}
		int rank = Integer.valueOf(tempRank).intValue();
		return rank;
	}

I am totally confused now its saying no error but not working

i tried something similar in python it worked for me now its not working for me here is what i triend in python

def main():
    p=["12. My name is Furqan by Awesome Publisher"]
    name=p[0]
    i=0
    temp=""
    while (name[i]!="."):
        temp=temp+name[i]
        i+=1
    print temp
main()

it worked for me but is not working in java may be its different way to do it in java

Because you haven't converted correctly the code.
You put the line in the ArrayEntry[0] and the rest of its values are null.
You check the ArrayEntry[0] if it is "." which is not because it contains the entire line.

You don't need the array, the line's value is stored in the parameter. That parameter alone you need to work with.
And you stilled haven't check my suggestions about the String class. Why don't you take a look at it

I am considering your suggestion but I am going step by step first I am trying to learn how to handle arrays and how to handle the entries of array then I will surely go for Object Orientation.

This question has already been answered. Start a new discussion instead.