Results 1 to 15 of 15
Thread: String Builder
- 08-15-2012, 03:44 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 27
- Rep Power
- 0
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.
textTyped.deleteCharAt(textTyped.length()-1); <--- adds something different not totally deleting it. It adds a character. A box with question mark inside..Java Code:textTyped.deleteCharAt(textTyped.length()-1); textTyped.replace(textTyped.length()-1, textTyped.length(), "");
Can you suggest a code that I can use?
Thanks
- 08-15-2012, 04:01 PM #2
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
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
- 08-15-2012, 04:07 PM #3
Member
- Join Date
- Jul 2012
- Posts
- 27
- Rep Power
- 0
Re: String Builder
This is the rest of the code:
Yes I printed it in the console.. The box with a question mark inside is added to the length of the textTyped -_-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); } } }
Thanks for your help
- 08-15-2012, 04:14 PM #4
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
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
- 08-15-2012, 04:33 PM #5
Member
- Join Date
- Jul 2012
- Posts
- 27
- Rep Power
- 0
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(); } }
- 08-15-2012, 04:38 PM #6
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
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
- 08-15-2012, 04:57 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 08-16-2012, 05:02 AM #8
Member
- Join Date
- Jul 2012
- Posts
- 27
- Rep Power
- 0
Re: String Builder
What does supplementary character mean? :)
- 08-16-2012, 10:12 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: String Builder
Are you using any fancy characters?
Non-ASCII?Please do not ask for code as refusal often offends.
- 08-16-2012, 03:22 PM #10
Member
- Join Date
- Jul 2012
- Posts
- 27
- Rep Power
- 0
Re: String Builder
No sir, I only use letters, since its a typing game.. :)
- 08-16-2012, 03:36 PM #11
Member
- Join Date
- Jul 2012
- Posts
- 27
- Rep Power
- 0
- 08-16-2012, 03:58 PM #12
Re: String Builder
See #4 and #6.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-16-2012, 04:11 PM #13
Member
- Join Date
- Jul 2012
- Posts
- 27
- Rep Power
- 0
Re: String Builder
What do you mean by #4 and #6?See #4 and #6.
- 08-16-2012, 04:51 PM #14
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
- 08-16-2012, 04:55 PM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
Similar Threads
-
Regarding string builder
By swarupa_shet in forum Advanced JavaReplies: 6Last Post: 03-29-2011, 06:45 AM -
String Builder
By whateverme in forum New To JavaReplies: 2Last Post: 12-14-2010, 01:07 AM -
String builder scramble word game
By moncur in forum New To JavaReplies: 4Last Post: 10-22-2010, 03:14 AM -
Finding and storing a word from a string builder
By ao241 in forum Advanced JavaReplies: 3Last Post: 06-28-2010, 12:46 PM -
string vs string builder??
By j2vdk in forum New To JavaReplies: 6Last Post: 09-08-2008, 09:38 AM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks