Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 25 of 25

Thread: simulate BufferedReader's readLine()

  1. #1
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default simulate BufferedReader's readLine()

    Any idea please? I am stuck, I look at BufferedReader methods but i didn't found any useful, need to do a method that return a String with the next line to read from a file. When it reach the end of file , it return null instead the String.

    It is required that i pass BufferedReader as parameter

    The only code I am able to think is "return br.readLine;" somewhere, but obviusly i need to remake that method, and not use it inside. I'm really stucked

    import java.io.*;
     
    public class leer {
     
    	public static void main(String[] args) {
    		try {
    			BufferedReader br = new BufferedReader(new StringReader("First line\nSecond line"));
     
    			leeLinea(br);
     
    		}catch(FileNotFoundException e) {
     
    		}catch(IOException e){
     
    		}
    	}
    	static String leeLinea(BufferedReader br) throws IOException{
    		String string = "";
    		//while((string = nextLine()) != null)
    			return string;
    		return null;
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: simulate BufferedReader's readLine()

    When it reach the end of file , it return null instead the String.
    That is what the method is defined to do. What do you want your method to return when it finds end of file?

    I'm not sure I understand your problem. null at end of file is easy to test for. Any code that reads data from a file needs a way to know when it has reached end of file. null says that there is no more data.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: simulate BufferedReader's readLine()

    a

    --- Update ---

    Quote Originally Posted by Norm View Post
    That is what the method is defined to do. What do you want your method to return when it finds end of file?

    I'm not sure I understand your problem. null at end of file is easy to test for. Any code that reads data from a file needs a way to know when it has reached end of file. null says that there is no more data.
    I must return null when it reach end of file.

    I must write the code of readLine()

    My method must pass a BufferedReader as parameter.
    I can't use readLine() method defined by the API

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: simulate BufferedReader's readLine()

    I can't use readLine() method defined by the API
    Why? Is this a requirement from an instructor? Your assignment is write your own version using the read() method?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: simulate BufferedReader's readLine()

    he don't really say it, but it has no sense that i use readLine inside my method that simulates readLine

    I don't think i should do this
    static String leeLinea(BufferedReader br) throws IOException{
    		String string = "";
    		while((string = br.readLine()) != null)
    			return string;
    		return null;
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: simulate BufferedReader's readLine()

    Can you explain your problem?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: simulate BufferedReader's readLine()

    "Need to do a method that return a String with the next line to read from a file. When it reach the end of file , it return null instead the String."



    Should do this: without using readLine()
    static String leeLinea(BufferedReader br) throws IOException{
    		String string = "";
    		while((string = br.readLine()) != null)
    			return string;
    		return null;
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: simulate BufferedReader's readLine()

    Why use a while loop?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: simulate BufferedReader's readLine()

    it's how i read a line from a file, but is just an example, how should my method work.
    Anyway, if it is too hard, 100% sure i missunderstood my problem. so i ask tomorrow to my teacher. thank you for today

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: simulate BufferedReader's readLine()

    Yes, you need to ask the teacher what the method should do and how it should do it.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Rexshine (February 24th, 2013)

  12. #11
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: simulate BufferedReader's readLine()

    Ok, i know exactly what i have to do: I must simulate readLine() method, reading char by char, and returning the first String when find carriage return, and if it is the end of file, must return NULL.

    I don't know how to check if a character is end of file. it doesn't work with ASCII: 3 (end of text)

    in my while loop i get a infinite loop because i get an unknown character infinite.
    My code:
    import java.io.*;
     
    public class Ejercicio13 {
     
    	public static void main(String[] args) {
    		try {
    			BufferedReader br = new BufferedReader(new StringReader("En un lugar\r\nDe la mancha"));
     
     
    			String linea = "";
    			while((linea = leeLinea(br))!= null)
    				System.out.println(linea);
    		}catch(FileNotFoundException e) {
     
    		}catch(IOException e){
     
    		}
    	}
    	static String leeLinea(BufferedReader br) throws IOException{
    		//Character caracter = null;
    		char caracter = '\0';
    		String linea = "";
     
    		while((caracter = (char) br.read()) != 13){
    			if(caracter == 3){
    				return null;
    			}
    			linea+=caracter;
    		}
    		return linea;
    	}
    }

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: simulate BufferedReader's readLine()

    Try debugging the code by printing out the value returned by each call to the read() method so you can see what the computer sees when it executes the code. Be sure to use a small file with a few lines.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: simulate BufferedReader's readLine()

    I did before, i see the char '?' then i search for char ? that is 63 and tried : if (caracter == (char)63) {return null;} but doesnt work. so i made this to find the ascii code for that char

    for(int i=0; i<255; i++)
      if(caracter == (char) i)
         System.out.println(i);
    but never show which ascii code is, so i don't know what to do

    I even tried with i<5000 , and never show which one is

    I think is an encoded byte or so...

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: simulate BufferedReader's readLine()

    Print out the int value before casting it to char, then there won't be a ? problem.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: simulate BufferedReader's readLine()

    it shows '-1' : System.out.println(br.read());

    oh my god, now i understand what the -1 that uses the teacher means... thank you
    this is like index -1 for string index right?

    This is right?
    if(caracter == (char)-1)
    Looks like wrong

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: simulate BufferedReader's readLine()

    Read the API doc for the read() method.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #17
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: simulate BufferedReader's readLine()

    "The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached"



    Some help please. How to return the string when it find \n or when it find -1 but last index of string is not -1.

    	//Character caracter = null;
    		char caracter = '\0';
    		String linea = "";
    		int indice = 0;
    		while((caracter = (char) br.read()) != 13 && caracter != (char)-1){
    			indice++;
    			linea+=caracter;
    			if(linea.length()>0)
    				if(linea.charAt(indice-1)!= (char)-1 && caracter == (char)-1)
    					return linea;
    		}
    		if(caracter == 13)
    			return linea;
     
    		return null;

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: simulate BufferedReader's readLine()

    -1 if the end of the stream
    That value says there is no more data. Time to return a null.

    but last index of string is not -1.
    I don't know what that means?
    The read() method has nothing to do with Strings or indexes. It reads bytes or chars
    If you don't understand my answer, don't ignore it, ask a question.

  20. #19
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: simulate BufferedReader's readLine()

    but then i could not return last string in my example input: new StringReader("En un lugar\r\nDe la mancha"))

    i only return first line "en un lugar" but second one has append the -1 with the string, then i can't return the string, and at the same time after it return null

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: simulate BufferedReader's readLine()

    Return what was read up to but not including the -1.
    Next call return the null.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #21
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: simulate BufferedReader's readLine()

    I tried many ways but this is the best. that doesn't show first character because i do a read() at start
    	static String leeLinea(BufferedReader br) throws IOException{
    		//Character caracter = null;
    		char caracter = '\0';
    		if((caracter = (char) br.read()) == (char)-1)
    			return null;
    		String linea = "";
    		while((caracter = (char) br.read()) != 13 && caracter != (char)-1)
    			linea+=caracter;
    		return linea;
    	}
    They should make more methods for BufferedReader

  23. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: simulate BufferedReader's readLine()

    rewrite the code to read into an int and work with its value: cast and concatenate or whatever needs to be done.
    Don't make so many tests and casts.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #23
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: simulate BufferedReader's readLine()

    I don't understand you at all. Do you mean this?
    	static String leeLinea(BufferedReader br) throws IOException{
    		char caracter = '\0';
    		int esNulo = 0;
    		if((esNulo = (int) br.read()) == -1)
    			return null;
    		String linea = "";
    		while((caracter = (char) br.read()) != 13 && caracter != (char)-1)
    			linea+=caracter;
    		return linea;
    	}
    same problem ))

  25. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: simulate BufferedReader's readLine()

    Do the read in only one place, not two.
    Test what was read after it is read and then decide what to do.
    If you don't understand my answer, don't ignore it, ask a question.

  26. The Following User Says Thank You to Norm For This Useful Post:

    Rexshine (February 25th, 2013)

  27. #25
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: simulate BufferedReader's readLine()

    will check it tomorrow, thank u , good night

Similar Threads

  1. Re: Problem with BufferedReader : readLine() cut the line
    By caveden in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 10th, 2012, 08:46 AM
  2. Problem with BufferedReader : readLine() cut the line
    By mfgagne73 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 3rd, 2012, 10:18 AM
  3. simulate calculation ?
    By meryqat in forum Java Theory & Questions
    Replies: 3
    Last Post: April 30th, 2012, 01:03 PM
  4. Simulate Latency
    By pixelDepth in forum Java Networking
    Replies: 6
    Last Post: November 30th, 2011, 08:26 AM
  5. (.readLine() method) of BufferedReader class
    By chronoz13 in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: October 13th, 2009, 06:59 PM