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

Thread: Need some help with substring please

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Need some help with substring please

    Hi,
    I am creating a seating plan generator for my computing coursework and need to separate a string into parts:

    "Barn, Sam",10,AG,M,07.08.97
    "Smith, Trevor James",10,GB,M,25.03.97
    "Paddington, Kieran Simon",10,AG,M,10.09.96
    "Benton, Rebecca",10,AT,M,31.10.96

    It will be read in as ClassCodeString.
    I need each of their names, Gender (M or F) and the last 6 digits(DOB).
    The user will put in different students each time so the positions of the names, gender and DOB will change, but the symbols separating them will remain the same.

    Any help would be greatly appreciated


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Need some help with substring please

    Which part of this is giving you trouble? What have you tried? Where are you stuck? Did you read the API for String?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some help with substring please

    I want to store the each person's name in an array, their genders in another, and their DOB in the last. I've tried using substring but it didn't work very well.
    I have no idea what an API is so this probably in the wrong section, sorry.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Need some help with substring please

    This is the API, it lists classes and their methods: Java Platform SE 6

    You're going to have to be a lot more specific. What exactly do you mean when you say "it didn't work very well"? What happened? I recommend you create an SSCCE that demonstrates exactly what you're talking about, as well as a specific technical question, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some help with substring please

    I don't have any code for it because I have no idea how to do it, which is why I am here.

    I need someone to show how I can make a program that takes the names and the gender for each of the students from the following:

    "Barn, Sam",10,AG,M,07.08.97
    "Smith, Trevor James",10,GB,M,25.03.97
    "Paddington, Kieran Simon",10,AG,M,10.09.96
    "Benton, Rebecca",10,AT,M,31.10.96

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Need some help with substring please

    You seem to know that you need to use substring. How would you get the gender from that String using substring? How do you do it without a computer?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some help with substring please

    Well first of all I need to get the name between the speech marks and store that in an array. Then I need to find the second name and put that in the same array. and so on...
    How would I do that?

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Need some help with substring please

    Write out some rules, in English (not code) about how to find those things in the String. If somebody gave you a piece of paper with this written on it, how do you know where to look for that information?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some help with substring please

    Everything inside the speech marks is the name,
    After 3 commas you will find a letter that represents the gender,
    That is one person's information,
    Once you hit the next speech marks it means you have reached the next person's information, and follow the previous instructions.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help with substring please

    Look at the String class API doc. It has methods for finding the index values (location of) for short strings in a longer String. You could use that repeatedly to find the third comma.

    Or you could use a loop that looks at each character in a String (using charat() method) and count the commas until you get to where the beginning and then again to the end of the substring where the data is you want to get using the substring method.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some help with substring please

    So would the charat method be like:

    if(classCode.charat(0)==","){

    }

    When I try that I get an error saying "Incomparable types: char and java.lang.string"

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help with substring please

    A char literal is inside 's not "s
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    iijakeh (April 6th, 2012)

  14. #13
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some help with substring please

    It's a bit messy, but i tried to get the counter to work first. It's supposed to print out where the first speech marks are and where the second speech marks are. I get this error:
    java.lang.StringIndexOutOfBoundsException: String index out of range: 12
    The number at the end of the error is the number of characters the string I typed in was.

    String ClassCodeString = ClassCode.getText();
     
    boolean found=false;
    int counterTemp1=0;
    int counterTemp2=0;
     
    for(int counter=0;ClassCodeString.length()>counter;counter++){
     
        found=false;
     
        if(ClassCodeString.charAt(counter)=='"'){
     
            counterTemp1=counter;
     
            for(;found==false;counter++){
     
                if(ClassCodeString.charAt(counter+1)=='"'){
     
                    counterTemp2=counter+1;
     
                }
            }
            System.out.println("counterTemp1 = "+counterTemp1);
            System.out.println("counterTemp2 = "+counterTemp2);
        }
    }

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help with substring please

    Remember that indexes are 0 based. The max allowed value is the length-1
    The max index for 12 chars is 11
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some help with substring please

    I did this:

    int ClassCodeLength = ClassCodeString.length();

    for(int counter=0;ClassCodeLength-1>counter;counter++){
    ...

    But I'm still getting the same error

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help with substring please

    Does the code use the value of counter only? It looks like it adds to it (+1)
    If you don't understand my answer, don't ignore it, ask a question.

  18. #17
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some help with substring please

    Sorry, I don't understand what you mean.

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help with substring please

    If counter has a value of 11, counter+1 will be 12

    Add a println to print out the value of counter inside the loop to see what its value is. Be sure to print it out every time its value is changed.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #19
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some help with substring please

    Ah, don't worry, it was because I forgot to end the last for loop.
    Now I just have to find a way to save the names to an array.
    Last edited by iijakeh; April 7th, 2012 at 08:11 AM.

Similar Threads

  1. substring StringIndexOutOfBoundsException
    By ddonn in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 9th, 2012, 02:34 PM
  2. Sorting substring?
    By alpvii in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 23rd, 2011, 05:39 AM
  3. replacement substring
    By dcshoecousa in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 9th, 2011, 04:28 PM
  4. SUBSTRING
    By cutee_eyeh in forum Object Oriented Programming
    Replies: 1
    Last Post: November 12th, 2010, 03:57 AM
  5. Substring help
    By Roach in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 21st, 2010, 07:57 AM