Results 1 to 10 of 10
  1. #1
    gicp89 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default Reading extra /n on file.

    Im reading a .txt file with this function:

    public void leerArchivo() throws IOException
    {
    File input = new File("Estadisticas.txt");
    FileReader in = new FileReader(input);
    int c;

    String st=new String();

    while((c=in.read())!=(-1))
    {
    contL++;
    st=new String();

    while((char)c!='\n')
    {
    st=st+(char)c; // conversion de datos
    c=in.read();
    }
    System.out.println(st);
    }
    System.out.println("FILE END");
    in.close();
    }

    example of the text file:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

    When i run this code it comes out to:
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

    i need it to not have that space between lines.

    Thanks for the help,
    G

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    If you use a FileReader you might want to wrap it in a BufferedRead for ease of use. Or even easier use Scanner.

  3. #3
    gicp89 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    How would i do that?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    How would you do what? Bake a cake?

  5. #5
    gicp89 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    How would i wrap it in a Buffered read?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    Oops typo. Thats BufferedReader. To learn you would go to the Java API, look for the BufferedReader class and read about it. Then write some code to test it. If it doesn't work then come back here, post your code, error messages and ask a specific question.

  7. #7
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    nowadays, use the Scanner to read files. although I don't understand yet what you are trying to do, but for reading files using Scanner, here's a skeleton.

    Java Code:
                    Scanner sc = new Scanner ( new File("file") );
                    while  (sc.hasNext() ){
                            String line = sc.nextLine();
                            System.out.println( line ) ;
                    }

  8. #8
    gicp89 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Do i need to add a class or something to use Scanner? im trying to compile the code you just put on and it giving me this error:

    C:\Users\Gustavo Castillo\Desktop\Craps\src\Estadistica.java:105: cannot find symbol
    symbol : class Scanner
    location: class Estadistica
    Scanner sc = new Scanner ( new File("Estadistica.txt") );
    ^
    C:\Users\Gustavo Castillo\Desktop\Craps\src\Estadistica.java:105: cannot find symbol
    symbol : class Scanner
    location: class Estadistica
    Scanner sc = new Scanner ( new File("Estadistica.txt") );

  9. #9
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    make sure you import it first
    Java Code:
    import java.util.Scanner;

  10. #10
    gicp89 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Extra spaces in JLabel...Please help..
    By sohamde in forum New To Java
    Replies: 6
    Last Post: 12-06-2010, 03:53 AM
  2. Extra Sun Voucher available
    By PriyaK in forum Reviews / Advertising
    Replies: 4
    Last Post: 01-15-2010, 06:41 AM
  3. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  4. [SOLVED] File I/O Extra Credit Assignment FTW!
    By Bascotie in forum New To Java
    Replies: 9
    Last Post: 06-10-2008, 08:20 AM
  5. Extra bracket
    By CrazyShells Slam in forum New To Java
    Replies: 5
    Last Post: 05-16-2008, 06:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •