473,399 Members | 3,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

readLine()

281 100+
Hi..can I ask one question about readLine()?
By having such below codes, I can get it to read first 200 lines out of 500 lines in an output file.
Expand|Select|Wrap|Line Numbers
  1. for(int ind=0; ind<200;ind++)
  2.             {                
  3.                 String lineA = "";
  4.                 lineA = inputA.readLine(); 
  5.  
How to start readLine() at line number 200 until 500 then?
Kindly please advisme how .....Thank you in advance.
May 17 '07 #1
37 7561
prometheuzz
197 Expert 100+
Hi..can I ask one question about readLine()?
By having such below codes, I can get it to read first 200 lines out of 500 lines in an output file.
Expand|Select|Wrap|Line Numbers
  1. for(int ind=0; ind<200;ind++)
  2.             {                
  3.                 String lineA = "";
  4.                 lineA = inputA.readLine(); 
  5.  
How to start readLine() at line number 200 until 500 then?
Kindly please advisme how .....Thank you in advance.
By just ignoring the lines 1 to 200. Also be sure that there are indeed 500 lines in your buffer/text file!

Expand|Select|Wrap|Line Numbers
  1. int numOfLines = 500;
  2.  
  3. for(int i = 1; i <= numOfLines && inputA.hasNextLine(); i++) {
  4.   String line = inputA.nextLine();
  5.   if(i > 200) {
  6.     // your code
  7.   }
  8. }
May 17 '07 #2
shana07
281 100+
By just ignoring the lines 1 to 200. Also be sure that there are indeed 500 lines in your buffer/text file!

Expand|Select|Wrap|Line Numbers
  1. int numOfLines = 500;
  2.  
  3. for(int i = 1; i <= numOfLines && inputA.hasNextLine(); i++) {
  4.   String line = inputA.nextLine();
  5.   if(i > 200) {
  6.     // your code
  7.   }
  8. }
Thank you very much. It's working fine.
At first I was using BufferedReader, so I changed it to Scanner...
Eventho I have 2 output files A & B, it works for both files by putting your codes...and that's what I want..tq again.
May 17 '07 #3
prometheuzz
197 Expert 100+
Thank you very much. It's working fine.
At first I was using BufferedReader, so I changed it to Scanner...
Eventho I have 2 output files A & B, it works for both files by putting your codes...and that's what I want..tq again.
Good to hear that, and you're welcome.
May 17 '07 #4
shana07
281 100+
May I ask this one question...Why the execution doesn't stop?
And it stops after executed only a file...I have 40 files need to read and
print out...Thank you in advance.
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. public class DeleteFile
  4.     public static void main(String[] args) throws IOException
  5.     {        
  6.        for (int m=1; m<=40; m++)  
  7.        {          
  8.              makeOutFile (m);          
  9.        }        
  10.     }
  11.  
  12.    public static void makeOutFile(int m) throws IOException 
  13.      {
  14.          PrintWriter printWriter = null; 
  15.          String lineA = "";  //have to skip the next line
  16.          String lineB = "";  
  17.  
  18.          BufferedReader inputFIRST = new BufferedReader (new FileReader( m + "akbk.txt")); 
  19.             lineA = inputFIRST.readLine();   
  20.             lineB = inputFIRST.readLine();
  21.  
  22.             while(lineA != null) 
  23.             {  
  24.                 printWriter = new PrintWriter( new FileWriter( m + "delAKBK.txt", true));
  25.                 printWriter.println(lineA);   //only print one line
  26.  
  27.                 lineA = inputFIRST.readLine();   
  28.                 lineB = inputFIRST.readLine();
  29.  
  30.                 printWriter.close();
  31.             }
  32.  
  33.             inputFIRST.close();
  34.  
  35.     } 
  36. }
May 18 '07 #5
prometheuzz
197 Expert 100+
May I ask this one question...Why the execution doesn't stop?
And it stops after executed only a file...I have 40 files need to read and
print out...Thank you in advance.
Expand|Select|Wrap|Line Numbers
  1.  ...
  2.  
  3.     while(lineA != null) 
  4.     {
  5.         ...
  6.     }
  7. }
If it doesn't stop looping, lineA will probably never become null.
But could you explain what it is you're trying to do? Perhaps I (or someone else) can suggest another way to achieve it: because by only looking at your code, I'm not sure what it is you're trying to do.
May 18 '07 #6
prometheuzz
197 Expert 100+
Have a look at this little demo:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. public class DeleteFile {
  4.  
  5.     private static File[] getFilesEndingWith(File directory, final String end) {
  6.         return directory.listFiles(new FileFilter() {
  7.             public boolean accept(File file) {
  8.                 return file.getName().endsWith(end);
  9.             }
  10.         });
  11.     }
  12.  
  13.     private static void doSomethingWith(File file) {
  14.         System.out.println("Do something with: "+file);
  15.     }
  16.  
  17.     public static void main(String[] args) throws IOException {
  18.  
  19.         // Read all files ending with 'akbk.txt' from C:\Temp
  20.         File[] files = getFilesEndingWith(new File("C:\\Temp"), "akbk.txt");
  21.  
  22.         // Now do something with those files
  23.         for(File f : files) {
  24.             doSomethingWith(f);
  25.         }
  26.     }
  27. }
May 18 '07 #7
shana07
281 100+
Have a look at this little demo:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. public class DeleteFile {
  4.  
  5.     private static File[] getFilesEndingWith(File directory, final String end) {
  6.         return directory.listFiles(new FileFilter() {
  7.             public boolean accept(File file) {
  8.                 return file.getName().endsWith(end);
  9.             }
  10.         });
  11.     }
  12.  
  13.     private static void doSomethingWith(File file) {
  14.         System.out.println("Do something with: "+file);
  15.     }
  16.  
  17.     public static void main(String[] args) throws IOException {
  18.  
  19.         // Read all files ending with 'akbk.txt' from C:\Temp
  20.         File[] files = getFilesEndingWith(new File("C:\\Temp"), "akbk.txt");
  21.  
  22.         // Now do something with those files
  23.         for(File f : files) {
  24.             doSomethingWith(f);
  25.         }
  26.     }
  27. }
Thank you bro for your reply. I tried your methods and happened the same..
So now I knew why it doesn't stop the execution and just produce one file. The file contains too many files (500K++) and imagine have 40 files...can't wait it to be done all files one short. So I have to do it one by one...so muchhhh quicker.
Doesn't matter....

Okay..may I ask one question to you about File?
Actually I am confused how to use RandomAccessFile..or maybe you can help me to explain what does it for? If I have to read/open an image file, let's say JPEG file, so can I use RandomAccessFile instead of InputOutputStream/FileReader?

Kindly please share some info with me...thank you very much
May 19 '07 #8
prometheuzz
197 Expert 100+
...

Okay..may I ask one question to you about File?
Actually I am confused how to use RandomAccessFile..or maybe you can help me to explain what does it for? If I have to read/open an image file, let's say JPEG file, so can I use RandomAccessFile instead of InputOutputStream/FileReader?
...
Depends what it is you want to do with whose JPEG files. So my question to you is: what are you trying to achieve?
May 19 '07 #9
shana07
281 100+
Depends what it is you want to do with whose JPEG files. So my question to you is: what are you trying to achieve?
I just want to open the JPEG file and check how big it is and see the pixels...
May 19 '07 #10
prometheuzz
197 Expert 100+
I just want to open the JPEG file and check how big it is and see the pixels...
Err, see the pixels? You mean you want to display the image? Then you don't need the java.io package but rather the java.awt package.
Have a look at this tutorial:
http://java.sun.com/docs/books/tutor...ges/index.html

Good luck.
May 19 '07 #11
JosAH
11,448 Expert 8TB
Err, see the pixels? You mean you want to display the image? Then you don't need the java.io package but rather the java.awt package.
Have a look at this tutorial:
http://java.sun.com/docs/books/tutor...ges/index.html

Good luck.
Nice how a simple "readLine" thread can grow out of hand: before you know it
the image has to come from an RDBMS reading BLOBS over a network
connection and it must be displayed in a browser on a cell phone ;-)

kind regards,

Jos
May 19 '07 #12
prometheuzz
197 Expert 100+
Nice how a simple "readLine" thread can grow out of hand: before you know it
the image has to come from an RDBMS reading BLOBS over a network
connection and it must be displayed in a browser on a cell phone ;-)

kind regards,

Jos
Yeah, I was thinking the same thing!
; )
May 19 '07 #13
shana07
281 100+
you guys cool ! :) :)
I am back to ask readLine() then......Before that thank you for giving me some guidance about reading image file...appreciate it.

Kindly please take a look at my code below:
Expand|Select|Wrap|Line Numbers
  1. public static void main(String[] args) throws IOException {        
  2.         for (int m=1; m<=40; m++)  {          
  3.                    makeOutFile (m);          
  4.              }         
  5.         }
  6.  
  7. public static void makeOutFile(int m) throws IOException 
  8.  {     
  9.     String lineA = "";
  10.     String lineB = "";
  11.     PrintWriter printWriter;
  12.  
  13.     try
  14.     {   
  15.       BufferedReader inputA = new BufferedReader (new FileReader("OutputA" + m + ".txt"));
  16.       BufferedReader inputB = new BufferedReader (new FileReader("OutputB" + m+ ".txt"));
  17.  
  18.        lineA = inputA.readLine();
  19.        lineB = inputB.readLine();
  20.  
  21.        int testnum = 1;
  22.        int counterF = 1;
  23.  
  24.             while(lineA != null && lineB != null)
  25.             {
  26.     printWriter = new PrintWriter( new FileWriter( "Result" + m + ".txt", true));
  27.  
  28.               String a = lineA;
  29.               String b = lineB;             
  30.  
  31.               if(a == b)  //Is this correct way to compare 2 lines that have BigInteger numbers?
  32.               {
  33.                  printWriter.println("   EQUAL  " +testnum); 
  34.  
  35.               }
  36.                else
  37.                {
  38.                     printWriter.println("   UNEQUAL  " +testnum); 
  39.  
  40.                     printWriter.println(counterF);
  41.                     counterF++;   //hoping it to count only for unequal 
  42.                }
  43.             testnum++;
  44.  
  45.             lineA = inputA.readLine();   
  46.             lineB = inputB.readLine();
  47.  
  48.  
  49.             printWriter.close();
  50.             }
  51.  
  52.  
  53.             inputA.close();
  54.             inputB.close();
  55.  
  56.           } catch (IOException e) 
  57.            {
  58.               System.out.println("There was a problem creating/writing to the temp file");
  59.               e.printStackTrace();
  60.            }              
  61.  
  62.     } 
1. Something is wrong with my comparison method..it's giving all "UNEQUAL" result. There are equals value too in the both input files..
Here are the sample input files.
InputA:
18088471895755763384595664405566364596733005
18823316190632083835
....//700 lines all together

InputB
18088471895755763384595664405566364596733005
59074062611518823316190632083835
....//700 lines all together

2. I want it to count how many unequal lines then....I know I did wrong there, but just don't know how to make it works....

Kindly please teach me guys...thank you in advance.
May 20 '07 #14
JosAH
11,448 Expert 8TB
Don't compare two Strings for equality using the '==' operaror. Use the member
method equals() instead; e.g. if two distinct Strings a and b both point to two
distinct String values "foo" a == b will return false while a.equals(b) returns true.

kind regards,

Jos
May 20 '07 #15
prometheuzz
197 Expert 100+
...

if(a == b) //Is this correct way to compare 2 lines that have BigInteger numbers?

...
No. Only primitives can be compared for equality with the '==' operator. When comparing Objects, you should use their equals(...) method:
Expand|Select|Wrap|Line Numbers
  1. if(a.equals(b)) {
  2.   // ...
  3. }
May 20 '07 #16
shana07
281 100+
Don't compare two Strings for equality using the '==' operaror. Use the member
method equals() instead; e.g. if two distinct Strings a and b both point to two
distinct String values "foo" a == b will return false while a.equals(b) returns true.

kind regards,

Jos
Thank you Jos...I am learning...
btw, why 'foo' is so sinonimn in java?
it's like so many examples about foo...why :)
May 20 '07 #17
shana07
281 100+
No. Only primitives can be compared for equality with the '==' operator. When comparing Objects, you should use their equals(...) method:
Expand|Select|Wrap|Line Numbers
  1. if(a.equals(b)) {
  2.   // ...
  3. }
Thank you prometheuzz...you guys responded at the same time...cool!.
the equals and counter work fine!
May 20 '07 #18
shana07
281 100+
I hope this would be my last question about file's....

Below is my input file (sample):

1 : 30 2
1 : 134 10
2 : 35 2
4 : 30 2
4 : 80 8
..../many more lines

How to delete the same fileno (fileno refers to the first array in line) and take only the first line for the same fileno?
Something like this:

if filenoLine1 == filenoLine2 {
print fileno1
else
print filenoLine1
print filenoLine2


So I will write and get new output file like this:

1 : 30 2
2 : 35 2
4 : 30 2

How's the syntax looks like yeah?

Expand|Select|Wrap|Line Numbers
  1. while((lineFRST != null) && (lineSEC != null))  
  2. {    
  3.     printWriter = new PrintWriter( new FileWriter("OutputFile.txt", true));
  4.  
  5.     String[] thelineFRST = lineFRST.split(" ");
  6.     String fileNo1 = thelineFRST[0];
  7.  
  8.     String[] thelineSEC = lineSEC.split(" ");
  9.     String fileNo2 = thelineSEC[0];
  10.  
  11.             if(fileNo1.equals(fileNo2))  
  12.             {
  13.                printWriter.println(lineFRST); 
  14.             }
  15.             else
  16.             {  
  17.                 printWriter.println(lineFRST); 
  18.                 printWriter.println(lineSEC); 
  19.             }
  20.  
  21.             printWriter.close();
  22.  
  23.             lineFRST = in1.readLine(); 
  24.             lineSEC = in1.readLine(); 
  25.         }  
  26.  
  27.          in1.close(); 
Problem here is - Read from one file, read 2 lines one short, compare then move them to next two lines...I will miss some equal lines if keep moving two lines one short...again kindly teach me...thank you
May 20 '07 #19
prometheuzz
197 Expert 100+
Thank you Jos...I am learning...
btw, why 'foo' is so sinonimn in java?
it's like so many examples about foo...why :)
Have a look at this wiki page:
http://en.wikipedia.org/wiki/Foo
May 20 '07 #20
shana07
281 100+
Have a look at this wiki page:
http://en.wikipedia.org/wiki/Foo
Now I got it why....tq
May 20 '07 #21
I hope this would be my last question about file's....

Below is my input file (sample):

1 : 30 2
1 : 134 10
2 : 35 2
4 : 30 2
4 : 80 8
..../many more lines

How to delete the same fileno (fileno refers to the first array in line) and take only the first line for the same fileno?
Something like this:

if filenoLine1 == filenoLine2 {
print fileno1
else
print filenoLine1
print filenoLine2


So I will write and get new output file like this:

1 : 30 2
2 : 35 2
4 : 30 2

How's the syntax looks like yeah?

Expand|Select|Wrap|Line Numbers
  1. while((lineFRST != null) && (lineSEC != null))  
  2. {    
  3.     printWriter = new PrintWriter( new FileWriter("OutputFile.txt", true));
  4.  
  5.     String[] thelineFRST = lineFRST.split(" ");
  6.     String fileNo1 = thelineFRST[0];
  7.  
  8.     String[] thelineSEC = lineSEC.split(" ");
  9.     String fileNo2 = thelineSEC[0];
  10.  
  11.             if(fileNo1.equals(fileNo2))  
  12.             {
  13.                printWriter.println(lineFRST); 
  14.             }
  15.             else
  16.             {  
  17.                 printWriter.println(lineFRST); 
  18.                 printWriter.println(lineSEC); 
  19.             }
  20.  
  21.             printWriter.close();
  22.  
  23.             lineFRST = in1.readLine(); 
  24.             lineSEC = in1.readLine(); 
  25.         }  
  26.  
  27.          in1.close(); 
Problem here is - Read from one file, read 2 lines one short, compare then move them to next two lines...I will miss some equal lines if keep moving two lines one short...again kindly teach me...thank you
Is this possible to achieve?
May 21 '07 #22
prometheuzz
197 Expert 100+
Is this possible to achieve?
Of course it is, but how you're trying to do it, is not the best way to do so.
IMO it's better to first read all the lines from your textfile in an array, and the analyse that array afterwards. Don't do these two things at the same time: a method should only be responsible for one thing!
Here's some pseudo code fo you to work on:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.  
  6.     private static void analyseLines(String[] lines) {
  7.         LOOP from 'i' <- 0 to 'lines.length'-1, each iteration increase 'i' by 2
  8.             String 'current' <- the first part of the element at 'lines[i]'
  9.             String 'next' <- the first part of the element at 'lines[i+1]'
  10.             IF 'current' is the same as 'next'
  11.                 print 'lines[i]'
  12.             ELSE
  13.                 print both 'lines[i]' and 'lines[i+1]' 
  14.             END IF
  15.         END LOOP
  16.     }
  17.  
  18.     private static String[] readLines(String fileName) throws IOException {
  19.         List lines <- new dynamical list
  20.         // see: http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html
  21.  
  22.         WHILE 'fileNames' has more lines
  23.             lines.add(line from 'fileNames')
  24.         END WHILE
  25.         return 'lines' as an array of Strings
  26.     }
  27.  
  28.     public static void main(String[] args) throws IOException {
  29.         String[] lines = readLines("/path/to/your/textfile");
  30.         analyseLines(lines);
  31.     }
  32. }
Good luck.
May 21 '07 #23
shana07
281 100+
Of course it is, but how you're trying to do it, is not the best way to do so.
IMO it's better to first read all the lines from your textfile in an array, and the analyse that array afterwards. Don't do these two things at the same time: a method should only be responsible for one thing!
Here's some pseudo code fo you to work on:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.  
  6.     private static void analyseLines(String[] lines) {
  7.         LOOP from 'i' <- 0 to 'lines.length'-1, each iteration increase 'i' by 2
  8.             String 'current' <- the first part of the element at 'lines[i]'
  9.             String 'next' <- the first part of the element at 'lines[i+1]'
  10.             IF 'current' is the same as 'next'
  11.                 print 'lines[i]'
  12.             ELSE
  13.                 print both 'lines[i]' and 'lines[i+1]' 
  14.             END IF
  15.         END LOOP
  16.     }
  17.  
  18.     private static String[] readLines(String fileName) throws IOException {
  19.         List lines <- new dynamical list
  20.         // see: http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html
  21.  
  22.         WHILE 'fileNames' has more lines
  23.             lines.add(line from 'fileNames')
  24.         END WHILE
  25.         return 'lines' as an array of Strings
  26.     }
  27.  
  28.     public static void main(String[] args) throws IOException {
  29.         String[] lines = readLines("/path/to/your/textfile");
  30.         analyseLines(lines);
  31.     }
  32. }
Good luck.
Could you please take a look at my code? There are 3 errors still:

Expand|Select|Wrap|Line Numbers
  1. public class FinalSort
  2. {  
  3.      private static void analyseLines(String[] lines, int m)
  4.      {
  5.         PrintWriter printWriter = null;    
  6.         BufferedWriter bufferWriter = null;
  7.  
  8.   printWriter = new PrintWriter(new BufferedWriter(new FileWriter("C:\\result\\" + m +"Final.txt", true)));   //To get back 40 output files: eg: 1Final.txt       
  9.  
  10.          for (int i=0; i < lines.length-1; i+=2)  {         
  11.              String pos = lines[0];
  12.              String[] current = pos[i];     //array required but string found        
  13.              String[] next = pos[i+1];   //array required but string found        
  14.  
  15.              if(current.equals(next)) {         
  16.      printWriter.println (lines[i]);    }                     
  17.              else {
  18.                  printWriter.println (lines[i]);  
  19.              printWriter.println (lines[i+1]); 
  20.  }    
  21.          }         
  22.           }
  23.  
  24.  
  25.      private static String[] readLines (String fileName) throws IOException
  26.      {
  27.          List lines = new ArrayList();
  28.  
  29.          while (fileName != null)
  30.          {
  31.              lines.add (fileName); 
  32.          }         
  33.          return lines;  //How to return 'lines' as an array of Strings?
  34.      }
  35.  
  36.  
  37.      public static void main(String[] args) throws IOException
  38.     {        
  39.      for (int m=1; m<=40; m++)  // there are 40 files need to be analysed - eg: 1ResultFile.txt
  40.  {                        
  41.                String[] lines = readLines (m + "ResultFile.txt");     
  42.                analyseLines (lines, m);   
  43.          }         
  44.     }
  45.  
  46. }   
May 21 '07 #24
prometheuzz
197 Expert 100+
First get your readLines method working. Only then (try to) implement the analyseLines(...) method.


Expand|Select|Wrap|Line Numbers
  1. ...
  2.      private static String[] readLines (String fileName) throws IOException
  3.      {
  4.          List lines = new ArrayList();
  5.  
  6.          while (fileName != null)
  7.          {
  8.              lines.add (fileName); 
  9.          }         
  10.  ...
  11.  
This is not how you read the lines of a text file. You managed to read lines from a textfile before haven't you?


Expand|Select|Wrap|Line Numbers
  1. ...
  2.          return lines;  //How to return 'lines' as an array of Strings?
  3.      }
  4.  
Have a look at the API docs of the List class I posted earlier. There is a method in there that does just that.
May 21 '07 #25
shana07
281 100+
First get your readLines method working. Only then (try to) implement the analyseLines(...) method.




This is not how you read the lines of a text file. You managed to read lines from a textfile before haven't you?




Have a look at the API docs of the List class I posted earlier. There is a method in there that does just that.
Thank you..
Why it's giving error - can't find method toArray(java.util.list)?
Expand|Select|Wrap|Line Numbers
  1. private static String[] readLines (String fileName, int m) throws IOException
  2.      {
  3.          List lines = new ArrayList();
  4.  
  5.         String lineFRST = "";
  6.         String lineSEC = "";
  7.  
  8.   BufferedReader in1 = new BufferedReader (new FileReader( m + "FileInput.txt")); 
  9.  
  10.         lineFRST = in1.readLine(); 
  11.         lineSEC = in1.readLine();   
  12.  
  13.          while (fileName != null)
  14.          {
  15.                lines.add (lineFRST); 
  16.                 lines.add(lineSEC);
  17.          }
  18.  
  19.          return toArray (lines);       //is this correct?
  20.      }
May 21 '07 #26
prometheuzz
197 Expert 100+
Thank you..
Why it's giving error - can't find method toArray(java.util.list)?

....

return toArray (lines); //is this correct?
}[/code]
Because there is no method called toArray(...) in your class. You should call that method on your 'lines' reference.


...

Expand|Select|Wrap|Line Numbers
  1. private static String[] readLines (String fileName, int m) throws IOException
  2.      {
  3.          List lines = new ArrayList();
  4.  
  5.         String lineFRST = "";
  6.         String lineSEC = "";
  7.  
  8.   BufferedReader in1 = new BufferedReader (new FileReader( m + "FileInput.txt")); 
  9.  
  10.         lineFRST = in1.readLine(); 
  11.         lineSEC = in1.readLine();   
  12.  
  13.          while (fileName != null)
  14.          {
  15.                lines.add (lineFRST); 
  16.                 lines.add(lineSEC);
  17.          }
  18.     ...
  19.      }
No: do NOT read two lines at a time. Just read one at a time and add that one line in your List.
May 21 '07 #27
shana07
281 100+
Because there is no method called toArray(...) in your class. You should call that method on your 'lines' reference.




No: do NOT read two lines at a time. Just read one at a time and add that one line in your List.
Expand|Select|Wrap|Line Numbers
  1. private static String[] readLines (String fileName, int m) throws IOException
  2.      {
  3.          List lines = new ArrayList();
  4.  
  5.          String lineFRST = "";
  6.         String lineSEC = "";
  7.  
  8.         BufferedReader in1 = new BufferedReader (new FileReader( m + "FileInput.txt")); 
  9.  
  10.         lineFRST = in1.readLine();        //Okay?
  11.  
  12.          while (fileName != null)
  13.          {
  14.              lines.add (lineFRST);                
  15.          }
  16.  
  17.          return (String[]) lines.toArray();   //Okay?           
  18.      }
Please teach me what's next...TQ
May 21 '07 #28
prometheuzz
197 Expert 100+
Expand|Select|Wrap|Line Numbers
  1. private static String[] readLines (String fileName, int m) throws IOException
  2.      {
  3.          List lines = new ArrayList();
  4.  
  5.          String lineFRST = "";
  6.         String lineSEC = "";
  7.  
  8.         BufferedReader in1 = new BufferedReader (new FileReader( m + "FileInput.txt")); 
  9.  
  10.         lineFRST = in1.readLine();        //Okay?
  11.  
  12.          while (fileName != null)
  13.          {
  14.              lines.add (lineFRST);                
  15.          }
  16.  
  17.          return (String[]) lines.toArray();   //Okay?           
  18.      }
Please teach me what's next...TQ
Whether it OK or not can be easily asked to your compiler.
First check if your readLines(...) method works by printing the array your method returns like this:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.  
  6.     private static String[] readLines(String fileName, int m) throws IOException {
  7.         List lines = new ArrayList();        
  8.         String lineFRST = "";
  9.         BufferedReader in1 = new BufferedReader(new FileReader(m+"FileInput.txt"));
  10.         lineFRST = in1.readLine();
  11.         while(fileName != null) {
  12.             lines.add(lineFRST);               
  13.         }
  14.         return (String[])lines.toArray();      
  15.     }
  16.  
  17.     public static void main(String[] args) throws IOException {
  18.         String[] lines = readLines("/path/to/your/textfile", 1);
  19.         for(int i = 0; i < lines.length; i++) {
  20.             System.out.println("lines["+i+"] = "+lines[i]);
  21.         }
  22.     }
  23. }
May 21 '07 #29
shana07
281 100+
Whether it OK or not can be easily asked to your compiler.
First check if your readLines(...) method works by printing the array your method returns like this:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.  
  6.     private static String[] readLines(String fileName, int m) throws IOException {
  7.         List lines = new ArrayList();        
  8.         String lineFRST = "";
  9.         BufferedReader in1 = new BufferedReader(new FileReader(m+"FileInput.txt"));
  10.         lineFRST = in1.readLine();
  11.         while(fileName != null) {
  12.             lines.add(lineFRST);               
  13.         }
  14.         return (String[])lines.toArray();      
  15.     }
  16.  
  17.     public static void main(String[] args) throws IOException {
  18.         String[] lines = readLines("/path/to/your/textfile", 1);
  19.         for(int i = 0; i < lines.length; i++) {
  20.             System.out.println("lines["+i+"] = "+lines[i]);
  21.         }
  22.     }
  23. }
Opss...you're right.
Exception in thread " main" java.lang.OutOfMemoryError: Java heap space...
What am I supposed to do then yeah.
May 21 '07 #30
shana07
281 100+
Actually total lines of this first file is just 97.
suppose shouldn't be any problem isn't...
May 21 '07 #31
prometheuzz
197 Expert 100+
Opss...you're right.
Exception in thread " main" java.lang.OutOfMemoryError: Java heap space...
What am I supposed to do then yeah.
This happens because you're in an infinite loop.
May 21 '07 #32
shana07
281 100+
This happens because you're in an infinite loop.
I have this to replace the while loop, it's giving me new run time error - Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; can not be cast to [Ljava.lang.String;
have a look and teach me please..

Expand|Select|Wrap|Line Numbers
  1. while (readable)
  2. {
  3. lineFRST = in1.readLine();
  4. if (lineFRST != null)
  5. {
  6.  lines.add(lineFRST);
  7.  readable = true;
  8. } else
  9.  readable = false;
  10. }
  11.   return (String[]) lines.toArray();
May 22 '07 #33
prometheuzz
197 Expert 100+
I have this to replace the while loop, it's giving me new run time error - Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; can not be cast to [Ljava.lang.String;
have a look and teach me please..

Expand|Select|Wrap|Line Numbers
  1. while (readable)
  2. {
  3. lineFRST = in1.readLine();
  4. if (lineFRST != null)
  5. {
  6. lines.add(lineFRST);
  7. readable = true;
  8. } else
  9. readable = false;
  10. }
  11. return (String[]) lines.toArray();

Try returning it like this:

Expand|Select|Wrap|Line Numbers
  1. return lines.toArray(new String[lines.size()]);
May 22 '07 #34
shana07
281 100+
Try returning it like this:

Expand|Select|Wrap|Line Numbers
  1. return lines.toArray(new String[lines.size()]);
I am really clueless about type error...please.

incompatible types
found : java.lang.Object[]
required: java.lang.String[]
return lines.toArray(new String[lines.size()]);
^

Expand|Select|Wrap|Line Numbers
  1. return lines.toArray(new String[lines.size()]); 
  2.           // return (String[]) lines.toArray();
May 22 '07 #35
prometheuzz
197 Expert 100+
I am really clueless about type error...please.

incompatible types
found : java.lang.Object[]
required: java.lang.String[]
return lines.toArray(new String[lines.size()]);
^

Expand|Select|Wrap|Line Numbers
  1. return lines.toArray(new String[lines.size()]); 
  2.           // return (String[]) lines.toArray();
That is probably because you didn't declare what type of objects you're storing in your java.util.List (Strings in your case). So, try it like this:

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.  
  6.     private static String[] readLines(String fileName, int m) throws IOException {
  7.         List<String> lines = new ArrayList<String>();       
  8.         String line;
  9.         BufferedReader in1 = new BufferedReader(new FileReader(m+fileName));
  10.         while((line = in1.readLine()) != null) {
  11.             lines.add(line);               
  12.         }
  13.         return lines.toArray(new String[lines.size()]);     
  14.     }
  15.  
  16.     public static void main(String[] args) throws IOException {
  17.         String[] lines = readLines("/path/to/your/textfile", 1);
  18.         for(int i = 0; i < lines.length; i++) {
  19.             System.out.println("lines["+i+"] = "+lines[i]);
  20.         }
  21.     }
  22. }
May 22 '07 #36
shana07
281 100+
That is probably because you didn't declare what type of objects you're storing in your java.util.List (Strings in your case). So, try it like this:

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.  
  6.     private static String[] readLines(String fileName, int m) throws IOException {
  7.         List<String> lines = new ArrayList<String>();       
  8.         String line;
  9.         BufferedReader in1 = new BufferedReader(new FileReader(m+fileName));
  10.         while((line = in1.readLine()) != null) {
  11.             lines.add(line);               
  12.         }
  13.         return lines.toArray(new String[lines.size()]);     
  14.     }
  15.  
  16.     public static void main(String[] args) throws IOException {
  17.         String[] lines = readLines("/path/to/your/textfile", 1);
  18.         for(int i = 0; i < lines.length; i++) {
  19.             System.out.println("lines["+i+"] = "+lines[i]);
  20.         }
  21.     }
  22. }
Yes, you are really there! It works great. Thank you very much prometheuzz
Just this line makes my program different...
Expand|Select|Wrap|Line Numbers
  1. List<String> lines = new ArrayList<String>();      
I can't think that complicated....
The next mission is how to compare them....
again please advise me....
May 22 '07 #37
prometheuzz
197 Expert 100+
Yes, you are really there! It works great. Thank you very much prometheuzz
Just this line makes my program different...
Expand|Select|Wrap|Line Numbers
  1. List<String> lines = new ArrayList<String>();      
I can't think that complicated....
The next mission is how to compare them....
again please advise me....
I don't know what you mean by "how to compare them", but you don't need to answer that straight away. First try to solve the next problem yourself, and if you don't succeed, post back here. Also post the code you're working on and explain exactly what it is you need help with.

Good luck.
May 22 '07 #38

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Russell E. Owen | last post by:
At one time, mixing for x in file and readline was dangerous. For example: for line in file: # read some lines from a file, then break nextline = readline() # bad would not do what a naive...
0
by: John C. Worsley | last post by:
I've got an extremely inscrutable problem here using Perl's Term::ReadLine::Gnu module. I'm using Perl 5.8.0, readline 4.3 and Term::ReadLine::Gnu 1.14. The problem is specific to catching INT...
1
by: Jian Qiu | last post by:
Hi, I tried to install python2.4.2. Unfortunately building 'readline' extension failed. Here is what I got: (It is a bit long. If you are impatient, please look at the end where it reports the...
4
by: David Bear | last post by:
I built python 2.4.2 for suse linux 9.3. I configured it to be a separate instance of python from the version packaged with suse. Now when I start it I get an error: python Python 2.4.2 (#4,...
3
by: PointMan | last post by:
what i know is... while ((currentStr = sr.ReadLine()) != null) { in this area, can i get next ReadLine Value of currentStr } best regard,,
0
by: 7stud | last post by:
Hi, 1) Does this make any sense: """ Thus, the loop: for line in f: iterates on each line of the file. Due to buffering issues,
6
by: Sean Davis | last post by:
I have a large file that I would like to transform and then feed to a function (psycopg2 copy_from) that expects a file-like object (needs read and readline methods). I have a class like so: ...
0
by: Akira Kitada | last post by:
Hi list, I was trying to build Python 2.6 on FreeBSD 4.11 and found it failed to build some of the modules. """ Failed to find the necessary bits to build these modules: _bsddb ...
0
by: Akira Kitada | last post by:
Hi Marc-Andre, Thanks for the suggestion. I opened a ticket for this issue: http://bugs.python.org/issue4204 Now I understand the state of the multiprocessing module, but it's too bad to see...
0
by: M.-A. Lemburg | last post by:
On 2008-10-25 20:19, Akira Kitada wrote: Thanks. The errors you are getting appear to be related to either some missing header files or a missing symbol definition to enable these - looking...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.