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

Thread: find the position of the field separator in the String---need help ASAP

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default find the position of the field separator in the String---need help ASAP

    Hi All,

    I have the following input with the Pipe Field Separator(i.e Each filed is separated by Pipe).


    My requirement is
    i need to take the string required and do some validations

    how do i get the filed with the name required from the following string.

    java|21|new1|kkkk|eee|rrrr||dddd|dddd|ooooo|2073|| ||||7747474|48888|required||||444444|.
    java|22|new2|kkkk|eee|rrrr||dddd|dddd|ooooo|2073|g gggg||rrrrrrrr||5555555|7747474|48888|required|||| 444444|.

    The record separator is NewLine and the length of each record is different.
    Is there any method() to find the position of the field separator pipe?


  2. #2
    Junior Member
    Join Date
    Aug 2009
    Location
    San Fransisco
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: find the position of the field separator in the String---need help ASAP

    say

    String str = "java|21|new1|kkkk|eee|rrrr||dddd|dddd|ooooo|2073| | ||||7747474|48888|required||||444444|";
    int index = str.indexOf("|");

    this gives you the index of pipe in the string. But given that you have lot of pipes, use one of the following overloaded methods of indexOf and apply some logic:

    int indexOf(int ch)
    Returns the index within this string of the first occurrence of the specified character.
    int indexOf(int ch, int fromIndex)
    Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
    int indexOf(String str)
    Returns the index within this string of the first occurrence of the specified substring.
    int indexOf(String str, int fromIndex)
    Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

    I would use last method and keep changing the second parameter to get every position of pipe.

    Acumen,
    Lucid forums

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: find the position of the field separator in the String---need help ASAP

    How about converting it to an array, that way you could loop through it.

            final String inputString = "java|21|new1|kkkk|eee|rrrr||dddd|dddd|ooooo|2073| | ||||7747474|48888|required||||444444|";
            final String[] array = inputString.split("\\|");

    // Json

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: find the position of the field separator in the String---need help ASAP

    Are you looking to extract the word 'required' and print the position of it within the String?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Junior Member
    Join Date
    Aug 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: find the position of the field separator in the String---need help ASAP

    yes Exactly(Is there any methods to find out the postion of the string without splitting ).if you know any solution just give me reply

  6. #6
    Junior Member
    Join Date
    Aug 2009
    Location
    San Fransisco
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: find the position of the field separator in the String---need help ASAP

    String index = str.indexOf("required");
    that is what you want right?

    Acumen,
    Lucid forums

  7. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: find the position of the field separator in the String---need help ASAP

    Quote Originally Posted by rajesh.mv View Post
    yes Exactly(Is there any methods to find out the postion of the string without splitting ).if you know any solution just give me reply
    You mean like this then? For the purpose of this code the Strings are in a file called pipefile.txt

    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
     
    public class Pipe {
     
        /**
         * JavaProgrammingForums.com
         */
        public static int position;
     
        public void readFile(){
     
              try
              {
              FileInputStream in = new FileInputStream("pipefile.txt");
              BufferedReader br = new BufferedReader(new InputStreamReader(in));
              String strLine;
     
              while((strLine = br.readLine())!= null)
              {
               System.out.println(strLine);
               position = strLine.indexOf("required");
               System.out.println("'required' at position " + position);
              }
     
              }catch(Exception e){
               System.out.println(e);
              }
     
        }
     
        public static void main(String[] args) {
     
            Pipe p = new Pipe();
            p.readFile();        
     
        }
    }

    Example output:

    java|21|new1|kkkk|eee|rrrr||dddd|dddd|ooooo|2073|| ||||7747474|48888|required||||444444|.
    'required' at position 69
    java|22|new2|kkkk|eee|rrrr||dddd|dddd|ooooo|2073|g gggg||rrrrrrrr||5555555|7747474|48888|required|||| 444444|.
    'required' at position 89
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  8. The Following User Says Thank You to JavaPF For This Useful Post:

    rajesh.mv (August 18th, 2009)

Similar Threads

  1. How to get Mouse Position even if it is not within our application?
    By Freaky Chris in forum Java Programming Tutorials
    Replies: 2
    Last Post: January 4th, 2012, 10:57 AM
  2. Conversion of string into integer in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 17
    Last Post: January 23rd, 2010, 09:33 AM
  3. [SOLVED] How to string a decimal number in Java?
    By Lizard in forum Loops & Control Statements
    Replies: 6
    Last Post: May 14th, 2009, 03:59 PM
  4. Replies: 1
    Last Post: April 1st, 2009, 02:47 PM
  5. [SOLVED] How to a set java class path?
    By captjade in forum Java Theory & Questions
    Replies: 1
    Last Post: March 10th, 2009, 06:40 AM