Results 1 to 16 of 16
  1. #1
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

    Default Java word scramble -Trouble reading input

    Hey guys, I've been working on this word scramble game for days. I've read the entire book the professor assigned (Object Oriented Programming in Java) and searched the internet for a while. This is actually the third java forum that I've joined!

    Alright here it is. I need to read from a text box and depending on what the person types in do different actions. However I cannot get my text box input to register.

    The textinputbox i'm using extends inputbox and the only method it has is getValue which is a string. I don't have any of the key events listed yet because I don't have any variables to work with. I've tried assigned getValue to a string and scanning the string, but that doesn't seem to work. I've tried switching over to Jframe and using all of the Jtexfields and all that, but for some reason Vectors won't work. There may be some conflict in one of the packages but I can't find it.

    The file it's reading is a huge list of words and hints seperated by a colon and randomly shuffled through another class. If you would like that class as well just let me know.


    Here is my code:

    Java Code:
    import wheelsunh.users.*;
    import java.awt.event.*;
    import java.util.*;
    import java.io.*;
    
    public class WordApp extends Frame implements KeyListener, Animator
    {
    
      private AnimationTimer timer;
      private int seconds = 120;
      private String scrambled, current, word, hint;
      private TextBox textBox, hintBox;
      private int score = 0;
     
      private TextInputBox inputRow;
      private Vector <String> v_words, v_hints, phrases;
     
      
      
    public WordApp()
    {
      inputRow = new TextInputBox();
      inputRow.setLocation(200,100);
      
      phrases = new Vector <String>(FileUtilities.getStrings());
      v_hints = new Vector<String>();
      v_words = new Vector<String>();
      
      categorizeStrings();
      scrambledWord(word);
      textBox = new TextBox(200,100);
      textBox.setLocation(200,200);
      textBox.setText(scrambled + "\n\n Time Left: \t" + seconds + "\n Score: \t" + score);
      
      hintBox = new TextBox(200,200);
      hintBox.setLocation(300,400);
      hintBox.setText("Hint: " + hint);
      hintBox.hide();
    }
    
    public void categorizeStrings()
    {
      Iterator <String> categorize = phrases.iterator();
      while(categorize.hasNext())
      {
            
        String element = categorize.next();
       int index = element.indexOf(":");
        v_words.add(element.substring(0,index));
        v_hints.add(element.substring(index +1));
        
      }
     
    Iterator <String> words = v_words.iterator();
    word = words.next();
    
    Iterator <String> hints = v_hints.iterator();
    hint = hints.next();
      
    }
    
    public String scrambledWord(String something)
    {
    java.util.Random r;
     scrambled = "";
      for(int i = 0; i<word.length(); i++)
      {
       r = new java.util.Random();
       if(r.nextBoolean())
       scrambled = scrambled + word.charAt(i);
       else
       scrambled = word.charAt(i) + scrambled;
      }
      return scrambled;
    }
      
      public void keyTyped(KeyEvent e)
      {
          
      }
    
      public void keyPressed(KeyEvent e)
      {   
      }
    
      public void keyReleased(KeyEvent e)
      {
      }
    
    public void animate()
    {
      seconds -=10;
    }
    
    public static void main (String[] args)
    {
      
      WordApp app = new WordApp();
     
    }
    }
    Thanks in advanced guys!
    Last edited by Fubarable; 11-25-2011 at 06:55 PM. Reason: code tags added

  2. #2
    bigjo is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default Re: Java word scramble -Trouble reading input

    We don't have enough with this code I think. What is for example a TextBox and a TextInputBox. Why don't you use SWING instead of AWT, with a JTextField?

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default Re: Java word scramble -Trouble reading input

    import wheelsunh.users.*;
    You are using third party classes that not everyone has. If you were to use the java SE classes more people could help you with your code.

  4. #4
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

    Default Re: Java word scramble -Trouble reading input

    Thanks for responding.

    Like I said, I tried using swing instead of awt but for some reason I get errors with my vectors. And I don't know how to expand the height of the text fields. If I could correct the error with the vectors and find a way to change the height of the text fields I would go that route.

    As for the text boxes, they are extensions of rectangle that have default border width and default sizes as well as a protected text field. They have a few accessor and mutator methods related to those fields as well.

    At any rate, I don't need to use a text input box if there is a simpler way to go about being able to type information to the frame and have those characters be visible and be able to write some loops regarding what can go in and what happens when certain characters are typed.

    This all just needs to happen in a frame, it doesn't need to look fancy or anything it just needs to work.

    Thanks again for responding so quickly!

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default Re: Java word scramble -Trouble reading input

    I get errors with my vectors
    Please post the full text of the error messages here.
    change the height of the text fields I would go that route.
    Why do you need to change the height of a text field?
    Can you post code that compiles with java SE classes that shows your problem?

  6. #6
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

    Default Re: Java word scramble -Trouble reading input

    Well I've been working on two versions of the same program, one that extends frame and one that extends jframe. I have changed the code around on both of them so much that I can't really put the original error message down for the vector thing. I can't even get it to come up again because I've created a gui that handles half of the stuff and a few classes that handle the other half.

    Any way, to answer your question about the text fields. The book I'm reading created a LabeledTexBox class that I've been using on the Jframe program. It extends Jpanel but it's very thin and doesn't extend depending on the information it has. I have tried using other objects like textfields but I can't get them to become larger. The only option is to make the character size different. I can e-mail you what I have so far if anyone wants to look at the entirety of the program and run it themselves.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default Re: Java word scramble -Trouble reading input

    No need to email the program. Can you make a very small simple version with code that compiles, executes and demonstrates your problem and post that?

  8. #8
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

    Default Re: Java word scramble -Trouble reading input

    Sorry, I've been trying to make a small version but I can't seem to do it. I feel like it needs all the information I have. It's like I'm a mother hen and can't give up any eggs.

    Could you post a sample code of a textfield that is editable and has actions dependent on certain characters?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default Re: Java word scramble -Trouble reading input

    No hurry, take your time.

    What do the actions have to do with the height of the textfield?
    Last edited by Norm; 11-25-2011 at 09:44 PM.

  10. #10
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

    Default Re: Java word scramble -Trouble reading input

    Ok nevermind, I've decided to take out everything and just show the exact problem I have.
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class Main {
      public static void main(String[] args) {
        JTextField tf = new JTextField("The approximate value of pi is: 3.1415695");
        tf.setPreferredSize(tf.getPreferredSize());
        
    
        JPanel pHacked = new JPanel();
        pHacked.add(tf);
    
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new java.awt.GridLayout(5, 5));
        frame.add(pHacked);
        frame.setSize(200,200);
        frame.setVisible(true);
        tf.requestFocus();
      }
    }
    From this you can see that I need to set the height of the textfield so that the entire string can be displayed. Otherwise you need to scroll through it yourself.
    Last edited by Norm; 11-25-2011 at 09:45 PM. Reason: added code tags

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default Re: Java word scramble -Trouble reading input

    The text field is of finite length. You can always add a String too long.

  12. #12
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

    Default Re: Java word scramble -Trouble reading input

    So is there any way to wrap the text to increase the height? I just want it to be like the text box we are typing in here. Something that you can see the entire

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default Re: Java word scramble -Trouble reading input

    any way to wrap the text
    Use a JTextArea in a JScrollPane.
    If you want the text to wrap to a new line, you could add a newline character where you want it to go to the next line.
    Or you could use a JTextPane which will accept some HTML tags and will wrap the text.

  14. #14
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

    Default Re: Java word scramble -Trouble reading input

    Thanks for the help Norm.

    One last quick question. I'm trying to have the "enter" button register to the keytyped event. How would I put that in?

    The end result I'm looking for is the word I type in needs to be cleared when the enter button is hit. Also, is there a way for the "backspace" button or "delete" button to register as well?
    Thanks again for the help.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default Re: Java word scramble -Trouble reading input

    have the "enter" button register to the keytyped event.
    Have you tried printing out the events received by the keyPressed listener to see what events that method receives?

  16. #16
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

    Default Re: Java word scramble -Trouble reading input

    I have not tried that yet, I'll do that now.

    I'm nearly finished, thanks again for all your help Norm.

Similar Threads

  1. String builder scramble word game
    By moncur in forum New To Java
    Replies: 4
    Last Post: 10-22-2010, 03:14 AM
  2. Scramble Java Game
    By equinox55 in forum New To Java
    Replies: 3
    Last Post: 10-21-2010, 11:30 PM
  3. reading tables word document
    By ashik03 in forum Advanced Java
    Replies: 1
    Last Post: 02-06-2010, 01:01 AM
  4. Reading Microsoft Word Document in JAVA
    By satheeshtech in forum Advanced Java
    Replies: 0
    Last Post: 07-18-2009, 08:21 AM
  5. Word Scramble
    By lk9865 in forum New To Java
    Replies: 5
    Last Post: 11-17-2007, 02:22 AM

Tags for this Thread

Posting Permissions

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