1

I am trying to read input from a text file(read only numbers in arraylist). My text file looks like this containing numbers(146,7,-1,-2,3).It looks like this actually: HEADER 1467-1-23

Now,when I put HEADER string in the file,my code would not read the first number which is 146 in my case and starts with the second number "7".However,when the string HEADER is not there,146 is also read which is obviously what I want.My code is this:

String pathToWrite="C:\\Users\\User\\Desktop\\Hello.txt";

 FileReader fr=new FileReader(pathToWrite);
     BufferedReader bufferedReader=new BufferedReader(fr);

     String aLine=null;
     int numberOfLine=0;
    List<Integer> list = new ArrayList<Integer>();
    List<Integer> list2 = new ArrayList<Integer>();


    while((aLine=bufferedReader.readLine()) != null )
     {
            if(numberOfLine > 1) //since header lies in first line,I want to skip that line
            {
             list.add(Integer.parseInt(aLine));
            }
             numberOfLine++;


     }

Want suggestions about how I can change the code to read the first number too? Thanks in advance

3
  • Do you mean the first line which is skipped ? Commented Oct 13, 2014 at 7:09
  • No,I mean how to read the first number in the list which is 146 in my case.The first line would be skipped as I am putting the if statement Commented Oct 13, 2014 at 7:11
  • From 1467-1-23 number - How do you make out that 146 is one number and 7 is another. Is the format fixed? For ex, I can say 146 is actually 3 numbers - 1, 4 and 6. Commented Oct 13, 2014 at 7:24

5 Answers 5

1

The file looks like:

HEADER
146
7
1
23

? Then try if(numberOfLine > 0). It's because you are beginning counting from zero.

Sign up to request clarification or add additional context in comments.

Comments

0

Change your if statment or numberOfLine=0; variable. You can do one of the following

Case 1:

int numberOfLine=0;

while((aLine=bufferedReader.readLine()) != null )
     {
            if(numberOfLine >= 1) //since header lies in first line,I want to skip that line
            {
             list.add(Integer.parseInt(aLine));
            }
             numberOfLine++;


     }

============================================================
Case 2:

int numberOfLine=1;

while((aLine=bufferedReader.readLine()) != null )
     {
            if(numberOfLine > 1) //since header lies in first line,I want to skip that line
            {
             list.add(Integer.parseInt(aLine));
            }
             numberOfLine++;


     }

Comments

0

Split your line using String's split function

String [] numbers = aLine.split(",");

This will give you all numbers in an array.

I hope it resolves your query !!!

Comments

0

Typical > vs. >= typo. Just change if(numberOfLine > 1) to if(numberOfLine >= 1).

Comments

0

The file looks like this?

HEADER 1467-1-23

if like this, give an example of second line. Because this is one line which id = 0; or like this?

HEADER
1467-1-23
...

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.