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 13 of 13

Thread: use Delimiter, Scanner Class

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy use Delimiter, Scanner Class

    use Delimiter method to read in a series of urls and print out each part on a separate line. For example part of the output could be;

    URL: http://java.sun.com/docs/books/tutor...pts/index.html

    http:
    java.sun.com
    docs
    books
    tutorial
    java
    concepts
    index.html

    Use the following data
    http://java.sun.com/docs/books/tutor...pts/index.html
    http://www.javaworld.com/javaworld/j...6-java101.html
    http://www.developer.com/java/article.php/935351
    http://java.sun.com/javase/6/docs/api



    pls! hurry!!!! help i am stuck in this .....



    the other question is ???



    ---------------------------------------------------------------------------

    Write a program to read a text file and replace a specific string of text with another, write the output to a file also. If you use the following as the input text then you could replace ‘tutor’ with some suitable alternative!
    Last edited by izzahmed; October 22nd, 2011 at 05:35 PM.


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: use Delimiter, Scanner Class

    It looks like you want to replace the / with a new line, have a look at the String.replaceAll(String expr, String substitute) method.

  3. #3
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: use Delimiter, Scanner Class

    good idea to read this see what it says. Just think anything can be the Delimiter

    Delimiter

    hope this helps..

  4. #4
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: use Delimiter, Scanner Class

    Im also trying a similar program, where i read in an url from the user and seperate it using a delimeter("/")

    i.e.

    user input: 'www.google.co.uk/index.html'

    output should be:

    'www.google.co.uk'
    index.html


    i think im almost there, here is my code

    public static void main(String[] args) {
     
            String url;
     
            Scanner scan, seperateUrl;
     
            scan = new Scanner(System.in);
     
            System.out.println("Enter url: ");
            url = scan.nextLine();
     
            seperateUrl = new Scanner(url);
            seperateUrl.useDelimiter("/");
     
            System.out.println(seperateUrl.next());
     
            System.out.println();
     
        }
    }


    My output when i type in 'www.google.co.uk/index.html'

    is just:

    'www.google.co.uk'


    i.e. it seems to just be cutting off the rest of the url at the point of the first forward slash.

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: use Delimiter, Scanner Class

    It works fine for me. Perhaps if you tried printing out more than one token it might do what you want.
    Improving the world one idiot at a time!

  6. #6
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: use Delimiter, Scanner Class

    What do you mean by token mate?

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: use Delimiter, Scanner Class

    a/b/c/d would be split up into 4 tokens a, b, c and d. In your code you only print the first token: 'www.google.co.uk'. Your code does not print any more tokens: index.html etc.
    Improving the world one idiot at a time!

  8. #8
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: use Delimiter, Scanner Class

    how do i get it to print all the tokens mate?
    Last edited by djl1990; October 25th, 2011 at 04:39 PM.

  9. #9
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: use Delimiter, Scanner Class

    Is it a while loop?

    to keep printing after every forward slash?

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: use Delimiter, Scanner Class

    Quote Originally Posted by djl1990 View Post
    Is it a while loop?
    Did you try it?
    Improving the world one idiot at a time!

  11. #11
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: use Delimiter, Scanner Class

    Just sussed it out i think.

    I have this now and it seems to be working

    while (seperateUrl.hasNext())
            System.out.println("    " + seperateUrl.next());
     
            System.out.println();

  12. #12
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: use Delimiter, Scanner Class

    Congrats!

    Now isn't it better to work out a solution yourself rather than have someone tell you?
    Improving the world one idiot at a time!

  13. #13
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: use Delimiter, Scanner Class

    Yeah it is. Makes a change for once.

    Ive only just begun though, and i just cant get my head around half the concepts. I think the book im working through is hopeless and rammed packed of mistakes aswell, which doesnt help either.

    Im okay at writing single main class programs, its writing multiple classes and my own classes where it gets tricky and hard to follow.

Similar Threads

  1. I am Facing problem in Scanner Class
    By snithishkumar in forum Java SE APIs
    Replies: 2
    Last Post: October 11th, 2011, 09:39 AM
  2. delimiter question
    By Sterzerkmode in forum Java SE APIs
    Replies: 1
    Last Post: November 19th, 2009, 05:21 AM
  3. Scanner class error "java.lang.Error"
    By Lheviathan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 21st, 2009, 02:23 AM
  4. [SOLVED] Problem in Coin-counter with scanner class
    By coccoster in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: March 25th, 2009, 08:46 AM
  5. Replies: 1
    Last Post: May 13th, 2008, 08:08 AM

Tags for this Thread