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

Thread: Parsing a txt file having different number of spaces

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

    Default Parsing a txt file having different number of spaces

    I am not able to parse the following data in txt file in java.

    SoftSwitchN956 Name IP Address Result
    BRAZIL(Current) 199.111.0.115 Success

    line.trim(),line.replaceAll("\\s", ","), none of the methods is working.
    Please suggest a way to parse the data.

  2. #2
    Junior Member
    Join Date
    Dec 2021
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Parsing a txt file having different number of spaces

    I am sure you already found the answer but i will answer in case anyone else need to do something similar. Based on your example here, you could have used StringTokenizer:

    StringTokenizer st = new StringTokenizer("BRAZIL(Current) 199.111.0.115 Success"," ");
    while (st.hasMoreTokens()) {
    System.out.println(st.nextToken());
    }
    This would have printed each words. No matter how many spaces in between them all.

Similar Threads

  1. Replies: 10
    Last Post: November 8th, 2012, 06:29 AM
  2. Replies: 2
    Last Post: November 7th, 2012, 10:45 PM
  3. JAVA code for deleting spaces from Text file
    By Maheshgx in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 17th, 2012, 06:26 AM
  4. [SOLVED] Splitting a txt file with multiple spaces
    By macko in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: November 1st, 2011, 01:03 PM
  5. Number Format Exception while parsing long
    By Aamir in forum Java Networking
    Replies: 1
    Last Post: May 19th, 2011, 03:59 AM

Tags for this Thread