Results 1 to 15 of 15

Thread: String Builder

  1. #1
    tjt
    tjt is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default String Builder

    Hello,
    Is there a way to delete a certain character?
    Totally deleting it.
    I'm trying to create a Typing Game.
    I tried this following codes which didn't work.

    Java Code:
    textTyped.deleteCharAt(textTyped.length()-1);
    textTyped.replace(textTyped.length()-1, textTyped.length(), "");
    textTyped.deleteCharAt(textTyped.length()-1); <--- adds something different not totally deleting it. It adds a character. A box with question mark inside..

    Can you suggest a code that I can use?
    Thanks

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: String Builder

    Using the deleteCharAt() method works fine for me...

    What is the content of textTyped, and is it an object of type StringBuilder? Can you post the construction of the object? Also, how do you know that box with a question mark inside was appended to the StringBuilder? Did you print it?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    tjt
    tjt is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default Re: String Builder

    This is the rest of the code:
    Java Code:
    package withLevels;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.font.TextAttribute;
    import java.text.AttributedCharacterIterator;
    import java.text.AttributedString;
    import java.util.Random;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;
    import javax.swing.Timer;
    
    
    
    public class EasyLevel extends JPanel implements KeyListener,ActionListener 
    	{
    	
    		private String word = "";
    		private StringBuilder textTyped = new StringBuilder( "");
    		private int correct = 0;
    		private int chart = 0;
    		private boolean timerDone = false, checker = true;
    		
    		
    		AttributedString attributedString = new AttributedString(word);
    		
    		JFrame promptFrame, easyframe , menuframe;
    		JPanel promptPanel;
    		JLabel wordlabel , timerLabel , scoreLabel;
    		JButton pauseButton , resumeButton , gomenuButton, restartButton , exitButton  ;
    		Timer timer;
    		
    		Random hello = new Random();
    		
    		String[] wordList = new String [50];
    		
    		{
    		
    		wordList [0] = "bored";
    		wordList [1] = "clock";
    		wordList [2] = "hello";
    		wordList [3] = "brand";
    		wordList [4] = "faith";
    		wordList [5] = "sleep";
    		wordList [6] = "might";
    		wordList [7] = "while";
    		wordList [8] = "break";
    		wordList [9] = "still";
    		wordList [10] = "birds";
    		wordList [11] = "cream";
    		wordList [12] = "south";
    		wordList [13] = "north";
    		wordList [14] = "count";
    		wordList [15] = "frame";
    		wordList [16] = "phone";	
    		wordList [17] = "green";
    		wordList [18] = "cross";
    		wordList [19] = "cakes";
    		wordList [20] = "mirro";
    		wordList [21] = "steel";
    		wordList [22] = "steal";
    		wordList [23] = "doors";
    		wordList [24] = "rooms";
    		wordList [25] = "short";
    		wordList [26] = "stare";
    		wordList [27] = "books";
    		wordList [28] = "chair";
    		wordList [29] = "sound";
    		wordList [30] = "music";
    		wordList [31] = "songs";
    		wordList [32] = "sting";
    		wordList [33] = "light";
    		wordList [34] = "sling";
    		wordList [35] = "files";
    		wordList [36] = "calls";
    		wordList [37] = "games";
    		wordList [38] = "power";
    		wordList [39] = "happy";
    		wordList [40] = "paint";
    		wordList [41] = "colds";
    		wordList [42] = "cloud";
    		wordList [43] = "black";
    		wordList [44] = "lower";
    		wordList [45] = "bliss";
    		wordList [46] = "pants";
    		wordList [47] = "lease";
    		wordList [48] = "naval";
    		wordList [49] = "salon";
    		
    		word = wordList[hello.nextInt(50)] ;
    		
    		
    		}
    	
    	
    		public EasyLevel()
    			{
    				super();
    				
    				
    				
    				this.addKeyListener(this);
    				timerLabel = new JLabel("Timer: 120 seconds" , SwingConstants.CENTER);
    				promptFrame = new JFrame ("Typing Game");
    				promptPanel = new JPanel ();
    				gomenuButton = new JButton("Go Back to Menu?");
    				restartButton = new JButton ("Play Easy Level Again?");
    				exitButton = new JButton ("Exit Program");
    				scoreLabel = new JLabel ("Score: ");
    				
    				promptFrame.setVisible(false);
    				promptFrame.add(promptPanel);
    				promptPanel.add(scoreLabel);
    				promptPanel.add(gomenuButton);
    				promptPanel.add(restartButton);
    				promptPanel.add(exitButton);
    				
    				promptFrame.setSize(500,500);
    				gomenuButton.addActionListener(this);
    				
    				easyframe = new JFrame ();
    				easyframe.add(this);
    				//pauseButton = new JButton ("Pause");
    				//resumeButton = new JButton ("Resume");
    				
    				//this.add(resumeButton);
    				//this.add(pauseButton);
    				this.add(timerLabel);
    				this.setBackground(Color.pink);
    				int count = 120;
    				
    				timerLabel.setFont(new Font("Serif", Font.PLAIN, 25));
    				timerLabel.setText("Time Left: " + count);
    				timerLabel.setForeground(Color.white);
    				
    				TimeClass tc = new TimeClass(count);
    				
    				timer = new Timer(100, tc);
    				timer.start();
    			}
    	
    		public static void main (String [] args)
    			{
    			
    			TypingGame typing = new TypingGame();
    			
    			}
    		
    		
    		
    		
    	
    		public class TimeClass implements ActionListener
    			{
    			
    				int counter;
    				
    				public TimeClass(int counter)
    				{
    					
    				     this.counter = counter;
    				
    				}
    				
    					public void actionPerformed(ActionEvent tc)
    					{
    						//System.out.println("hello");
    					
    							counter--;
    							
    							if (counter >= 1)
    								{
    								
    									timerLabel.setText("Time Left: " + counter + " seconds");
    									
    								}
    							
    							else if (counter == 0)
    							{
    								timer.stop();
    								System.out.println(counter);
    								timerLabel.setText("Done!");
    								Toolkit.getDefaultToolkit().beep();
    								timerDone = true;
    								System.out.println("hi");
    								promptFrame.setVisible(true);
    							}
    					
    					
    					}
    				
    			}
    	
    		
    		@Override
    		public void keyPressed(KeyEvent arg0)
    			
    			{
    				// TODO Auto-generated method stub
    		    	int length = word.length();
    		    	
    		    		
    				  	if (arg0.getKeyChar()==KeyEvent.VK_ENTER)
    				  		{	
    				  		    //System.out.println("hello");
    				  		    System.out.println("word" + word);
    				  		    System.out.println("text:" + textTyped);
    				  			
    				  		    if (String.valueOf(textTyped).equals(word))
    				  			{
    				  			textTyped.delete(0,textTyped.length());
    							word = wordList[hello.nextInt(50)] ;
    							System.out.println("hello");
    							correct+= length * 3;
    							chart += length;
    							
    							System.out.println("hwya enter " + textTyped.length());
    							this.repaint();
    				  			}
    				  		}
    				    	
    				  	try
    				    	{
    							if (arg0.getKeyChar()==KeyEvent.VK_BACK_SPACE)
    								{
    									//text.deleteCharAt(text.length()-1);
    									
    								
    									//textTyped = String.valueOf(textTyped.replace(length-1, length, ""));
    									//textTyped = textTyped.toString().replaceFirst(textTyped.charAt(length-1), "0");
    									textTyped.deleteCharAt(textTyped.length()-1);
    									//textTyped.replace(textTyped.length()-1, textTyped.length(), "");
    									System.out.println("herhe backspace " + textTyped.length());
    									System.out.println(textTyped);	
    									correct-=2;
    									this.repaint();
    								}
    				    	}
    				    	
    				    catch (Exception e)
    				    	{System.out.println("error");
    		
    						}
    				  	
    				  	
    		    		
    
    			}
    	
    		
    		@Override
    		public void keyReleased(KeyEvent arg0) 
    			{
    				// TODO Auto-generated method stub
    	
    			}
    		
    		
    		@Override
    		public void keyTyped(KeyEvent arg0) 
    			{
    				// TODO Auto-generated method stub
    			
    			if (arg0.getKeyChar()!=KeyEvent.VK_ENTER)
    			
    			
    			//	System.out.println("entered keytype");
    				
    				textTyped.append(arg0.getKeyChar());
    				
    				try
    				{
    					if (textTyped.charAt(0)!= word.charAt(0))
    						checker = false;
    					else if (textTyped.charAt(0)== word.charAt(0)) checker = true;
    					
    					if (textTyped.charAt(1)!= word.charAt(1))
    						checker = false;
    					else if (textTyped.charAt(1)== word.charAt(1)) checker = true;
    					
    					if (textTyped.charAt(2)!= word.charAt(2))
    						checker = false;
    					else if (textTyped.charAt(2)== word.charAt(2)) checker = true;
    					
    					if (textTyped.charAt(3)!= word.charAt(3))
    						checker = false;
    					else if (textTyped.charAt(3)== word.charAt(3)) checker = true;
    					
    					if (textTyped.charAt(4)!= word.charAt(4))
    						checker = false;
    					else if (textTyped.charAt(4)== word.charAt(4)) checker = true;
    					
    					
    				}
    				
    				catch (Exception jaja )
    				{
    					
    				}
    			
    				//JOptionPane.showMessageDialog(null, "Timer Done!","Typing Game: Easy Level \nGo Back to Menu?(Yes) \nPlay Again (No)" , JOptionPane.YES_NO_OPTION);
    			}
    		
    		public void paint(Graphics g) 
    		{
    			super.paint(g);
    			
    			g.setFont(new Font("comic sans",Font.BOLD,20));
    			
    			//g.setColor(Color.blue);
    			//g.fillRect(5, 20, 270, 50);
    		
    			if (checker == false)
    				g.setColor(Color.orange);
    			else if (checker == true)
    			g.setColor(Color.blue);
    			g.fillRect(5, 30, 270, 50);
    			g.setColor(Color.white);
    			g.drawString("Word:", 10, 60);
    			g.drawString(String.valueOf(word), 100, 60);
    			
    			g.setColor(Color.green);
    			g.fillRect(5, 80, 270, 50);
    			g.setColor(Color.white);
    			g.drawString("Type:", 10, 110);
    			g.drawString(textTyped.toString(), 100, 110);
    			
    			g.setColor(Color.red);
    			g.fillRect(5, 130, 270, 50);
    			g.setColor(Color.white);
    			g.drawString("Score", 10, 160);
    			g.drawString(String.valueOf(correct), 100, 160);
    			
    			g.setColor(Color.yellow);
    			g.fillRect(5, 180, 270, 50);
    			g.setColor(Color.white);
    			g.drawString("Char", 10, 210);
    			g.drawString(String.valueOf(chart), 100, 210);
    			
    			
    			this.repaint();
    	
    
    		}
    
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			// TODO Auto-generated method stub
    			
    			
    			if (e.getSource()== gomenuButton)
    				{
    					Menu menuclass = new Menu();
    					menuframe.add(menuclass);
    					menuframe.setVisible(true);
    				
    				}
    		}
    
    		
    
    
    
    
    	}
    Yes I printed it in the console.. The box with a question mark inside is added to the length of the textTyped -_-
    Thanks for your help

  4. #4
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: String Builder

    Could you possibly provide a shorter piece of code that clearly demonstrates the issue? There is a lot of irrelevant information in the code that you posted.

    More info: Short, Self Contained, Correct Example
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  5. #5
    tjt
    tjt is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default Re: String Builder

    Java Code:
     try
                            {
                                if (arg0.getKeyChar()==KeyEvent.VK_BACK_SPACE)
                                    {
                                        //text.deleteCharAt(text.length()-1);
                                         
                                     
                                        //textTyped = String.valueOf(textTyped.replace(length-1, length, ""));
                                        //textTyped = textTyped.toString().replaceFirst(textTyped.charAt(length-1), "0");
                                        textTyped.deleteCharAt(textTyped.length()-1);
                                        //textTyped.replace(textTyped.length()-1, textTyped.length(), "");
                                        System.out.println("herhe backspace " + textTyped.length());
                                        System.out.println(textTyped);  
                                        correct-=2;
                                        this.repaint();
                                    }
                            }

  6. #6
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: String Builder

    I recommend that you read the link I provided you so that it is easier for us to help you. You will probably need to create another piece of code to satisfy the requirements of a SSCCE.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,184
    Rep Power
    20

    Default Re: String Builder

    From the API for that method:
    "If the character at the given index is a supplementary character, this method does not remove the entire character."

    Is this a supplementary character?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    tjt
    tjt is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default Re: String Builder

    What does supplementary character mean? :)

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,184
    Rep Power
    20

    Default Re: String Builder

    Are you using any fancy characters?
    Non-ASCII?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    tjt
    tjt is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default Re: String Builder

    No sir, I only use letters, since its a typing game.. :)

  11. #11
    tjt
    tjt is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default Re: String Builder

    String Builder-aint.png

    This is what happened.. I first typed "paint" then I deleted the "t" then "pain" came out of the console which is correct but then when I added the "t" again to make the word correct, it displays that character/symbol which makes the input incorrect

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: String Builder

    See #4 and #6.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    tjt
    tjt is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default Re: String Builder

    See #4 and #6.
    What do you mean by #4 and #6?

  14. #14
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: String Builder

    Quote Originally Posted by tjt View Post
    What do you mean by #4 and #6?
    Posts #4 and #6 on this thread.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,184
    Rep Power
    20

    Default Re: String Builder

    Posts.
    Because without an SSCCE it's going to be rather difficult for us to tell what's happening.

    Where do you add characters to textTyped?
    There's a lot of code up there (hence the need for a much shorter version for us to look at) and I can't see it.
    Essentially I'm wondering if the BACKSPACE character has ended up in the string builder.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Regarding string builder
    By swarupa_shet in forum Advanced Java
    Replies: 6
    Last Post: 03-29-2011, 07:45 AM
  2. String Builder
    By whateverme in forum New To Java
    Replies: 2
    Last Post: 12-14-2010, 02:07 AM
  3. String builder scramble word game
    By moncur in forum New To Java
    Replies: 4
    Last Post: 10-22-2010, 04:14 AM
  4. Finding and storing a word from a string builder
    By ao241 in forum Advanced Java
    Replies: 3
    Last Post: 06-28-2010, 01:46 PM
  5. string vs string builder??
    By j2vdk in forum New To Java
    Replies: 6
    Last Post: 09-08-2008, 10:38 AM

Posting Permissions

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