Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By wolfcro

Thread: Use of Scanner class and Delimiter

  1. #1
    tjhodge is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default Use of Scanner class and Delimiter

    How would i use the scanner class to read a textfile called "music". It contains data in the form;

    David/Grey#Elton/John#Micheal/Buble#Celine/Dion#

    / - splits up the name
    # - marks end of person

    i would like to put them in to the fields 'forename' and 'surname' for printing.

  2. #2
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

    Default

    an easy solution could be:

    Java Code:
    scan = new Scanner(new File("music.txt"));
    			
    scan.useDelimiter("#");
    			
    while(scan.hasNext()){
    				
    	String[] list = scan.next().split("/");
    	System.out.println("name = "+list[0]);
    	System.out.println("surname = "+list[1]);
    }
    the important parts are the settings of delimiters in scanner and in split....
    a delimiter can be any regular expression

    hope it helped
    Milan99 likes this.

  3. #3
    tjhodge is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default

    Sorry i did not make it very clear. The main method should input one person’s data at a time, assign that person’s names to two String variables called forename and surname, respectively, and then print out forename and surname on a line.

    i understand that i need the two variables
    String forename;
    String surname;

    and i have

    try
    {
    filescan = new Scanner(new File("music"));
    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    filescan.useDelimiter("#");

    Its the fact of breaking up the string
    David/Grey#...etc
    and putting it into the two String variables then printing.

  4. #4
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

    Default

    sorry sorry.....
    or i don't understand what you want to do or you don't understand the code i wrote.....

    i splitted all your text file....
    the scanner gets me strings like "name/surename"
    and then the split gets me an array containing ["name","surename"]

    so....
    Java Code:
    String name = list[0];
    String surname = list[1];
    
    // and we write it to the same line
    System.out.println(name+" "+surename);

Similar Threads

  1. Scanner Class...
    By TheRocket in forum New To Java
    Replies: 5
    Last Post: 12-05-2008, 10:48 AM
  2. Scanner class problems
    By sypherz in forum New To Java
    Replies: 6
    Last Post: 06-09-2008, 10:46 AM
  3. Using Scanner class to read int value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 10:34 AM
  4. Scanner class
    By ajaymenon.k in forum Advanced Java
    Replies: 1
    Last Post: 11-26-2007, 08:01 AM
  5. JDK 5.0 Scanner Class
    By Sircedric88 in forum New To Java
    Replies: 3
    Last Post: 07-27-2007, 07:55 PM

Posting Permissions

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