Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

String Interger solution

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone know how to solve this problem?
i want a user to enter a number in a textfield then parse it as integer. this can be done with numbers that does not begin with zeros. eg 90000
will reflect 90000 but 00009 will only show 9.
this is the code:
String texttest = cardnum.getText();
int number;
if(texttest.equals("")){
JOptionPane.showMessageDialog(this,"Please enter card number for search");
cardnum.requestFocus();
return;
}
else{
char charArr[]=texttest.toCharArray();
for(int j=0;j<charArr.length;j++)
{
if(!(Character.isDigit(charArr[j])))
{
JOptionPane.showMessageDialog(this,"incorrect entry... Cardnumber should be digits");
cardnum.requestFocus();
return;
}
}
//outside of for loop call search procedure then add to tableResults.java
number = Integer.parseInt(texttest);
System.out.println(number+"");
}
does anyone have a solution?
 
Bartender
Posts: 1843
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a little bit unclear on your problem here. What exactly is not working/what do you want the program to do?
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm really not exactly sure what you're asking, but a very simple technique to use for parsing Strings into numbers is to wrap the parsing method in a try block and catch NumberFormatException:

Here's the results:
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could just as easily iterate through individual chars in the String object, rather than converting it to a char array. However, this is still more work than you need since, as noted above, Integer.parseInt() will throw a NumberFormatException if the String is not a valid integer.
Also, you stated that you would like a number with leading zeros to keep them. Why is this important? I think we need more context for this code to see how it fits in the rest of the program. If you can please give some more detail, we'll be glad to help you out.
Layne
 
Trelan Hylton
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am using the resulting number for a check on a database. it is possible for a cardnumber held in the database to be 0098 or something similar therefore it would be neccesary for the leeading zreos to remain in place.
my question is really how do i turn a string into an int with all the digits intact including the leading zeros?
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


90000 will reflect 90000 but 00009 will only show 9.


Are you using the input to query a database? What type is the card number field in the database? If it is a numeric type the query param 9 and 09 will have the same effect, if it's character type perhaps you should just parse the text field to verify if the format is correct and then continue to treat the text in the input field as a string, in which case 000009 will remain as 00009. Hope this helps.
Kevin
 
Joel McNary
Bartender
Posts: 1843
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed. Most databases will strip leading zeros from their numbers (I won't say all, because Joe's Database Company of Peoria might not ... ) If the data in the database reads "0098" then it is undoubtedly some sort of char/varchar. You would then be better off leaving your numbers as strings instead of converting them to Integers.
Some "numbers" are not really numbers, they are just strings of digits. As such, these are best represented in databases and code as a string type and not a number type. Prime examples are Credit card numbers and telephone numbers. I've run across large number of people that insist on making telephone numbers actual numbers. My general rule of thumb:
If you read off each digit individually (or in groups no higher than a hundred), keep it a string. If you pronounce the number as a whole, make it a number.
Example:
We don't say "dial eighteen billion five million five-hundred fifty-one thousand two-hundred twelve for information;" we say, "dial one eight-hundred five five five one two one two for information".
 
Trelan Hylton
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, you fellas are correct. i will change my code. thanx.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic