Results 1 to 13 of 13
  1. #1
    Input is offline Member
    Join Date
    Dec 2013
    Posts
    10
    Rep Power
    0

    Default .useDeliniter("\r\n");

    I've followed through a tutorial and I'm just messing about with the code... On the scanner that was used there is ".useDeliniter("\r\n");" at the end of it :

    Java Code:
    		Scanner input = new Scanner(System.in).useDelimiter("\r\n");

    I've tried to find a basic explanation but everything's a bit much :

    Quote Originally Posted by SomeDudeOnTheInternet...

    \n is a line feed (LF) character, character code 10. \r is a carriage return (CR) character, character code 13. What they do differs from system to system. On Windows, for instance, lines in text files are terminated using CR followed immediately by LF (e.g., CRLF). On Unix systems......
    that goes over my head a bit...

    I know that \n is new line.... If I use \r in a System.out it seems to do the same thing as \n... Though I don't know if it is the same or not...


    Also, If I delete that part from the end of the scanner so the code reads

    Java Code:
    Scanner input = new Scanner(System.in);

    It works just the same, so I don't even understand why they were used in the first place.

    Any pointers welcome!

    Here's the code in full just in case anyone wanted to see it in context :

    Java Code:
    import java.util.Scanner;
    
    public class TwoDiamensionalArrayExample {
    
    	public static void main(String[] args) {
    
    		Scanner input = new Scanner(System.in).useDelimiter("\r\n");
    
    		String[][] bookTitles = new String[2][3];
    		int[][] publishYears = new int[2][3];
    
    		for (int i = 0; i < bookTitles.length; i++) {
    			System.out.println("Shelf " + (i + 1));
    
    			for (int k = 0; k < 3; k++) {
    				System.out.print("Enter a book title: ");
    				String title = input.next();
    				bookTitles[i][k] = title;
    
    				System.out.println("Publish Year: ");
    				int year = input.nextInt();
    				publishYears[i][k] = year;
    
    			}
    		}
    
    		System.out.println("\nDone");
    		for (int i = 0; i < bookTitles.length; i++) {
    
    			for (int k = 0; k < 3; k++) {
    				String title = bookTitles[i][k];
    				int publishYear = publishYears[i][k];
    				System.out.println(title + " Year : " + publishYear);
    
    			}
    		}
    
    		
    		
    
    	}
    
    }

    And one more question - should that Scanner be closed??? I seem to read mixed thing's about Scanners, yes they should be closed / no they shouldn't...

    thank you :)

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: .useDeliniter("\r\n");

    Not sure what to tell you if that explanation you quoted goes over your head. Is there something specific you don't understand about it?
    Get in the habit of using standard Java naming conventions!

  3. #3
    Input is offline Member
    Join Date
    Dec 2013
    Posts
    10
    Rep Power
    0

    Default Re: .useDeliniter("\r\n");

    Quote Originally Posted by kjkrum View Post
    Not sure what to tell you if that explanation you quoted goes over your head. Is there something specific you don't understand about it?
    well I just wasn't sure but ok...

    What about the scanner then, should that be closed?

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: .useDeliniter("\r\n");

    Not if it's reading from System.in.
    Get in the habit of using standard Java naming conventions!

  5. #5
    Input is offline Member
    Join Date
    Dec 2013
    Posts
    10
    Rep Power
    0

    Default Re: .useDeliniter("\r\n");

    Quote Originally Posted by kjkrum View Post
    Not if it's reading from System.in.
    OK cheers kj - the little error message from Eclipse always makes me want to shut it, but I'll leave it be.

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: .useDeliniter("\r\n");

    Well, perhaps you should close it. Generally you shouldn't try to close System.in, and if your program is exiting immediately after using the Scanner, it probably doesn't matter. But if the program kept running, leaving the scanner open might leak some resource.

    What happens if you close the first Scanner, then try to create another Scanner that also reads System.in? Does the second one work? If not, then you probably shouldn't close the first one. There are some threads on Stack Overflow about other types of streams and readers closing System.in and it subsequently not working.
    Get in the habit of using standard Java naming conventions!

  7. #7
    Input is offline Member
    Join Date
    Dec 2013
    Posts
    10
    Rep Power
    0

    Default Re: .useDeliniter("\r\n");

    Quote Originally Posted by kjkrum View Post
    Well, perhaps you should close it. Generally you shouldn't try to close System.in, and if your program is exiting immediately after using the Scanner, it probably doesn't matter. But if the program kept running, leaving the scanner open might leak some resource.

    What happens if you close the first Scanner, then try to create another Scanner that also reads System.in? Does the second one work? If not, then you probably shouldn't close the first one. There are some threads on Stack Overflow about other types of streams and readers closing System.in and it subsequently not working.


    Not sure - I can test later though.... At the moment as I'm so new to learning I think it's a case of best practice rather than the practical application within the programs that I'm making, as they are thing like

    "what's your name"

    dorngrg

    "your name is dorngrg!"



    Pretty intricate.

  8. #8
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: .useDeliniter("\r\n");

    This is a good question. I don't know what the best practice is when using the System streams like this. I always heard you shouldn't try to close them, but I could be wrong.
    Get in the habit of using standard Java naming conventions!

  9. #9
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: .useDeliniter("\r\n");

    I know that \n is new line.... If I use \r in a System.out it seems to do the same thing as \n... Though I don't know if it is the same or not...
    They are not the same \r returns to the beggining of the line, while \n jumps to the next line

  10. #10
    Input is offline Member
    Join Date
    Dec 2013
    Posts
    10
    Rep Power
    0

    Default Re: .useDeliniter("\r\n");

    Quote Originally Posted by Kagiso View Post
    They are not the same \r returns to the beggining of the line, while \n jumps to the next line
    Cheers Kagiso - how would that effect a program in general? Are you able to be a basic context / example for them please?

    Thanks!

  11. #11
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: .useDeliniter("\r\n");

    Cheers Kagiso - how would that effect a program in general? Are you able to be a basic context / example for them please?
    System.out.print("Kagiso \rEddie \nis my name"); // try a simple print statement such as this one & you'll see for yourself

    But I do not see \r coming handy in any code I've ever written, I wouldn't reccomend anyone to use it

  12. #12
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: .useDeliniter("\r\n");

    You should use "\r\n" in data transmitted over the network. CSC 209: Newline conventions and internet communications
    Get in the habit of using standard Java naming conventions!

  13. #13
    Input is offline Member
    Join Date
    Dec 2013
    Posts
    10
    Rep Power
    0

    Default Re: .useDeliniter("\r\n");

    Quote Originally Posted by kjkrum View Post
    You should use "\r\n" in data transmitted over the network. CSC 209: Newline conventions and internet communications
    Right, cheers

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 09:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 04:06 PM
  3. Replies: 1
    Last Post: 02-01-2012, 09:37 PM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 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
  •