DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2002
    Posts
    6

    converting characters to Objects question

    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):

    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." );
    
    	}
    }
    And here is the errors I got when I compiled:

    [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
    Attached Files

  2. #2
    Join Date
    Jul 2002
    Posts
    26
    Instead of this:

    (Character)sentence.charAt ( count )

    try this:

    new Character( sentence.charAt ( count ) )

    You can't cast a primative char to a Character Object, but you can create a Character Object from a primative char.

    -gc

  3. #3
    Join Date
    Nov 2002
    Posts
    6
    Originally posted by godcomplex
    Instead of this:

    (Character)sentence.charAt ( count )

    try this:

    new Character( sentence.charAt ( count ) )

    You can't cast a primative char to a Character Object, but you can create a Character Object from a primative char.

    -gc
    Thnx alot godcomplex meh program is working now

    /me so happy

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links