Java Input Numbers - java

I made ​​a method to input numbers. But I want that input is numeric but a dot (.) Can still be entered. please my friend help me. Thanks
private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
FilterHanyaAngka(evt);
}
public void FilterHanyaAngka(java.awt.event.KeyEvent evt) {
char c = evt.getKeyChar();
if (!((Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)))) {
evt.consume();
}
}

Don't use KeyListener to filter content to the a text component, you have no idea of knowing in what order the KeyListeners will be notified and the key stroke may already have being sent to the field before you.
Instead you should use a DocumentFilter
Take a look at Text Component Features, in particular Implementing a Document Filter and here for examples
In fact, I believe there is actually a numeric filter example listed there...

You can use ASCII instead of the Character or KeyEvent class.
Look at this:
public class ASCIITest {
public static void main(String[] args) {
char c = '3';
if ((c >= '0' && c <= '9') || c == '.') {
//some code..
}
}
}
For more information read about the ASCII chart

yes I've found the solution in my opinion
public void filterDesimal(java.awt.event.KeyEvent evt) {
char ch = evt.getKeyChar();
if (!((ch == '.') ||
(ch == '0') ||
(ch == '1') ||
(ch == '2') ||
(ch == '3') ||
(ch == '4') ||
(ch == '5')||
(ch == '6') ||
(ch == '7') ||
(ch == '8') ||
(ch == '9') ||
(ch == ',') ||
(ch == KeyEvent.VK_BACK_SPACE) ||
(ch == KeyEvent.VK_DELETE)
)){
evt.consume();
}
}

Related

java. do not accept 0 as first input in a text field

I'm using Java and this part of my code is for entering age in a text field that only accepts numbers, back spaces and delete. How can I also tell the code to avoid accepting 0 if its the first character ?
Thank you.
Here is the code:
private void tfAgeKeyTyped(java.awt.event.KeyEvent evt) {
char c = evt.getKeyChar();
if(!(Character.isDigit(c)) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)){
evt.consume();
}
}
Well you just need to check if your entered character isn't equal to 0 in your condition using c == '0' when the current input is empty:
if((this.currentInput.isEmpty() && (!Character.isDigit(c) || c == '0')) || !(Character.isDigit(c)) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)){
evt.consume();
}
private void tfAgeKeyTyped(final java.awt.event.KeyEvent evt) {
final char c = evt.getKeyChar();
// You need access to the current input to known if you are on the
// first character or not.
// Here I assume it exists as a private member variable.
final boolean isFirstChar = this.currentInput.isEmpty();
final boolean isValidEvent = (Character.isDigit(c) && !(isFirstChar && c == '0')) ||
(c == KeyEvent.VK_BACK_SPACE) ||
(c == KeyEvent.VK_DELETE);
if (isValidEvent) {
evt.consume();
}
}

Can't figure out why int is duplicating in Java

I am creatings a simple Java program that in the main class asks for a string (input) and then prints out how many vowels (int count) and consonants are in the string. The number of vowels works perfectly however the number of consonants double, so the string "James" has 2 vowels and 6 consonants according to my program.
public class counter {
vowels p1 = new vowels();
public int con = 0;
public int count() {
String input = p1.getInput();
int i = 0;
int count = 0;
while (i < input.length()){
if (input.charAt(i) == 'a' || input.charAt(i) == 'e' || input.charAt(i) == 'i' || input.charAt(i) == 'o' || input.charAt(i) == 'u') {
count++;
} else if (input.charAt(i) != ' ') {
con++;
}
i++;
}
return count;
}
public int con() {
return con;
}
}
You are using an instance member con for counting the consonants, and you don't initialize it at the beginning of the count method, so multiple calls to that method will result in invalid counts.
seems like you are using
int con=0;
is used for consonant count
so instead of using
else if (input.charAt(i) != ' ') {
con++;
}
simply use
else {
con++;
}
Alternate :
subtract the vowel count from string length
'com = P1.length()-count;'
Try to set the variable con to zero at the begining of the method "count".
con = 0;
I hope it works.
char ch;
for(int i = 0; i < str.length(); i ++)
{
ch = str.charAt(i);
if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' ||
ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
count ++;
else
con;
}
you had nod consider the case when vowels are in caps I solved this in my code
hope my code helps you in this regard thanks.Happy coding

How to stop this java.lang.ArrayIndexOutOfBoundsException?

I'm trying validate my jtextfeild to enter only money value. which include only numeric and a full-stop. ex-17652.50
So I tried this method. But while it is executing I got this java.lang.ArrayIndexOutOfBoundsException: 1
Here is the method.
private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {
try {
char c = evt.getKeyChar();
String mny[] = jTextField1.getText().split("\\.");
if (!((c >= '0') && (c <= '9') || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE) || (c == KeyEvent.VK_ENTER) || (c == KeyEvent.VK_TAB) || (c == KeyEvent.VK_NUM_LOCK) || (c == '.'))) {
getToolkit().beep();
evt.consume();
}
if (mny[1].length() == 2) {
getToolkit().beep();
evt.consume();
}
} catch (Exception e) {
e.printStackTrace();
}
}
I am getting Array Index Out of Bounds Exception after I typed the first number in textfeild. As I understand this is happening because mny[o] should occur after I enter fullstop. But I can't find a solution. Please help me.
Thank you.
Iy there is no dot in you input String mny[] = jTextField1.getText().split("\\."); will return an array with only one item. Arrays in java are zero based. So mny[1].length() will throw an ArrayIndexOutOfBoundsException.
You should check here if your array have a size of 2
if (mny.length > 1 && mny[1].length() == 2) {
You could change your condition to :
if (mny.length > 1 && mny[1].length() == 2) {
getToolkit().beep();
evt.consume();
}
(or something similar, depending on the required logic)

Cannot find symbol, Java and Strings

I've tried tinkering around with this for awhile and have yet to figure out what its giving me this error. The code is far from complete but I'm just trying to figure out why it says it can't find variable ch1. Any help is greatly appreciated!
public class PhoneNumber {
String phoneNumber;
public PhoneNumber(String num) {
phoneNumber = num;
}
public String decodePhoneNumber() {
// Takes string form phone number and decodes based on number pad
// Find code that makes if statement not care about caps
// so if a || b || c number[cnt] = 1 etc..
for (int cnt = 0; cnt < phoneNumber.length(); cnt++) {
char ch1 = phoneNumber.charAt(cnt);
if (Character.ch1.equalsIgnoreCase("a") || ("b") || ("c")) {
} else if (ch1.equalsIgnoreCase("d" || "e" || "f")) {
} else if (ch1.equalsIgnoreCase("j" || "k" || "l")) {
} else if (ch1.equalsIgnoreCase("m" || "n" || "o")) {
} else if (ch1.equalsIgnoreCase("p" || "q" || "r" || "s")) {
} else if (ch1.equalsIgnoreCase("t" || "u" || "v")) {
} else {
}
}
}
}
You have syntax errors and that is why you cannot find ch1.
Try modifying your code as per this syntax. These changes need to be done in all the conditionals.
if ((ch1 == 'a') || (ch1 == 'b') || (ch1 =='c')) {
If you want to make it work regardless of capital letters then you would need to normalize the input to lower case and then do the character comparison:
char ch1 = phoneNumber.toLowerCase().charAt(cnt);
if (ch1 == 'a' || ch1 == 'b' || ch1 == 'c') {
// Do something
}
...

KeyTyped event doesn't recognize colon key

I'm trying to only accept numbers (0...9) and the ':' (colon) key in a jTextField, but it doesn't accept the colon key. Why is that?
My code is:
private void horaInicioKeyTyped(java.awt.event.KeyEvent evt) {
char c=evt.getKeyChar();
if(!(Character.isDigit(c) || c== KeyEvent.VK_BACK_SPACE ||
c==KeyEvent.VK_DELETE || (c==KeyEvent.VK_COLON ))){
evt.consume();
getToolkit().beep();
}
}
Don't use VK_COLON, just use ':' like so -
if (Character.isDigit(c) || c==':' ||
c==KeyEvent.VK_BACK_SPACE || c==KeyEvent.VK_DELETE) {
evt.consume();
getToolkit().beep();
}

Categories

Resources