Results 1 to 20 of 20
Thread: Make String into chars
- 06-20-2010, 12:27 PM #1
Member
- Join Date
- May 2010
- Posts
- 44
- Rep Power
- 0
- 06-20-2010, 12:48 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
- 06-20-2010, 01:20 PM #3
Member
- Join Date
- May 2010
- Posts
- 44
- Rep Power
- 0
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'??
- 06-20-2010, 01:24 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
- 06-20-2010, 01:26 PM #5
Member
- Join Date
- May 2010
- Posts
- 44
- Rep Power
- 0
because those are the rules for my assignment...
- 06-20-2010, 01:45 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
- 06-20-2010, 01:49 PM #7
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Or at least fully specify your assignment here so we can decide for ourselves just what is allowed or not.
- 06-20-2010, 02:39 PM #8
Member
- Join Date
- May 2010
- Posts
- 44
- Rep Power
- 0
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:
Class StringList represents 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; } }
The task is to add the following methods to StringList:Java Code:public class StringList{ private CharNode _head; public StringList( ) { _head = null; } public StringList(CharNode node) { _head = node; } }
1.which returns the char in place i of the string. Assume that i is in the correct range.Java Code:public char charAt(int i)
2.which adds the string str to the end of the linked list (as nodes...)Java Code:public StringList concat (String str)
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!
- 06-20-2010, 02:46 PM #9
If you can't use any of the String methods, I don't see how you will get the chars out of it.public StringList concat (String str)
Are you allowed to use other classes that can like StringBuffer?
- 06-20-2010, 02:52 PM #10
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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.
Please show what you've tried to do to solve the second.The task is to add the following methods to StringList:
1.which returns the char in place i of the string. Assume that i is in the correct range.Java Code:public char charAt(int i)
2.which adds the string str to the end of the linked list (as nodes...)Java Code:public StringList concat (String str)
There are more methods to add but I will stop here. I answered the first question but am stuck on the second.
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?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 }
- 06-20-2010, 03:13 PM #11
Member
- Join Date
- May 2010
- Posts
- 44
- Rep Power
- 0
It seems to be working fine for me: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?
and it returns b which is the correct value. If it is incorrect, can you give me a word which will return incorrectly??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)); } }
I don't know how to solve this. Can you maybe give me some direction???Please show what you've tried to do to solve the second.
Thanks
- 06-20-2010, 03:23 PM #12
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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:
and is not correct.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
- 06-20-2010, 03:32 PM #13
Member
- Join Date
- May 2010
- Posts
- 44
- Rep Power
- 0
I don't get what you did. Why did you make those changes?? My Tester class shows that I wrote it correctly.Is it now?
- 06-20-2010, 03:42 PM #14
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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.
- 06-20-2010, 03:47 PM #15
Member
- Join Date
- May 2010
- Posts
- 44
- Rep Power
- 0
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???
- 06-20-2010, 03:51 PM #16
"Correctly" if one of 9 values is in the correct place. What about the other 8 values as shown in curmudgeon's output?
- 06-20-2010, 04:00 PM #17
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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
- 06-20-2010, 04:03 PM #18
Member
- Join Date
- May 2010
- Posts
- 44
- Rep Power
- 0
I made minus 1 to the if commands to get exact match...
but it still returns: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(); // }
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??
- 06-20-2010, 04:07 PM #19
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Please see pseudo-code above for one possible algorithm.
- 06-20-2010, 04:24 PM #20
Member
- Join Date
- May 2010
- Posts
- 44
- Rep Power
- 0
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
-
Swap chars in a String?
By spatel14 in forum New To JavaReplies: 5Last Post: 06-08-2010, 09:05 PM -
make a string as a link
By pharo in forum AWT / SwingReplies: 1Last Post: 04-26-2009, 12:23 PM -
Replacing the chars within a string.
By Mayur in forum New To JavaReplies: 2Last Post: 03-27-2009, 04:00 AM -
How to print chars with symbols from a string
By blacksky in forum New To JavaReplies: 23Last Post: 01-06-2009, 01:14 PM -
make a variable name from a string?
By Kinnikinnick in forum New To JavaReplies: 3Last Post: 11-13-2007, 03:54 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks