I'm writting a program that at some point has to push onto a stack or enqueue in a queue characters from a string. I tried converting the characters to objects before pushing/enqueueing but the compiler complains about inconvertible types. Enough talking here is the code (the lines in bold are the ones giving me errors):
And here is the errors I got when I compiled:Code:public class Prog3 {//start public static void main ( String args [] ) {//begin main StackListBased stack1 = new StackListBased(); // create new stack QueueListBased queue1 = new QueueListBased(); // create new queue Character dollarSign = new Character ('$'); // store dollar sign in dollarSign Character compareStack = new Character ('s'); //character for comparison purposes Character compareQueue = new Character ('q'); //character for comparison purposes String sentence; // create empty string int strLength; // integer variable to store length of string int SignCount = 0; //store position of $ in string int count = 0; //counter used for queue insertion int count2 = 0; // counter used for stack pushing boolean good = true; //boolean value System.out.println ("Enter a string seperated by a $ sign: "); //prompt user to enter a formula sentence = Keyboard.readString(); //store input in sentence strLength = sentence.length(); //return the length of string and save in strLength //determine position of $ and store in SignCount while ( sentence.charAt ( SignCount ) != dollarSign.charValue()) { SignCount ++; } //insert characters before $ in queue while ( count < SignCount ) { queue1.enqueue( (Character)sentence.charAt ( count ) ); count ++; } count2 = SignCount + 1; //push character after $ onto stack while ( count2 <= strLength ) { stack1.push( (Character)sentence.charAt ( count2 ) ); count2 ++; } //pop stack and dequeue queue while both are not empty while ( !stack1.isEmpty() && !queue1.isEmpty() ) { compareStack = (Character)stack1.peek(); //peek at stack and store peeked value here compareQueue = (Character)queue1.peek(); //peek at queue and store peeked value here // if peeked values are equal pop stack and dequeue queue if ( compareStack.charValue() == compareQueue.charValue() ) { stack1.pop(); queue1.dequeue(); } else { //if peeked values are not equal change boolean value and break good = false; break; } } //display if ( good == true ) System.out.println ("String balanced." ); else System.out.println ("String unbalanced." ); } }
[compiler]
C:\Documents and Settings\battlefield\Desktop\prog3-java\Prog3.java:47: inconvertible types
found : char
required: java.lang.Character
queue1.enqueue( (Character)sentence.charAt ( count ) );
^
C:\Documents and Settings\battlefield\Desktop\prog3-java\Prog3.java:56: inconvertible types
found : char
required: java.lang.Character
stack1.push( (Character)sentence.charAt ( count2 ) );
^
2 errors
[/compiler]
I included the zip file (pro3-java) which has prog3 in it and all the other classes. You just need to compile/run prog3.java if you want to check it out that is. Your help will be a lot appreciated


Reply With Quote



Bookmarks