Results 1 to 8 of 8
- 02-05-2011, 11:00 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 49
- Rep Power
- 0
"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:
RiskApp.javaJava 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); } }
When i tried it (this is the code for the nonworking code that i tried to add "boolean isDigit(char ch)" for: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(); } }); } }
RiskApp.java stayed the same so no reason to repost.
Risk.java:
I got two error messages...they are: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); } }
If (isDigit == false)Java Code:Risk.java:18: ';' expected
and
So it is the same error. If anyone could make this work i would be grateful.Java Code:Risk.java:26: ';' expected If (isDigit == false)
Thanks,
cc11rocks
-
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
Also, you're testing if isDigit is false before you've assigned anything to this variable which doesn't make logical sense.Java Code:if (foo == false) { //...
- 02-05-2011, 11:06 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 49
- Rep Power
- 0
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
- 02-05-2011, 11:08 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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...)
-
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.
- 02-05-2011, 11:14 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 49
- Rep Power
- 0
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
- 02-06-2011, 12:14 AM #7
Member
- Join Date
- Jan 2011
- Posts
- 49
- Rep Power
- 0
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
- 02-06-2011, 01:40 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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
-
Equivalent of "char *" in JAVA
By ABHIJEEEEEEET in forum New To JavaReplies: 3Last Post: 08-11-2010, 11:37 AM -
contentType="application/vnd.ms-excel" Not Working
By JordanParfitt in forum Advanced JavaReplies: 1Last Post: 02-06-2009, 03:16 PM -
pretty much completed hangman, but "new" button is not working
By diggitydoggz in forum New To JavaReplies: 10Last Post: 12-30-2008, 03:46 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks