readLine()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shana07
    Contributor
    • Jan 2007
    • 280

    #31
    Actually total lines of this first file is just 97.
    suppose shouldn't be any problem isn't...

    Comment

    • prometheuzz
      Recognized Expert New Member
      • Apr 2007
      • 197

      #32
      Originally posted by shana07
      Opss...you're right.
      Exception in thread " main" java.lang.OutOf MemoryError: Java heap space...
      What am I supposed to do then yeah.
      This happens because you're in an infinite loop.

      Comment

      • shana07
        Contributor
        • Jan 2007
        • 280

        #33
        Originally posted by prometheuzz
        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.Class CastException: [Ljava.lang.Obje ct; can not be cast to [Ljava.lang.Stri ng;
        have a look and teach me please..

        Code:
        while (readable)
        {
        lineFRST = in1.readLine();
        if (lineFRST != null)
        {
         lines.add(lineFRST);
         readable = true;
        } else
         readable = false;
        }
          return (String[]) lines.toArray();

        Comment

        • prometheuzz
          Recognized Expert New Member
          • Apr 2007
          • 197

          #34
          Originally posted by shana07
          I have this to replace the while loop, it's giving me new run time error - Exception in thread "main" java.lang.Class CastException: [Ljava.lang.Obje ct; can not be cast to [Ljava.lang.Stri ng;
          have a look and teach me please..

          Code:
          while (readable)
          {
          lineFRST = in1.readLine();
          if (lineFRST != null)
          {
          lines.add(lineFRST);
          readable = true;
          } else
          readable = false;
          }
          return (String[]) lines.toArray();

          Try returning it like this:

          [CODE=java]return lines.toArray(n ew String[lines.size()]);[/CODE]

          Comment

          • shana07
            Contributor
            • Jan 2007
            • 280

            #35
            Originally posted by prometheuzz
            Try returning it like this:

            [CODE=java]return lines.toArray(n ew String[lines.size()]);[/CODE]
            I am really clueless about type error...please.

            incompatible types
            found : java.lang.Objec t[]
            required: java.lang.Strin g[]
            return lines.toArray(n ew String[lines.size()]);
            ^

            Code:
            return lines.toArray(new String[lines.size()]); 
                      // return (String[]) lines.toArray();

            Comment

            • prometheuzz
              Recognized Expert New Member
              • Apr 2007
              • 197

              #36
              Originally posted by shana07
              I am really clueless about type error...please.

              incompatible types
              found : java.lang.Objec t[]
              required: java.lang.Strin g[]
              return lines.toArray(n ew String[lines.size()]);
              ^

              Code:
              return lines.toArray(new String[lines.size()]); 
                        // 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:

              [CODE=java]import java.io.*;
              import java.util.*;

              public class Main {

              private static String[] readLines(Strin g fileName, int m) throws IOException {
              List<String> lines = new ArrayList<Strin g>();
              String line;
              BufferedReader in1 = new BufferedReader( new FileReader(m+fi leName));
              while((line = in1.readLine()) != null) {
              lines.add(line) ;
              }
              return lines.toArray(n ew String[lines.size()]);
              }

              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

                #37
                Originally posted by prometheuzz
                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:

                [CODE=java]import java.io.*;
                import java.util.*;

                public class Main {

                private static String[] readLines(Strin g fileName, int m) throws IOException {
                List<String> lines = new ArrayList<Strin g>();
                String line;
                BufferedReader in1 = new BufferedReader( new FileReader(m+fi leName));
                while((line = in1.readLine()) != null) {
                lines.add(line) ;
                }
                return lines.toArray(n ew String[lines.size()]);
                }

                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]
                Yes, you are really there! It works great. Thank you very much prometheuzz
                Just this line makes my program different...
                Code:
                List<String> lines = new ArrayList<String>();
                I can't think that complicated....
                The next mission is how to compare them....
                again please advise me....

                Comment

                • prometheuzz
                  Recognized Expert New Member
                  • Apr 2007
                  • 197

                  #38
                  Originally posted by shana07
                  Yes, you are really there! It works great. Thank you very much prometheuzz
                  Just this line makes my program different...
                  Code:
                  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.

                  Comment

                  Working...