Results 1 to 8 of 8
  1. #1
    danoc93 is offline Member
    Join Date
    Apr 2012
    Posts
    37
    Rep Power
    0

    Default Give a format to a string

    Well, I developed a program that connects to an IRC server and room...

    The problem is that everyline the program receives has the IRC format, for example this is what I get everytime someone sends a msg:

    :doc123!~deortiz93@186.42.74.154 PRIVMSG #danieloc :adasd

    I want to format that string in something nicer

    doc123>adasd

    From the first one to the second one, I don't know what to do...

    IN C++ I could just use a counter to read everychar, but Java has many tools that are a little different, and I'd like to find an easy way to improve the string..

    I was trying to use charAt, but it really didn't help that much... I also tried to search for the ":" and then take the username until the !, but the program then had issues if the message also contained a : or a !... that's why I am a little bit confused...

    Thanks in advance

  2. #2
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: Give a format to a string

    You could use the substring() and indexOf() functions to parse the string into whatever you want. indexOf() returns the index of the first occurrence of a given string/character. Use these to split up your string into user and message strings.
    Last edited by brynpttrsn; 05-07-2012 at 10:46 AM.

  3. #3
    danoc93 is offline Member
    Join Date
    Apr 2012
    Posts
    37
    Rep Power
    0

    Default Re: Give a format to a string

    I love u bry XD thanks.

  4. #4
    danoc93 is offline Member
    Join Date
    Apr 2012
    Posts
    37
    Rep Power
    0

    Default Re: Give a format to a string

    Quote Originally Posted by brynpttrsn View Post
    You could use the substring() and indexOf() functions to parse the string into whatever you want. indexOf() returns the index of the first occurrence of a given character. Use these to split up your string into user and message strings.
    When I try to get the index of ! in this:
    :doc123!~deortiz93@186.42.74.154 PRIVMSG #danieloc :adasd

    it will give me -1, I don't know why... any help?

  5. #5
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: Give a format to a string

    If the indexOf() method returns -1, it cannot find the string specified.

    Please post code to help me help you. :)

  6. #6
    danoc93 is offline Member
    Join Date
    Apr 2012
    Posts
    37
    Rep Power
    0

    Default Re: Give a format to a string

    Yes, I know it does that... but why? the "!" is there...

    This is the code I'm using...

    line is received by a bufferedreader:

    line=":doc123!~deortiz93@186.42.74.154 PRIVMSG #danieloc :adasd";

    int pos1 = line.indexOf(":");
    int post2 = line.indexOf("!");

    String username=substring(pos1, pos2);

    But I kept getting -1 exception... I don't know why it does that...

    It finds the ":" correctly, but not the other one...

    I used a System.out.println for both positions and it gets 0 for the first one but -1 for the second one...

    I had to do a for to get the correct position, but I don't really want to waste that much space on a code that could be as small as a line:

    for(i=0; i<line.length(); i++){

    if(line.charAt(i)=='!'){

    pos=i;

    }
    }

  7. #7
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: Give a format to a string

    Is it a runtime error that you're getting? or is it just not finding the "!" correctly?
    The line of code you have there should work fine.

    If isn't a runtime error please paste more of your code in [code} tags.

    Edit:
    I copied and pasted the code you have there and it works fine.
    (After fixing the typo post2 > pos2)
    Last edited by brynpttrsn; 05-07-2012 at 11:16 AM.

  8. #8
    danoc93 is offline Member
    Join Date
    Apr 2012
    Posts
    37
    Rep Power
    0

    Default Re: Give a format to a string

    there is an outofbounds exception (-1) in the line where i create the substring using the two indexes... and it's caused becuase it goes with 0,-1...

Similar Threads

  1. Pls help to print string in given format
    By know.how in forum New To Java
    Replies: 2
    Last Post: 08-31-2011, 02:43 PM
  2. Display a '%' when using String.format( )
    By kmckinley820 in forum New To Java
    Replies: 3
    Last Post: 03-19-2011, 12:01 AM
  3. Help with checking for a certain format in a String
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 03-26-2010, 05:56 AM
  4. format content of String[]
    By eponcedeleon in forum Advanced Java
    Replies: 9
    Last Post: 02-23-2010, 06:12 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
  •