Results 1 to 5 of 5
  1. #1
    reis3k is offline Member
    Join Date
    Aug 2009
    Posts
    12
    Rep Power
    0

    Default useDelimiter usage

    Hi I really confused with delimiters. I have a text file which has two number separated by white space on each line. I need first number as a array index and second number for that array's index value. How can I do this. I started my code in this way:

    Java Code:
    		BufferedReader in = new BufferedReader(new FileReader("data.txt"));
    		Scanner s = new Scanner(in).useDelimiter("\n");
    		
    		String strLine = null;
    
    		
    		while (s.hasNextLine()) {
    			System.out.println(s.nextInt());
    		}

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,354
    Blog Entries
    7
    Rep Power
    20

    Default

    Don't fiddle with those delimeters for now, the default values are adequate because your numbers are separated by white space two of them per line.

    kind regards,

    Jos

  3. #3
    reis3k is offline Member
    Join Date
    Aug 2009
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Don't fiddle with those delimeters for now, the default values are adequate because your numbers are separated by white space two of them per line.

    kind regards,

    Jos
    Thanks, but I just need the first number on one line firstly as a index number of my array. So in a way I should skip the second number on the line and just see the fisrt numbers on each line. How can I do this?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,354
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by reis3k View Post
    Thanks, but I just need the first number on one line firstly as a index number of my array. So in a way I should skip the second number on the line and just see the fisrt numbers on each line. How can I do this?
    Do a nextInt() to get your index number and then a nextLine() to read the rest of the line (and discard the return value of that method) then start all over ...

    kind regards,

    Jos

  5. #5
    reis3k is offline Member
    Join Date
    Aug 2009
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Do a nextInt() to get your index number and then a nextLine() to read the rest of the line (and discard the return value of that method) then start all over ...

    kind regards,

    Jos
    Thanks, now I got the idea. Here is the code:
    Java Code:
    		while (s.hasNextLine()) {
    			//System.out.println(s.nextInt());
    			//s.nextLine();#
    			puzzle[s.nextInt()]=s.nextInt();
    		}

Similar Threads

  1. HashMap usage
    By vasavi.singh in forum New To Java
    Replies: 4
    Last Post: 03-05-2009, 04:50 PM
  2. usage of Hashtable
    By venu2807 in forum New To Java
    Replies: 4
    Last Post: 01-27-2009, 03:36 AM
  3. Need help with AST usage on Eclipse! Thanks!
    By j_aquino314 in forum Eclipse
    Replies: 0
    Last Post: 10-08-2008, 07:15 PM
  4. JVM memory usage
    By lardum in forum New To Java
    Replies: 7
    Last Post: 06-26-2008, 03:30 AM
  5. Integer Usage
    By bluegreen7hi in forum New To Java
    Replies: 11
    Last Post: 01-24-2008, 08:54 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
  •