Results 1 to 20 of 20
  1. #1
    myst is offline Member
    Join Date
    May 2010
    Posts
    44
    Rep Power
    0

    Default Make String into chars

    Hi. I'm blanking out. Can someone please tell me how I can make the String "happy" into chars 'h' 'a' 'p' 'p' 'y'.


    Thanks.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by myst View Post
    Hi. I'm blanking out. Can someone please tell me how I can make the String "happy" into chars 'h' 'a' 'p' 'p' 'y'.
    Read the API documentation for the String class; it has a method that converts a String to a char array.

    kind regards,

    Jos

  3. #3
    myst is offline Member
    Join Date
    May 2010
    Posts
    44
    Rep Power
    0

    Default

    Thanks but I can't use methods from String class.

    I guess I just want to know if this is true:

    String s = "happy"

    s[0] = 'h'??

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by myst View Post
    Thanks but I can't use methods from String class.
    Why not?

    kind regards,

    Jos

  5. #5
    myst is offline Member
    Join Date
    May 2010
    Posts
    44
    Rep Power
    0

    Default

    because those are the rules for my assignment...

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by myst View Post
    because those are the rules for my assignment...
    It can't be done then: you need methods from the String class to be able to do anything with your String, e.g. length(), charAt() are both methods. Telll your teacher s/he's a moron.

    kind regards,

    Jos

  7. #7
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Or at least fully specify your assignment here so we can decide for ourselves just what is allowed or not.

  8. #8
    myst is offline Member
    Join Date
    May 2010
    Posts
    44
    Rep Power
    0

    Default

    ouch! My teachers are definitely not morons. My question really does not have much to do with my assignment. But it would help me understand how to answer part of it.

    Well, I may as well tell you my assignment...

    We want to represent a string of chars as a linked list. Every node has 3 fields. 1) the letter itself 2) The number of times it appears successively 3) Pointer to the next node in the list

    for example, the string "aabbbcca" is represented as
    a|2 (next)----b|3 (next)----c|2 (next)----a|1 (null)

    Class CharNode represents a node in the list:

    Java Code:
    public class CharNode{
         private char _data;
         private int _value;
         private CharNode _next;
         public CharNode(char c, int val, CharNode n) {
            _data = c;
            _value = val;
            _next = n;
         }
         public CharNode getNext( ) { return _next; }
         public void setNext(CharNode node) { _next = node; }
         public int getValue() { return _value; }
         public void setValue(int v) { _value = v; }
         public char getData() { return _data; }
         public void setData(char c) { _data = c; }
    }
    Class StringList represents the list:

    Java Code:
    public class StringList{
         private CharNode _head;
         public StringList( ) {
            _head = null;
         }
         public StringList(CharNode node) {
            _head = node;
         }
    }
    The task is to add the following methods to StringList:

    1.
    Java Code:
    public char charAt(int i)
    which returns the char in place i of the string. Assume that i is in the correct range.

    2.
    Java Code:
    public StringList concat (String str)
    which adds the string str to the end of the linked list (as nodes...)

    There are more methods to add but I will stop here. I answered the first question but am stuck on the second.

    Here's what I wrote for public char charAt(int i):

    Java Code:
    public char charAt(int i)
            CharNode currentNode = _head;
            if(currentNode.getValue() == i) // if i equals value
                return currentNode.getData();
            if(currentNode.getValue() > i) // if the first node's value is bigger than i, that just means that the same letter is used many times in a row...
                return currentNode.getData();
            while(currentNode.getValue() < i){ // stops when the value is larger or equal to i
                currentNode.getNext().setValue(currentNode.getNext().getValue() + currentNode.getValue());
                currentNode = currentNode.getNext(); 		
            }
            return currentNode.getData(); // and then you just return that value
        }

    Hopefully you guys understand the question and can help me solve the second method.

    Thanks!

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    public StringList concat (String str)
    If you can't use any of the String methods, I don't see how you will get the chars out of it.
    Are you allowed to use other classes that can like StringBuffer?

  10. #10
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by myst View Post
    ouch! My teachers are definitely not morons. My question really does not have much to do with my assignment. But it would help me understand how to answer part of it.
    But don't you see that there is absolutely no way that we could help you with your original question without knowing the information you've posted just now? You could have saved yourself and us a lot of trouble by giving all pertinent information in your first post. I say this mainly for future reference.


    The task is to add the following methods to StringList:

    1.
    Java Code:
    public char charAt(int i)
    which returns the char in place i of the string. Assume that i is in the correct range.

    2.
    Java Code:
    public StringList concat (String str)
    which adds the string str to the end of the linked list (as nodes...)

    There are more methods to add but I will stop here. I answered the first question but am stuck on the second.
    Please show what you've tried to do to solve the second.


    Here's what I wrote for public char charAt(int i):

    Java Code:
    public char charAt(int i)
            CharNode currentNode = _head;
            if(currentNode.getValue() == i) // if i equals value
                return currentNode.getData();
            if(currentNode.getValue() > i) // if the first node's value is bigger than i, that just means that the same letter is used many times in a row...
                return currentNode.getData();
            while(currentNode.getValue() < i){ // stops when the value is larger or equal to i
                currentNode.getNext().setValue(currentNode.getNext().getValue() + currentNode.getValue());
                currentNode = currentNode.getNext(); 		
            }
            return currentNode.getData(); // and then you just return that value
        }
    This won't work since you're confusing i with value. To do the method above correctly, you have to walk through the linked list keeping count of where you are -- use an int counter that you increment each time you move from one node to the next by value amount. Then when the counter reaches or exceeds "i", you're at your letter. Make sense?

  11. #11
    myst is offline Member
    Join Date
    May 2010
    Posts
    44
    Rep Power
    0

    Default

    This won't work since you're confusing i with value. To do the method above correctly, you have to walk through the linked list keeping count of where you are -- use an int counter that you increment each time you move from one node to the next by value amount. Then when the counter reaches or exceeds "i", you're at your letter. Make sense?
    It seems to be working fine for me:

    Java Code:
    public class Tester
    {
        public static void main(){
            CharNode node3 = new CharNode('c', 3, null);
            CharNode node2 = new CharNode('b', 3, node3);
            CharNode node1 = new CharNode('a', 3, node2);
            StringList s = new StringList(node1);
            System.out.print(s.charAt(5));
            
        }
    }
    and it returns b which is the correct value. If it is incorrect, can you give me a word which will return incorrectly??


    Please show what you've tried to do to solve the second.
    I don't know how to solve this. Can you maybe give me some direction???

    Thanks

  12. #12
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by myst View Post
    It seems to be working fine for me:
    Is it now?

    Java Code:
      public static void main(String[] args) {
        CharNode node3 = new Tester().new CharNode('c', 3, null);
        CharNode node2 = new Tester().new CharNode('b', 3, node3);
        CharNode node1 = new Tester().new CharNode('a', 3, node2);
        StringList s = new Tester().new StringList(node1);
        for (int i = 0; i < 9; i++) {
          System.out.println("s.charAt(" + i + "): " + s.charAt(i));
        }
      }

    Which returns:
    Java Code:
    s.charAt(0): a
    s.charAt(1): a
    s.charAt(2): a
    s.charAt(3): a
    s.charAt(4): b
    s.charAt(5): b
    s.charAt(6): b
    s.charAt(7): b
    s.charAt(8): b
    and is not correct.

  13. #13
    myst is offline Member
    Join Date
    May 2010
    Posts
    44
    Rep Power
    0

    Default

    Is it now?
    I don't get what you did. Why did you make those changes?? My Tester class shows that I wrote it correctly.

  14. #14
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Your Tester class only tested for one i, and my Tester class tested for a range of i, from 0 to 8, and so is a more robust test of your class. So you've just proven that your code satisfies your limited test conditions, not that your code is correct (which I can guarantee you it isn't) while my code shows that it fails a more robust test. You are free to accept my conclusions or not. Que tenga mucha suerte.

  15. #15
    myst is offline Member
    Join Date
    May 2010
    Posts
    44
    Rep Power
    0

    Default

    You are right! I tried it also from 0-8... So now I got to make some changes. Hm, what did you tell me to do again???

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    "Correctly" if one of 9 values is in the correct place. What about the other 8 values as shown in curmudgeon's output?

  17. #17
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Pseudo code:

    Java Code:
        Method charAt2 which takes int "i" as parameter
          Get the CharNode head and have currentNode refer to it
          create an int count variable and set to 0;
          repeat 
            if count plus the currentNode's value is greater than i
              then return the currentNode's data
            otherwise
              increase count by currentNode's value
              and have currentNode refer to the next node in the link
            end if/othewise
          until the current node is null
          If we've made it this far, throw an IllegalArgumentException

  18. #18
    myst is offline Member
    Join Date
    May 2010
    Posts
    44
    Rep Power
    0

    Default

    I made minus 1 to the if commands to get exact match...

    Java Code:
     public char charAt(int i){
            CharNode currentNode = _head;
            if(currentNode.getValue() - 1 == i)
                return currentNode.getData();
            if(currentNode.getValue() - 1 > i) 
                return currentNode.getData();
            while(currentNode.getValue() - 1 < i){ currentNode.getNext().setValue(currentNode.getNext().getValue() + currentNode.getValue());
                currentNode = currentNode.getNext(); 		
            }
            return currentNode.getData(); // 
        }
    but it still returns:

    char0: a
    char1: a
    char2: a
    char3: b
    char4: b
    char5: b
    char6: b
    char7: b
    char8: b

    why won't it go from b to c??? Where's my mistake??

  19. #19
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Please see pseudo-code above for one possible algorithm.

  20. #20
    myst is offline Member
    Join Date
    May 2010
    Posts
    44
    Rep Power
    0

    Default

    Your algorithm worked from 0-8 for me!

    Java Code:
        
    public char charAt(int i){
            CharNode currentNode = _head;
            int counter=0;
            while (currentNode != null){
                if (counter + currentNode.getValue() > i)
                    return currentNode.getData();
                else{
                    counter = counter + currentNode.getValue();
                    currentNode = currentNode.getNext();
                }
            }
            return currentNode.getData();
        }

    But I still don't know where my mistake is in my original attempts...

Similar Threads

  1. Swap chars in a String?
    By spatel14 in forum New To Java
    Replies: 5
    Last Post: 06-08-2010, 09:05 PM
  2. make a string as a link
    By pharo in forum AWT / Swing
    Replies: 1
    Last Post: 04-26-2009, 12:23 PM
  3. Replacing the chars within a string.
    By Mayur in forum New To Java
    Replies: 2
    Last Post: 03-27-2009, 04:00 AM
  4. How to print chars with symbols from a string
    By blacksky in forum New To Java
    Replies: 23
    Last Post: 01-06-2009, 01:14 PM
  5. make a variable name from a string?
    By Kinnikinnick in forum New To Java
    Replies: 3
    Last Post: 11-13-2007, 03:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •