13 Replies Latest reply: Sep 28, 2007 2:25 AM by 3004 RSS

    Character wrapper ?

    807600
      I'm trying to wrap a set of strings into character objects, below.
      The StringEditor class needs to create MyList objects but the
      formal parameters for the StringEditor constructors are strings.
      So I need to figure out how to wrap them into character objects.
      Can someone tell me how?
      Thanks,
      MB


      public class StringEditor {

      public StringEditor() {
      MyList one = new MyList();
      }

      public StringEditor(String left, String right) {
      MyList two = new MyList(left);
      MyList three = new MyList(right);
      }


      public class MyList {

      private int numItems;
      private Object item;
      private MyList next;

      public MyList() {

      }

      public MyList(MyList lst) {
      next = lst;

      }
        • 1. Re: Character wrapper ?
          796125
          Okay, I really don't get this question. At which point in time do they need to be "wrapped as character objects"? Before callling the constructor? Inside the constructor? Inside the MyList?

          In fact, what do you mean by "character objects". Do you mean an array of java.lang.Character?

          - Adam
          • 2. Re: Character wrapper ?
            807600
            Sorry, I think I made it too confusing.
            I just need to know how to "wrap" a string into character objects.
            e.g. abcdefg

            MB
            • 3. Re: Character wrapper ?
              DrClap
              MarkBaum wrote:
              Sorry, I think I made it too confusing.
              I just need to know how to "wrap" a string into character objects.
              No, it's the concept of "wrapping a string into character objects" that we are finding confusing. From the Java point of view that is just a bunch of words that don't make sense when you put them together in that order.

              Can you back up and explain the requirement that you thought that was a solution for?
              • 4. Re: Character wrapper ?
                807600
                Yes, I stated it wrong.

                I want to change a character or a string into an object of the Object class.

                e.g. how would I change Char c or String s into an object?
                • 5. Re: Character wrapper ?
                  807600
                  MarkBaum wrote:
                  Yes, I stated it wrong.

                  I want to change a character or a string into an object of the Object class.

                  e.g. how would I change Char c or String s into an object?
                  String s is already an object,

                  there's no class called Char in the standard API unless you roll your own. Character c, however, is an object.
                  • 6. Re: Character wrapper ?
                    807600
                    Sorry, I mean how would I change

                    Character c into an object?

                    Thanks,
                    Mark
                    • 7. Re: Character wrapper ?
                      3004
                      MarkBaum wrote:
                      Sorry, I mean how would I change

                      Character c into an object?
                      You don't.

                      An instance of Character is already an object. The variable c is a reference variable that points to that object.

                      It's not clear to me what you're trying to do.
                      • 8. Re: Character wrapper ?
                        3004
                        MarkBaum wrote:
                        Sorry, I think I made it too confusing.
                        I just need to know how to "wrap" a string into character objects.
                        WTF does this mean?

                        Do you want chars, which are not objects: 'a', 'b', 'c', etc.?

                        Or do you want Characters, which are objects?

                        What are you trying to accomplish?
                        • 9. Re: Character wrapper ?
                          3004
                          MarkBaum wrote:
                          Yes, I stated it wrong.

                          I want to change a character or a string into an object of the Object class.
                          This still makes no sense.

                          Every instance of Character and every instance of String is also an instance of Object.

                          e.g. how would I change Char c or String s into an object?
                          They're already objects.

                          Again: What are you trying to accomplish?
                          • 10. Re: Character wrapper ?
                            807600
                            Sorry for all the confusion.

                            Here is a related problem I am having.

                            I have declared MyList objects in a different class StringEditor and I want to
                            place these object into a StringEditor constructor and equate them to the formal
                            parameters of this contructor. The problem is that the parameter of the
                            StringEditor constructor are String objects. How can I change the Mylist
                            objects so that I can equate the String object with the Mylist object.

                            See below. (left = leftChars; right = rightChars;)


                            public class StringEditor {

                            MyList leftChars;
                            MyList rightChars;

                            // Effect: StringEditor object is created
                            // Postcondition: no characters are in the string
                            public StringEditor() {
                            }

                            // Effect: StringEditor object is created
                            // Precondition: parameters strings are non-null
                            // Postcondition: characters from strings left and right are represented
                            // in the editor
                            public StringEditor(String left,  String right) {

                            left = leftChars;
                            right = rightChars;
                            }
                            • 11. Re: Character wrapper ?
                              3004
                              I dont know what you mean by "equate the String object with the Mylist object," but it sounds like you want a String representation of your MyList object. The most common way to do that would be by overrideing MyList's toString method and passing its results.

                              Note that when you do that you've got a String. It's a completely separate object, and there's not necessarily any way to create a MyList from that String. There's no way to magically transform objects from one type to another and back.
                              • 12. Re: Character wrapper ?
                                800308
                                wtf is a MyList
                                • 13. Re: Character wrapper ?
                                  800308
                                  There's no way to magically transform objects from one type to another and back.
                                  But but... The fairy godmother turned my pumpkin into a coach.