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

Thread: How do i remove a substring from an element in an array

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default How do i remove a substring from an element in an array

    for(int i=0;i<entries.size();i++){

    //if it has java or the correct answer it will say its valid
    // if not it will remove the element from the array

    if(entries.get(i).contains("java")){
    System.out.println("Is valid");
    }else if(entries.get(i).contains(correctAnswer)){
    System.out.println("Is valid");
    }else{
    entries.remove(entries.get(i));
    }
    }

    //create a new list that will store the winners
    ArrayList<String> winners = new ArrayList<String>();
    //generate random number from 0 to 100
    int rand = (int)(Math.random() * 100);
    //while the array size is less then the prizeCount add another random element from array "entries"
    //until it is the same size as the prizeCount
    while(winners.size()<prizeCount){
    winners.add(entries.get(rand));
    }

    return winners;
    //remove java and correct answer from the elements in the list
    for(int i=0; i<winners.size();i++){


    this bit here is where im lost im trying to remove a substring from the array of elements and return the array in assecding order
    so ill have java <answer> john 0872972956
    turned into john 0872972956
    and have the list of names and numbers in alphabetical order.
    your help would be much appreciated and im sry if its not clear what i dont know


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: How do i remove a substring from an element in an array

    Quote Originally Posted by jonathanfox View Post
    ...your help ...
    Here is how I might test the removal scheme: I would make the program tell me what is working on and what it is doing at each step.

    Maybe something like:
    import java.util.ArrayList;
    import java.util.Arrays;
     
    public class z
    {
        public static void main(String [] args)
        {
            ArrayList<String> entries = new ArrayList<String>
            (
                Arrays.asList("abc","def", "javaxxx", "1242")
            );
     
            String correctAnswer = "42";
     
            for(int i = 0; i < entries.size(); i++)
            {
                System.out.printf("Top of loop: i = %d, size = %d: %s\n",
                        i, entries.size(), entries, entries.size());
     
                if(entries.get(i).contains("java"))
                {
                    System.out.printf("%d: %s contains \"java\".\n",
                            i, entries.get(i));
                }
                else if(entries.get(i).contains(correctAnswer))
                {
                    System.out.printf("%d: %s contains the correct answer.\n",
                            i, entries.get(i));
                }
                else
                {
                    System.out.printf("Removing entries.get(%d): \n",
                            i, entries.get(i));
                    entries.remove(entries.get(i));
                    System.out.printf("After removal: %s\n", entries);
                }
                System.out.println();
            }
            System.out.printf("After removal of spurious Strings: %s\n", entries);
        }
    }

    Output:

    Top of loop: i = 0, size = 4: [abc, def, javaxxx, 1242]
    Removing entries.get(0):
    After removal: [def, javaxxx, 1242]

    Top of loop: i = 1, size = 3: [def, javaxxx, 1242]
    1: javaxxx contains "java".

    Top of loop: i = 2, size = 3: [def, javaxxx, 1242]
    2: 1242 contains the correct answer.

    After removal of spurious Strings: [def, javaxxx, 1242]

    Look carefully and figure out why "def" was not removed.


    If it is not clear what I don't know about your assignment, please enlighten me.

    In other words, if that does not help with whatever it is that you had in mind, maybe you can explain specifically what you intend. Such a description is called a Program Specification.

    Additionally...

    If you want to return something in sorted order, then you have to sort it. I'm thinking that would be a separate task after unwanted elements are removed, right?


    Cheers!

    Z
    Last edited by Zaphod_b; July 23rd, 2012 at 01:16 PM.

  3. The Following User Says Thank You to Zaphod_b For This Useful Post:

    jonathanfox (July 24th, 2012)

  4. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Smile Re: How do i remove a substring from an element in an array

    okay first of all how do i post my code neatly like yours and ill show u the question we were given, ur code is hard to understand cus im terrible at java, and i didnt post all the code.

    In a radio programme quiz the entrants text their answers to a specified number. The text must start with the word JAVA followed by the answer (in angle brackets <> as <answer>) and then the entrants name and phone number. Any number of spaces can be used to separate the parts of the text but there is always at least one space between each part. The answer is always a single word or letter so there are no spaces in the answer. For example, the following are all valid entries
    JAVA <ANSWER> EDSGAR DIJSKTRA 023 7897654
    java <answer> donald knuth 056 54388765
    JaVA <Answer> Superman 999
    jAvA <answer> Wirth,Niklaus 082 75123423

    The entries have been stored in an ArrayList of Strings declared as follows
    ArrayList<String> entries ;
    Write the code to for a method with the following header

    public ArrayList<String> winners(ArrayList<String> entries, String correctAnswer, int prizeCount)

    The method is passed an ArrayList containing ALL the entries, a string containing the correct answer (without angle brackets) and an integer indicating the number of prizes available.
    The method should remove any entries that do not start with the word JAVA as well as those that do not have the correct answer. The method then randomly chooses prizeCount entries from the remaining correct entries and these entries are deemed to be the winners. Note, it is possible that the number of correct entries could be less than prizeCount. In that case all of the correct entries are chosen as winners.
    The winners are returned in an ArrayList of Strings that contains the original entry with the word JAVA and the answer removed - only the name and phone number are left. The strings should be sorted by ascending entrant name, which will now be at the start of the string. Because the names may be submitted in all sorts of styles just sort the list using the names as they were submitted. DO NOT try to identify the surname and first name parts - it is NOT required.
    thanks for your help

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How do i remove a substring from an element in an array

    how do i post my code neatly like yours
    Wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How can I remove a element from an array?
    By seaofFire in forum Java Theory & Questions
    Replies: 3
    Last Post: April 23rd, 2012, 03:42 PM
  2. How do we fill in an array using substring, FileReader and a txt file?
    By wholegrain in forum Java Theory & Questions
    Replies: 5
    Last Post: February 12th, 2012, 03:18 PM
  3. Subtracting each element from an array
    By Tronez in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 15th, 2011, 04:41 PM
  4. add new element in array
    By allen_k in forum Collections and Generics
    Replies: 3
    Last Post: December 11th, 2011, 03:13 PM
  5. Finding a substring from an array within a string argument
    By sitruz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 1st, 2011, 10:33 AM

Tags for this Thread