Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CHecking to see if a string can be casted to a char?

Status
Not open for further replies.

okiiyama

IS-IT--Management
Jan 3, 2003
269
US
after,

line = reader.readLine();

I want to see if the user typed in an integer, anything else typed in can be disregarded.

i.e
Type Something: dk39ko3 <---(That would be disregarded)
Type Something: 3322.87 <---(That would be disregarded)
Type Something: hello <---(That would be disregarded)
Type Something: 11245 <---(This would be an int)
Type Something: 11234e <---(That would be disregarded)

What can I use that will take a string then go character by character to see if I have all digits (no letters).

I ideas would be greatly appreciated.
 
here is a thought

find the length of the string
do this while loop
notFound = True


while (notFound && (index < line.length())
// use charAt to return the character at the specified
// index in the string

// use isDigit to check the character
// if it is a digit then notFound = false
// if it is not a digit index++
// the while loop will terminate if notFound = false or if you have checked the whole string
&quot;so many events, so little time&quot;
 
opps sorrie, the while loop is wrong for you problem,

change the part where is said
&quot;if it is a digit then notFound = false&quot;

instead
&quot;if it is NOT a digit then notFound = false&quot;

&quot;so many events, so little time&quot;
 
this class will compile and run and do what you want
I think, :)

public class ContainsOnlyInts
{
public static void main (String[] args)
{
boolean onlyInts = true;
checkValues: // Used to end
if ( args.length == 1 )
{
byte[] chars = args[0].getBytes();
for ( int i=0; i < chars.length ; i++)
{
byte value = chars;
// 48 == 0 and 57 == 9 in ascii
// you can use unicode also
if ( value > 57 || value < 48)
{
// 10 == newline
if ( value != 10)
{
onlyInts = false;
}
break checkValues;
}
}
}
else
{
System.err.println(&quot;Please enter 1 value&quot;);
onlyInts = false;
}
if (onlyInts)
{
System.out.println(&quot;Value entered contained only ints&quot;);
}
else
{
System.out.println(&quot;Values did not contain only ints&quot;);
}
}
}
 
sorry the line
byte value = chars;
should be char[j] with the j really being an i

I guess that is the how the software parses these posts
treats italics
 
Hi okiiyama:

Check this easy solution:

public boolean validate(String s){
try{
Integer.parseInt(s);
}
catch(NumberFormatException e){
System.out.println(&quot;This is NOT an integer -->&quot;+s);
return false;
}
System.out.println(&quot;This is an integer -->&quot;+s);
return true;
}


Hope it helps. By the way, excellent codes those above. Pedro Andrés Solorzano
Pontificia Universidad Javeriana
Bogotá, Colombia, SurAmérica.
 
A user will be entering in a &quot;ip address&quot; xxx.xxx.xxx. Its not a real Ip address where the numbers can be greater than 255 and there doesn't need to be two decimals. Right now I have it where the tokenizer breaks the string down into the parts seperated by decimals. Now from there I just need to make sure there are no letters in each token, and also make sure that there are no &quot;..&quot; in the string.

I need to validate the user's 'ip string' to make sure it is a possible Ip String.

Possible 'Ip Strings'
12.3
1.2.3
122.55.667
999.999.999 (no token can be greater than 999)
so, I need at least one decimal, but no more then 2.

I think Pedro so far is the closest, but maybe I can get help in how to limit the user's input would be great as well...

Thank yOu so far for your help.

Josh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top