Results 1 to 8 of 8
  1. #1
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default "boolean isDigit(char ch)" not working...please help

    This is my code. I have two files, "Risk.java" and "RiskApp.java". They are both working fine. I was wondering how to insert "boolean isDigit(char ch)" in my code for BOTH JTextFields.
    Risk.java:
    Java Code:
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    
    public class Risk extends JPanel {
    int leftNumber = 0;
    int rightNumber = 0;
    public JButton enter;
    public JTextField stupid, yummy;
    public JLabel smart, dumb;
    public Risk() {
    JLabel smart = new JLabel("First Number:");
    add(smart);
    JTextField stupid = new JTextField(10);
    add(stupid);
    JLabel dumb = new JLabel("Second Number:");
    add(dumb);
    JTextField yummy = new JTextField(10);
    add(yummy);
    enter = new JButton("Enter");
    add(enter);
    
    }
    }
    RiskApp.java
    Java Code:
    import javax.swing.*; 
    
    public class RiskApp {
    private static void createAndShowGUI() {
    JFrame frame = new JFrame("Risk");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Risk riskPanel = new Risk();
    frame.getContentPane().add(riskPanel);
    
    frame.pack();
    frame.setVisible(true);
    }
    
    public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    createAndShowGUI();
    }
    });
    }
    }
    When i tried it (this is the code for the nonworking code that i tried to add "boolean isDigit(char ch)" for:
    RiskApp.java stayed the same so no reason to repost.
    Risk.java:
    Java Code:
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    
    public class Risk extends JPanel {
        int leftNumber = 0;
        int rightNumber = 0;
        public JButton enter;
    public JTextField stupid, yummy;
    public JLabel smart, dumb;
        public Risk() {
    JLabel smart = new JLabel("First Number:");
    add(smart);
    JTextField stupid = new JTextField(leftNumber, 10);
    add(stupid);
    boolean isDigit;
    If (isDigit == false)
     {
    System.out.println("Not a digit");
    }
    JLabel dumb = new JLabel("Second Number:");
    add(dumb);
    JTextField yummy = new JTextField(rightNumber, 10);
    add(yummy);
    If (isDigit == false)
     {
    System.out.println("Not a digit");
    }
            enter = new JButton("Enter");
            add(enter);
    	
        }
    }
    I got two error messages...they are:
    Java Code:
    Risk.java:18: ';' expected
    If (isDigit == false)
    and
    Java Code:
    Risk.java:26: ';' expected
    If (isDigit == false)
    So it is the same error. If anyone could make this work i would be grateful.
    Thanks,
    cc11rocks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    One problem: Java is case sensitive. If is not the same as if (the latter, if, is what you want). Also, there is no JTextField constructor that takes two int parameters. You'll probably want to pass a String and an int instead.

    Also, it's cleaner to do
    Java Code:
     
    if (!foo) 
    {
      //...

    rather than
    Java Code:
     
    if (foo == false) 
    {
      //...
    Also, you're testing if isDigit is false before you've assigned anything to this variable which doesn't make logical sense.

  3. #3
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default Don't know how to do this?

    rightNumber = 0, and leftNumber =0. That is in my code and also in the textboxes. So it should come up as 0 in the boxes before the user types anything in. Could you please show me how to do that? And BTW, this is not for my homework.
    Thanks,
    cc11rocks

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Also it might help to say what you are actually testing for.


    [Edit]
    (I've got work to do for a bit. But I see Fubarable's on the case...)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by cc11rocks View Post
    Could you please show me how to do that? And BTW, this is not for my homework.
    Thanks,
    cc11rocks
    I Recommend you try the suggestions I made. If you still have errors, or specific questions, show them and ask them. And it doesn't matter whether it's homework or not, you are better off coding it yourself. Again, as per your deleted question, please don't ask others to do your coding for you.

  6. #6
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default What I want

    My end goal is to get two text boxes and an enter button in a gui. When you push enter it display the two text box numbers underneath it. When you push certain keys, it does certain things to the numbers. I'm not worried about what the enter button or listeners or anything like that right now. I'm starting simple, then building big. RIGHT NOW on the other hand, i want to display two text boxes. Then, have the numbers 0 in each of them, hence leftNumber and rightNumber. When they type something, I want it to be limited to numbers only. And i want to have the Enter button displayed. That's about it.
    Sincerely,
    cc11rocks

  7. #7
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default Found another way, please delete

    I found another way to do this. Could you please delete this? I will make a new one because it deals with something totally different.
    Thanks and sorry,
    cc11rocks

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Mark the thread as solved, and it will be regarded as finished with.

    Before you do I'll just note for posterity that "I want it to be limited to numbers only" means that there is going to be listeners in there somewhere: watching what is typed and vetoing bad input. Those listeners may be obsured (eg made part of a Swing component) but they will be there somewhere.

    Also, in case anyone was wondering, it is not enough to assign a value to leftNumber and hope that the runtime was smart enough to display that in smart or whereever. You would have to do something like:

    Java Code:
    int leftNumber = 0;
    //...
    JTextField stupid = new JTextField("" + leftNumber, 10);
    add(stupid);

    The first argument to the JTextField constructor has to be a String not an int hence the ""+ business. See the API docs. Moreover the variables leftNumber and stupid will not be linked in any way whatsoever.

Similar Threads

  1. Equivalent of "char *" in JAVA
    By ABHIJEEEEEEET in forum New To Java
    Replies: 3
    Last Post: 08-11-2010, 12:37 PM
  2. contentType="application/vnd.ms-excel" Not Working
    By JordanParfitt in forum Advanced Java
    Replies: 1
    Last Post: 02-06-2009, 04:16 PM
  3. Replies: 10
    Last Post: 12-30-2008, 04:46 PM
  4. Replies: 1
    Last Post: 10-20-2008, 08:35 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
  •