Results 1 to 2 of 2
  1. #1
    noble is offline Member
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    Default Insert String at End of ArrayStringLog

    Trying to figure out how to insert a String at the "End" of an Array String Log. Here is the code I have so far, right now the "insert" and "insertAtEnd" are obviously doing the same thing but I'm not sure how to make it do otherwise:

    public void insert(String element)
    // Precondition: This StringLog is not full.
    //
    // Places element into this StringLog.
    {
    if (isFull())
    lastIndex++;
    else {
    lastIndex++;
    log[lastIndex] = element;
    }
    }
    public void insertAtEnd(String element)
    {
    if (isFull())
    lastIndex++;
    else{
    lastIndex++;
    log[lastIndex] = element;
    }
    }


    I'm using a class called UseStringLog to insert strings using this method:

    log.insert("String 1");
    log.insert("String 2");

    etc.

    and it prints like this:

    1. String 1
    2. String 2

    etc.

    How would I change the "insertAtEnd" to get the string to insert before "String 1"?

  2. #2
    noble is offline Member
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    Default

    Here is the entire ArrayStringLog class if that helps:

    public class ArrayStringLog implements StringLogInterface {
    protected String name; // name of this StringLog
    protected String[] log; // array that holds strings
    protected int lastIndex = -1; // index of last string in array

    public ArrayStringLog(String name, int maxSize)
    // Precondition: maxSize > 0
    //
    // Instantiates and returns a reference to an empty StringLog object
    // with name "name" and room for maxSize strings.
    {
    log = new String[maxSize];
    this.name = name;
    }

    public ArrayStringLog(String name)
    // Instantiates and returns a reference to an empty StringLog object
    // with name "name" and room for 100 strings.
    {
    log = new String[100];
    this.name = name;
    }

    public void insert(String element)
    // Precondition: This StringLog is not full.
    //
    // Places element into this StringLog.
    {
    if (isFull())
    lastIndex++;
    //throw new FullLogException("Full Log\n");
    else {
    lastIndex++;
    log[lastIndex] = element;
    }
    }
    public void insertAtEnd(String element)
    {
    if (isFull())
    lastIndex++;
    else{
    lastIndex++;
    log[lastIndex] = element;
    }
    }

    public boolean isFull()
    // Returns true if this StringLog is full, otherwise returns false.
    {
    if (lastIndex == (log.length - 1))
    return true;
    else
    return false;
    }

    public int size()
    // Returns the number of Strings in this StringLog.
    {
    return (lastIndex + 1);
    }

    public boolean contains(String element)
    // Returns true if element is in this StringLog,
    // otherwise returns false.
    // Ignores case differences when doing string comparison.
    {
    boolean moreToSearch;
    int location = 0;
    boolean found = false;
    moreToSearch = (location <= lastIndex);

    while (moreToSearch && !found)
    {
    if (element.equalsIgnoreCase(log[location])) // if they match
    found = true;
    else
    {
    location++;
    moreToSearch = (location <= lastIndex);
    }
    }

    return found;
    }

    public void clear()
    // Makes this StringLog empty.
    {
    for (int i = 0; i <= lastIndex; i++)
    log[i] = null;
    lastIndex = -1;
    }

    public String getName()
    // Returns the name of this StringLog.
    {
    return name;
    }

    public String toString()
    // Returns a nicely formatted string representing this StringLog.
    {
    String logString = "Log: " + name + "\n\n";

    for (int i = 0; i <= lastIndex; i++)
    logString = logString + (i+1) + ". " + log[i] + "\n";

    return logString;
    }
    }

Similar Threads

  1. How can i insert a char into a string
    By Jamie in forum New To Java
    Replies: 8
    Last Post: 02-17-2011, 08:59 PM
  2. insert a row into the DB
    By miko5054 in forum JDBC
    Replies: 13
    Last Post: 07-11-2010, 02:01 PM
  3. jsp insert into database error(java.lang.NumberFormatException: For input string: "")
    By cypher_girl in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-22-2009, 03:14 AM
  4. Replies: 3
    Last Post: 02-28-2009, 09:17 AM
  5. SQL Insert Help!!!!
    By shaungoater in forum New To Java
    Replies: 1
    Last Post: 06-14-2008, 03:14 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
  •