readLine()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prometheuzz
    Recognized Expert New Member
    • Apr 2007
    • 197

    #16
    Originally posted by shana07
    ...

    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:
    [CODE=java]if(a.equals(b)) {
    // ...
    }[/CODE]

    Comment

    • shana07
      Contributor
      • Jan 2007
      • 280

      #17
      Originally posted by JosAH
      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 :)

      Comment

      • shana07
        Contributor
        • Jan 2007
        • 280

        #18
        Originally posted by prometheuzz
        No. Only primitives can be compared for equality with the '==' operator. When comparing Objects, you should use their equals(...) method:
        [CODE=java]if(a.equals(b)) {
        // ...
        }[/CODE]
        Thank you prometheuzz...y ou guys responded at the same time...cool!.
        the equals and counter work fine!

        Comment

        • shana07
          Contributor
          • Jan 2007
          • 280

          #19
          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?

          Code:
          while((lineFRST != null) && (lineSEC != null))  
          {	
              printWriter = new PrintWriter( new FileWriter("OutputFile.txt", true));
                  	
              String[] thelineFRST = lineFRST.split(" ");
              String fileNo1 = thelineFRST[0];
                      
              String[] thelineSEC = lineSEC.split(" ");
              String fileNo2 = thelineSEC[0];
                     
                      if(fileNo1.equals(fileNo2))  
                      {
                         printWriter.println(lineFRST); 
                      }
                      else
                      {  
                      	printWriter.println(lineFRST); 
                      	printWriter.println(lineSEC); 
                      }
                              
                      printWriter.close();
                       
                      lineFRST = in1.readLine(); 
                      lineSEC = in1.readLine(); 
                  }  
                 
                   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

          Comment

          • prometheuzz
            Recognized Expert New Member
            • Apr 2007
            • 197

            #20
            Originally posted by shana07
            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:

            Comment

            • shana07
              Contributor
              • Jan 2007
              • 280

              #21
              Originally posted by prometheuzz
              Have a look at this wiki page:
              http://en.wikipedia.org/wiki/Foo
              Now I got it why....tq

              Comment

              • javaalien
                New Member
                • May 2007
                • 34

                #22
                Originally posted by shana07
                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?

                Code:
                while((lineFRST != null) && (lineSEC != null))  
                {	
                    printWriter = new PrintWriter( new FileWriter("OutputFile.txt", true));
                        	
                    String[] thelineFRST = lineFRST.split(" ");
                    String fileNo1 = thelineFRST[0];
                            
                    String[] thelineSEC = lineSEC.split(" ");
                    String fileNo2 = thelineSEC[0];
                           
                            if(fileNo1.equals(fileNo2))  
                            {
                               printWriter.println(lineFRST); 
                            }
                            else
                            {  
                            	printWriter.println(lineFRST); 
                            	printWriter.println(lineSEC); 
                            }
                                    
                            printWriter.close();
                             
                            lineFRST = in1.readLine(); 
                            lineSEC = in1.readLine(); 
                        }  
                       
                         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?

                Comment

                • prometheuzz
                  Recognized Expert New Member
                  • Apr 2007
                  • 197

                  #23
                  Originally posted by javaalien
                  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:
                  [CODE=java]import java.io.*;
                  import java.util.*;

                  public class Main {

                  private static void analyseLines(St ring[] lines) {
                  LOOP from 'i' <- 0 to 'lines.length'-1, each iteration increase 'i' by 2
                  String 'current' <- the first part of the element at 'lines[i]'
                  String 'next' <- the first part of the element at 'lines[i+1]'
                  IF 'current' is the same as 'next'
                  print 'lines[i]'
                  ELSE
                  print both 'lines[i]' and 'lines[i+1]'
                  END IF
                  END LOOP
                  }

                  private static String[] readLines(Strin g fileName) throws IOException {
                  List lines <- new dynamical list
                  // see: http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html

                  WHILE 'fileNames' has more lines
                  lines.add(line from 'fileNames')
                  END WHILE
                  return 'lines' as an array of Strings
                  }

                  public static void main(String[] args) throws IOException {
                  String[] lines = readLines("/path/to/your/textfile");
                  analyseLines(li nes);
                  }
                  }[/CODE]
                  Good luck.

                  Comment

                  • shana07
                    Contributor
                    • Jan 2007
                    • 280

                    #24
                    Originally posted by prometheuzz
                    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:
                    [CODE=java]import java.io.*;
                    import java.util.*;

                    public class Main {

                    private static void analyseLines(St ring[] lines) {
                    LOOP from 'i' <- 0 to 'lines.length'-1, each iteration increase 'i' by 2
                    String 'current' <- the first part of the element at 'lines[i]'
                    String 'next' <- the first part of the element at 'lines[i+1]'
                    IF 'current' is the same as 'next'
                    print 'lines[i]'
                    ELSE
                    print both 'lines[i]' and 'lines[i+1]'
                    END IF
                    END LOOP
                    }

                    private static String[] readLines(Strin g fileName) throws IOException {
                    List lines <- new dynamical list
                    // see: http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html

                    WHILE 'fileNames' has more lines
                    lines.add(line from 'fileNames')
                    END WHILE
                    return 'lines' as an array of Strings
                    }

                    public static void main(String[] args) throws IOException {
                    String[] lines = readLines("/path/to/your/textfile");
                    analyseLines(li nes);
                    }
                    }[/CODE]
                    Good luck.
                    Could you please take a look at my code? There are 3 errors still:

                    Code:
                    public class FinalSort
                    {  
                         private static void analyseLines(String[] lines, int m)
                         {
                            PrintWriter printWriter = null;    
                            BufferedWriter bufferWriter = null;
                         
                      printWriter = new PrintWriter(new BufferedWriter(new FileWriter("C:\\result\\" + m +"Final.txt", true)));   //To get back 40 output files: eg: 1Final.txt       
                         	
                         	for (int i=0; i < lines.length-1; i+=2)  {     	
                                 String pos = lines[0];
                                 String[] current = pos[i];     //array required but string found        
                                 String[] next = pos[i+1];   //array required but string found        
                         		
                         		if(current.equals(next)) {     	
                         printWriter.println (lines[i]);    }		     		
                         		else {
                         		    printWriter.println (lines[i]);  
                     		    printWriter.println (lines[i+1]); 
                     }	
                         	}     	
                              }
                         
                         
                         private static String[] readLines (String fileName) throws IOException
                         {
                         	List lines = new ArrayList();
                         	
                         	while (fileName != null)
                         	{
                         		lines.add (fileName); 
                         	}     	
                         	return lines;  //How to return 'lines' as an array of Strings?
                         }
                         
                     
                         public static void main(String[] args) throws IOException
                        {        
                         for (int m=1; m<=40; m++)  // there are 40 files need to be analysed - eg: 1ResultFile.txt
                     {                        
                                   String[] lines = readLines (m + "ResultFile.txt");     
                                   analyseLines (lines, m);   
                             }         
                        }
                    
                    }

                    Comment

                    • prometheuzz
                      Recognized Expert New Member
                      • Apr 2007
                      • 197

                      #25
                      First get your readLines method working. Only then (try to) implement the analyseLines(.. .) method.


                      Originally posted by shana07
                      Code:
                      ...
                           private static String[] readLines (String fileName) throws IOException
                           {
                               List lines = new ArrayList();
                               
                               while (fileName != null)
                               {
                                   lines.add (fileName); 
                               }         
                       ...
                      This is not how you read the lines of a text file. You managed to read lines from a textfile before haven't you?


                      Originally posted by shana07
                      Code:
                      ...
                               return lines;  //How to return 'lines' as an array of Strings?
                           }
                      Have a look at the API docs of the List class I posted earlier. There is a method in there that does just that.

                      Comment

                      • shana07
                        Contributor
                        • Jan 2007
                        • 280

                        #26
                        Originally posted by prometheuzz
                        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.ut il.list)?
                        Code:
                        private static String[] readLines (String fileName, int m) throws IOException
                             {
                             	List lines = new ArrayList();
                                
                                String lineFRST = "";
                                String lineSEC = "";
                                
                          BufferedReader in1 = new BufferedReader (new FileReader( m + "FileInput.txt")); 
                               
                                lineFRST = in1.readLine(); 
                                lineSEC = in1.readLine();   
                               
                             	while (fileName != null)
                             	{
                                       lines.add (lineFRST); 
                                        lines.add(lineSEC);
                             	}
                             	
                             	return toArray (lines);       //is this correct?
                             }

                        Comment

                        • prometheuzz
                          Recognized Expert New Member
                          • Apr 2007
                          • 197

                          #27
                          Originally posted by shana07
                          Thank you..
                          Why it's giving error - can't find method toArray(java.ut il.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.


                          Originally posted by shana07
                          ...

                          Code:
                          private static String[] readLines (String fileName, int m) throws IOException
                               {
                               	List lines = new ArrayList();
                                  
                                  String lineFRST = "";
                                  String lineSEC = "";
                                  
                            BufferedReader in1 = new BufferedReader (new FileReader( m + "FileInput.txt")); 
                                 
                                  lineFRST = in1.readLine(); 
                                  lineSEC = in1.readLine();   
                                 
                               	while (fileName != null)
                               	{
                                         lines.add (lineFRST); 
                                          lines.add(lineSEC);
                               	}
                          	...
                               }
                          No: do NOT read two lines at a time. Just read one at a time and add that one line in your List.

                          Comment

                          • shana07
                            Contributor
                            • Jan 2007
                            • 280

                            #28
                            Originally posted by prometheuzz
                            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.
                            Code:
                            private static String[] readLines (String fileName, int m) throws IOException
                                 {
                                 	List lines = new ArrayList();
                                    
                                 	String lineFRST = "";
                                    String lineSEC = "";
                                    
                                    BufferedReader in1 = new BufferedReader (new FileReader( m + "FileInput.txt")); 
                                   
                                    lineFRST = in1.readLine();        //Okay?
                                   
                                 	while (fileName != null)
                                 	{
                                 		lines.add (lineFRST);                
                                 	}
                                 	
                                 	return (String[]) lines.toArray();   //Okay?           
                                 }
                            Please teach me what's next...TQ

                            Comment

                            • prometheuzz
                              Recognized Expert New Member
                              • Apr 2007
                              • 197

                              #29
                              Originally posted by shana07
                              Code:
                              private static String[] readLines (String fileName, int m) throws IOException
                                   {
                                   	List lines = new ArrayList();
                                      
                                   	String lineFRST = "";
                                      String lineSEC = "";
                                      
                                      BufferedReader in1 = new BufferedReader (new FileReader( m + "FileInput.txt")); 
                                     
                                      lineFRST = in1.readLine();        //Okay?
                                     
                                   	while (fileName != null)
                                   	{
                                   		lines.add (lineFRST);                
                                   	}
                                   	
                                   	return (String[]) lines.toArray();   //Okay?           
                                   }
                              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:
                              [CODE=java]import java.io.*;
                              import java.util.*;

                              public class Main {

                              private static String[] readLines(Strin g fileName, int m) throws IOException {
                              List lines = new ArrayList();
                              String lineFRST = "";
                              BufferedReader in1 = new BufferedReader( new FileReader(m+"F ileInput.txt")) ;
                              lineFRST = in1.readLine();
                              while(fileName != null) {
                              lines.add(lineF RST);
                              }
                              return (String[])lines.toArray( );
                              }

                              public static void main(String[] args) throws IOException {
                              String[] lines = readLines("/path/to/your/textfile", 1);
                              for(int i = 0; i < lines.length; i++) {
                              System.out.prin tln("lines["+i+"] = "+lines[i]);
                              }
                              }
                              }[/CODE]

                              Comment

                              • shana07
                                Contributor
                                • Jan 2007
                                • 280

                                #30
                                Originally posted by prometheuzz
                                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:
                                [CODE=java]import java.io.*;
                                import java.util.*;

                                public class Main {

                                private static String[] readLines(Strin g fileName, int m) throws IOException {
                                List lines = new ArrayList();
                                String lineFRST = "";
                                BufferedReader in1 = new BufferedReader( new FileReader(m+"F ileInput.txt")) ;
                                lineFRST = in1.readLine();
                                while(fileName != null) {
                                lines.add(lineF RST);
                                }
                                return (String[])lines.toArray( );
                                }

                                public static void main(String[] args) throws IOException {
                                String[] lines = readLines("/path/to/your/textfile", 1);
                                for(int i = 0; i < lines.length; i++) {
                                System.out.prin tln("lines["+i+"] = "+lines[i]);
                                }
                                }
                                }[/CODE]
                                Opss...you're right.
                                Exception in thread " main" java.lang.OutOf MemoryError: Java heap space...
                                What am I supposed to do then yeah.

                                Comment

                                Working...