Beginner question with check true value???

Status
Nicht offen für weitere Antworten.

vieboi

Neues Mitglied
Hey guys!!! It's new Java beginner here... really need for help

do you know how to check a String value as Integer value?

For example I set:
String telephoneNum = "47396430854"; // Please note String tyle
then check the incoming telephone number that every digit between 0 to 9 // is it integer value?
If I set telephoneNum = "7462M37R479", the program throw exception.


---> And also the sencond question, check a double value is true of tyle double.

For example If I set the wrong value like
Double = 9999;
then program say the incoming value is not of type double and throw exception.


Sorry about my poor English, but it's only the language I can write here.
 

lhein

Top Contributor
vieboi hat gesagt.:
Hey guys!!! It's new Java beginner here... really need for help

do you know how to check a String value as Integer value?

For example I set:
String telephoneNum = "47396430854"; // Please note String tyle
then check the incoming telephone number that every digit between 0 to 9 // is it integer value?
If I set telephoneNum = "7462M37R479", the program throw exception.


---> And also the sencond question, check a double value is true of tyle double.

For example If I set the wrong value like
Double = 9999;
then program say the incoming value is not of type double and throw exception.


Sorry about my poor English, but it's only the language I can write here.


Hi,

for the first try this:

Code:
int i_phoneNr;

try
{
   i_phoneNr = new Integer(s_telephonNum).intValue();
}
catch (NumberFormatException ex)
{
   // if the string can't be cast to Integer, then this exception is thrown, so handle it as you want
   // for my example I only set the phoneNr to zero 
   i_phoneNr = 0;
}


for the second problem:

if I understood you correct then your program throws an error when assigning 9999 to your Double value.
first of all Double is an object and can't be set to a value in this way.
Take double instead like
Code:
double d = 9999d;
because double is a primitive datatype like int.
The d behind the number makes the number handled as a double value.

But you can also have this
Code:
Double doubl = new Double(9999d);

Hope this helps,

LR
 

bummerland

Top Contributor
vieboi hat gesagt.:
Hey guys!!! It's new Java beginner here... really need for help

do you know how to check a String value as Integer value?

For example I set:
String telephoneNum = "47396430854"; // Please note String tyle
then check the incoming telephone number that every digit between 0 to 9 // is it integer value?
If I set telephoneNum = "7462M37R479", the program throw exception.

Code:
int x;
try
{
    x = Integer.parseInt(telephoneNum);
}
catch(NumberFormatException)
{
    //It's no number
}

vieboi hat gesagt.:
---> And also the sencond question, check a double value is true of tyle double.

For example If I set the wrong value like
Double = 9999;
then program say the incoming value is not of type double and throw exception.

Code:
double d = 9999; //works
 

lhein

Top Contributor
Code:
int x;
try
{
    x = Integer.parseInt(telephoneNum);
}
catch(NumberFormatException ex)
{
    //It's no number
}

now it's perfect :)

LR
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
B Sodoku vs. Beginner Java Basics - Anfänger-Themen 3
S Beginner: Text formatiert speichern Java Basics - Anfänger-Themen 7
L Java [Beginner] Problem Java Basics - Anfänger-Themen 8
G Absolut Beginner: sh in bat umwandeln Java Basics - Anfänger-Themen 6
L absolute beginner . wie klassen strukturieren Java Basics - Anfänger-Themen 2
P Java beginner - Buch: Java- Einstieg für Anspruchsvolle Java Basics - Anfänger-Themen 6
T Absolut Beginner auf nem MAC OS 9.2 Java Basics - Anfänger-Themen 2
G java question Java Basics - Anfänger-Themen 3
E Check Java Basics - Anfänger-Themen 25
F Check ob ein Programm installiert ist Java Basics - Anfänger-Themen 4
C Check ob eine HashMap schon existiert Java Basics - Anfänger-Themen 16
R Check Box mit Array Java Basics - Anfänger-Themen 21
timbeau Javax.Mail: Check this out Java Basics - Anfänger-Themen 10
JTeacher Check your skills Java Basics - Anfänger-Themen 8
B de-Domain Whois-Check? Java Basics - Anfänger-Themen 9
Dit_ "Check for Updates" Funktion Java Basics - Anfänger-Themen 10
I Primzahlen check, String prüfen lassen. Java Basics - Anfänger-Themen 6
A Die Werte der ersten beiden markierten Check-Boxen registrieren Java Basics - Anfänger-Themen 11
Developer_X Wie kann man Check Boxes checken? Java Basics - Anfänger-Themen 10
G check-funktion mit java schreiben! Java Basics - Anfänger-Themen 3

Ähnliche Java Themen

Neue Themen


Oben