Home arrow static arrow Java Programming [Archive] - is there any smart way to do it ?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - is there any smart way to do it ?
This topic has 8 replies on 1 page.

Posts:1,367
Registered: 1/5/04
is there any smart way to do it ?  
Jun 18, 2004 9:28 AM



 
i want something like below....

i will read from a txt file line by line. File may be something like below....


file.txt



6

XXX YYY

ZZZ ttt

i will read the file line by line. If the line contains integer i will skip that line.

so something like below...

if(line contains integer) // how do i check this ?
// then do something or do nothing

else

// do someother thing.

My problem is how do i check whether a line contains integer or not ?

my attempt



i usued NumberFormatException and catch the exception if the string does not contain integer (as PaseInt will fails to convert String to integer)....

my question



is it a good way ? or someother smart simple way exists ?
 

Posts:24,036
Registered: 2/3/03
Re: is there any smart way to do it ?  
Jun 18, 2004 9:33 AM (reply 1 of 8)



 
yourString.matches(".*\\d.*");
 

Posts:10,972
Registered: 10/23/03
Re: is there any smart way to do it ?  
Jun 18, 2004 9:33 AM (reply 2 of 8)



 
i will read the file line by line. If the line contains integer i will skip that line.

Define "Integer". Is the following an "Integer"?

999999999999999999999999999999999999999999999999999999999999999999999999999999999
 

Posts:3,534
Registered: 9/30/99
Re: is there any smart way to do it ?  
Jun 18, 2004 9:47 AM (reply 3 of 8)



 
Define "Integer". Is the following an "Integer"?
9999999999999999999999999999999999999999999999999999999
9999999999999999999999999

and are these type of lines possible and are they intergers?

asdfjsda;l66778776ksfaksdf;lkajs
adfa 123456 afdasdfa
1Fh
AAh

 

Posts:50
Registered: 12/9/97
Use Java's Reg Exp library to define what you want to match  
Jun 18, 2004 1:01 PM (reply 4 of 8)



 
Here's a link to the tutorial - highly recommended. The online java.util.Pattern API also provides good info.

http://java.sun.com/docs/books/tutorial/extra/regex/

HTH,
Alexx
 

Posts:445
Registered: 4/17/03
Re: is there any smart way to do it ?  
Jun 18, 2004 1:43 PM (reply 5 of 8)



 
this might work, try this algorithm:

while(there are still bytes to read)
read bytes into a buffer until you get a '\n' (which connotes the end of a line)
for(each byte in the buffer)
int thisByte = Integer.intValue(new String(currentByteFromBuffer));
if(thisByte < 10 && thisByte > 0)
then skip this line

I think this would work, but you'll have to test it. It might have some lapses in logic, who knows. I
just came up with it off the top of my head. Hope it helps, or at least sparks some ideas...
 

Posts:445
Registered: 4/17/03
Re: is there any smart way to do it ?  
Jun 18, 2004 1:44 PM (reply 6 of 8)



 
this might work, try this algorithm:
while(there are still bytes to read)    read bytes into a buffer until you get a '\n' (which connotes the end of a line)    for(each byte in the buffer)         int thisByte = Integer.intValue(new String(currentByteFromBuffer));         if(thisByte < 10 && thisByte > 0)              then skip this line

I think this would work, but you'll have to test it. It might have some lapses in logic, who knows. I
just came up with it off the top of my head. Hope it helps, or at least sparks some ideas...
 

Posts:19,725
Registered: 9/26/01
Re: is there any smart way to do it ?  
Jun 18, 2004 2:13 PM (reply 7 of 8)



 
this might work, try this algorithm:
And this would be better than reply #1, because what?
 

Posts:3,258
Registered: 00-08-28
Re: is there any smart way to do it ?  
Jun 18, 2004 3:42 PM (reply 8 of 8)



 
this might work, try this algorithm:
And this would be better than reply #1, because what?

because that is not formatted I guess
 
This topic has 8 replies on 1 page.