Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    magichaxx is offline Member
    Join Date
    Nov 2013
    Posts
    27
    Rep Power
    0

    Default Separating codeindexOf/substring methods + Using for different primitives/objects

    im working on a project in Eclipse where i have to input a String with commas in specific places and separate that String using the indexOf/substring methods and then output information based on the type of code that ranges from int, double, and boolean, all the way to String. i know how to use the indexOf/substring methods but i dont know how to tell the program that each String section is int, double, etc. Can anyone help?

    for the record, that title is supposed to be Separating code with indexOf/substring methods
    Last edited by magichaxx; 11-23-2013 at 08:06 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,302
    Blog Entries
    7
    Rep Power
    20

    Default Re: Separating codeindexOf/substring methods + Using for different primitives/objects

    You could try to parse each String with Double.parseDouble(yourString). If it throws an exception, try again with Integer.parseInt(yourString) etc. etc.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    magichaxx is offline Member
    Join Date
    Nov 2013
    Posts
    27
    Rep Power
    0

    Default Re: Separating codeindexOf/substring methods + Using for different primitives/objects

    thanks. and also, just so im not doing this wrong, how do you use indexOf to identify a recurring character and then use it to separate the String by that character?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,250
    Rep Power
    25

    Default Re: Separating codeindexOf/substring methods + Using for different primitives/objects

    Read the API doc for the String class's indexOf() method. It args for controlling where it searches.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,302
    Blog Entries
    7
    Rep Power
    20

    Default Re: Separating codeindexOf/substring methods + Using for different primitives/objects

    ... or even better: read the API docs for the String.split( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,250
    Rep Power
    25

    Default Re: Separating codeindexOf/substring methods + Using for different primitives/objects

    split() requires a regex which is orders of magnitude more complex than using indexOf()
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,302
    Blog Entries
    7
    Rep Power
    20

    Default Re: Separating codeindexOf/substring methods + Using for different primitives/objects

    Quote Originally Posted by Norm View Post
    split() requires a regex which is orders of magnitude more complex than using indexOf()
    Splitting on "," is just a one-state machine and the split( ... ) method builds a nice array of the individual String parts for you ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    magichaxx is offline Member
    Join Date
    Nov 2013
    Posts
    27
    Rep Power
    0

    Default Re: Separating codeindexOf/substring methods + Using for different primitives/objects

    i cant figure it out. everything i find is skipping around this problem and not addressing it directly. i want:

    name, age, gender

    to turn into:

    name
    age
    gender

    by using the indexOf/substring methods to separate it using the commas

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,250
    Rep Power
    25

    Default Re: Separating codeindexOf/substring methods + Using for different primitives/objects

    Post the code you are having problems with.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    magichaxx is offline Member
    Join Date
    Nov 2013
    Posts
    27
    Rep Power
    0

    Default Re: Separating codeindexOf/substring methods + Using for different primitives/objects

    String x = scan.nextLine();

    int y = x.indexOf(",");
    String a = x.substring(0, y);
    String b = x.substring(y + 1, y);

    and then i get nothing from there. i feel like im on the right track, but nothing i try works. this is the closest ive gotten, but it gives me a -1 exception.
    Last edited by magichaxx; 11-25-2013 at 12:01 AM.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,250
    Rep Power
    25

    Default Re: Separating codeindexOf/substring methods + Using for different primitives/objects

    You need to call indexOf() repeatedly to find each "," one after the other.
    After finding the first, use its position to find the next one.
    Always test the value returned by indexOf() to make sure the String was found.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    magichaxx is offline Member
    Join Date
    Nov 2013
    Posts
    27
    Rep Power
    0

    Default Re: Separating codeindexOf/substring methods + Using for different primitives/objects

    oh! thanks!

  13. #13
    magichaxx is offline Member
    Join Date
    Nov 2013
    Posts
    27
    Rep Power
    0

    Default Re: Separating codeindexOf/substring methods + Using for different primitives/objects

    problem. i cant get the next indexOf to identify the next "," in the String. ive tried:

    int i2 = x.indexOf(",");
    String b = x.substring(i2 + 1);

    int i2 = x.indexOf(i1 + ",");
    String b = x.substring(i2 + 1);

    int i2 = x.indexOf(i1 + 1, ",");
    String b = x.substring(i2 + 1);

    int i2 = x.indexOf(i1 + 1 + ",");
    String b = x.substring(i2 + 1);

    and none of them work. they either -1 me or just print out the entire String.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,250
    Rep Power
    25

    Default Re: Separating codeindexOf/substring methods + Using for different primitives/objects

    What is this statement supposed to do?
    int i2 = x.indexOf(i1 + ",");
    The method searches for the String that is passed to it as an arg.
    If i1 = 4, the String it searches for is "4," ???
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    magichaxx is offline Member
    Join Date
    Nov 2013
    Posts
    27
    Rep Power
    0

    Default Re: Separating codeindexOf/substring methods + Using for different primitives/objects

    its supposed to do this for a scan.nextLine(). the placement of the "," is unknown, so i was trying to find a way to get indexOf to identify the character right after i1, which is the first indexOf here:

    int i1 = x.indexOf(",");

    i need to find a pattern that can identify a character immediately following the previous indexOf, but i cant figure that out.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,250
    Rep Power
    25

    Default Re: Separating codeindexOf/substring methods + Using for different primitives/objects

    identify a character immediately following the previous indexOf
    Look at the substring() or the charAt() methods. Either one can be used to access a character in a String.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default Re: Separating codeindexOf/substring methods + Using for different primitives/objects

    I'm not sure if this is what's asked, but...

    Java Code:
            String x = "abcfg, def, gi, 4, 6, 7";
            int a = x.indexOf(",");
            
            String x2 = x.substring(a+1, x.length());
            int b = x2.indexOf(",");
    Here given the string x, we get int a which is the first location of the recurring comma
    Next to determine the next comma, we need to get the modified string which exclude the first comma, means x substring starting from a+1 (cause a is the position of the comma itself), then repeating the same indexOf() for the substring, you'll get the 2nd comma position, and so on
    Given the position of the comma known, you can also get the string between two commas using substring()

    Sorry, if there's mistake...just my first time try to post a reply
    First is to make something usable, next is to aim for perfection which means a never ending improvements.

  18. #18
    magichaxx is offline Member
    Join Date
    Nov 2013
    Posts
    27
    Rep Power
    0

    Default Re: Separating codeindexOf/substring methods + Using for different primitives/objects

    so what youre saying is somehow use .length to identify the number of characters between commas and use that to separate the String?

    and also what are these API docs you speak of?
    Last edited by magichaxx; 11-25-2013 at 03:44 AM.

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,250
    Rep Power
    25

    Default Re: Separating codeindexOf/substring methods + Using for different primitives/objects

    I was answering this question:
    identify a character immediately following the previous indexOf
    If there is a comma found by location 27 by indexOf() and you want to see the next char following the comma, it is at location 28.
    Use the 28 with the charAt(28) method or with the substring(28, 29) method to get the next character.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    magichaxx is offline Member
    Join Date
    Nov 2013
    Posts
    27
    Rep Power
    0

    Default Re: Separating codeindexOf/substring methods + Using for different primitives/objects

    ok i get that, and the character is identified, now how do i separate a section of a String thats, say, between ends of the String, that is basically unknown until the program scans in the user input?

Page 1 of 2 12 LastLast

Similar Threads

  1. Calling methods on objects stored in an array
    By Dreaming in forum New To Java
    Replies: 6
    Last Post: 10-30-2011, 12:50 AM
  2. Methods and Objects.
    By jim01 in forum New To Java
    Replies: 4
    Last Post: 05-15-2011, 09:07 PM
  3. Replies: 1
    Last Post: 02-03-2011, 05:43 PM
  4. creating objects by methods automatically
    By thedarlington in forum New To Java
    Replies: 13
    Last Post: 02-07-2010, 10:48 PM
  5. Separating program logic and GUI
    By loopingman in forum AWT / Swing
    Replies: 0
    Last Post: 12-08-2008, 02:46 PM

Tags for this Thread

Posting Permissions

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