Java Programming: From Problem Analysis to Program Design: Chapter 3: pg 118 to 126 Strings

Jordan O: Please paraphrase the section on Strings for the class.  Everyone should be adding to Jordan’s post, with Questions, Comments, Corrections, Additions… Please have this section read and commented upon by Friday September 29th.

14 thoughts on “Java Programming: From Problem Analysis to Program Design: Chapter 3: pg 118 to 126 Strings

  1. ***My Response to Pages 118-126***

    In this section we learn how to use String methods to manipulate strings.

    The String class allows us to use various methods to manipulate strings in many different ways.

    For an example consider the following methods.
    1.) Using the + operator one can join two different strings to form one string.
    2.) Using the method length one can find the length of a string.
    3.) Using the method substring one can extract part of a string.

    *For a larger list of commonly used methods, turn to pages 122 and 123 in the textbook.*

    ***Important Note— Because the Java system automatically creates the class String available, we don’t need to import the class. Also it is important to know the position of the first character in a string is 0, therefore consider the following-

    String pet = new String (“ Dog”);

    The length of pet would be 3, because length is the number of characters in the string.

    To use the substring method we must know two things.
    1.) The position of the first character of the substring being determined by our string.
    2.) The position of the last character of the substring being determined by our string.

    The general statement to use the method substring is:
    StringVariable.substring(startIndex, endIndex)

    Ex.
    String weather = new String (“Today the sky is clear.”);
    String weather2 = weather.substring(6, 22);

    This statement would return “the sky is clear.” Because at index 6 the character is “t” and at index 22 the character is “.” Therefore the method returns all characters from “t” to “.”

    Final important notes:
    1.) String variables are reference variables
    2.) A string object is an instance of the class String.
    3.) The class String has many methods used to manipulate strings.
    4.) String variables invoke a String method using the dot operator (a period), the method name, and the set of arguments (if any) required by the method.

    I hope this helped some people better understand these concepts.

  2. Good Job Jordan, one thing that I dont think you made very clear that is a confusing thing is that when you use length, it starts counting at one whereas when you use index’s it starts at 0. Its confusing to remember which one starts at what.

    ex. the length of “dog” is three, but the index’s of “dog” is d-0 o-1 g-2.

  3. Well this did help some, but I didn’t have too many problems with all this stuff about strings. Though you didn’t yell anyone that using the + operator is called a cancatination. Ok so I cannot spell it but I think I’m correct. Just a piece of info that we kind of need to know in this course.

  4. You did a good job Jordan. But I just have one thing to add and that being that in order to use a String method you need to know its name, parameters, and what the method does.

  5. I like the clarity of the notes, you did a great job. But because we have new students in the class this year, you should clarify that when finding string length you count each letter like you would count anything else, starting with 1. Therefore “Dog” would be three characters long. But when extracting words from a phrase, for example if you take ” Today the sky is clear.” and you want to extract “the sky is clear.”you would need to start at character 6 and end at 22, but we start at character 6 because when counting characters you start at zero and spaces also count as characters. This confused me alot when we first did strings and substrings because i was so used to counting starting at 1 and not counting spaces.

  6. Awesome job, um i am curious about the use of substrings…how we are able to join them together to form bigger strings.. is there a limit to how many strings we can connect?

  7. One thing that I found that you had said incorectly is that your endIndex is subtracted by one. So if your endIndex is 8 You subtract 1 and it becomes 7. When I first looked at that I was so confused, I had no idea what was going on. After some help from Mrs. Furman I figured it out. When I looked at what jorden said i figured it was a little off so I had to offer my bit of information.

  8. Since the issue of length and indexes is being discussed a lot. Let me try to tie this in with memory.

    Length just deals with the number of characters (spaces, puncuation, etc are included) a string contains.

    The indexes actually deal with how far from the beginning of the String we would need to go to get to the character. So, “Dog”, D is at index 0, because we do not need to move any spaces from the beginning of the String to get to D. The index of “o” is 1, because we need to move 1 space from the beginning to get to the “o”. And “g” is 2, because we move 2 spaces from the beginning to get to the “g”.

    I believe that there is an error in Jordan’s notes about substring. The first parameter is the starting index, but the second parameter, while called endIndex, is actually 1 more than the endIndex. So we do endIndex – 1 . So, in Jordan’s example we should not be getting the “.” from the weather statement, just “the sky is clear”

  9. One thing that is hard to get use to is the fact that we have to start at ZERO!!!!!

    These methods are not the only menthods there others that are just important like :

    char charAt (int index)// this returns a character at the given index

    Also another inportant one is :

    String replace(char charToBeReplaced, char char Replaced With)

    These are just two more I think are also important!!!!

  10. Good job Jordan. The first thin i noticed when i read the blog was that indexes were not entirely explained. Spaces and punctuation are counted as characters int he counting of the length of strings. for example: the string “Mrs.Furman rocks” would have a length of 16. M-1 r-2 s-3 .-4 F-5 u-6 r-7 m-8 a-9 n-10 ” “-11 r-12 o-13 c-14 k-15 s-16. this class may seem confusing at first but once you get the hang of it, it makes sense and is very fun.

  11. ooooooppppppsssss!!!!!!!!
    I made a mistake
    char chatAt(char cahrToBeReplaced, char charReplacedWith)
    there should no space between the char charReplacedWith

  12. Jordan,
    As I read your post, I remember that this is the class in our high school where I always feel completely lost. You did a great job of highlighting the important points in a clear, succinct way. I thought your organization and clarity were strong. Great job in a really tough subject. I’m proud of you and Courtney. You’ve both set the bar high so that your classmates better measure up.
    Mrs. Moritz

  13. Pingback: G-Town talks » We don’t need no thought control.