• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String search in file

 
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 inputting a file that has the system output when pinging an ip address. i have gotten it to read the file into an arraylist with readLine. Is there a way to have it input to list using spaces instead of lines?



I am hoping to get the arraylist to hold it like this:
(0)Pinging
(1)192.168.1.6
(2)with
(3)32
(4)bytes
(5)of
(6)data

Anybody with a knowledge bomb they can drop on me?
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what does it currently look like or possibly more importantly what is in the file you are reading in.
 
Matthew Joseph
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the file points to C:\command.txt
the output shows the normal ping output(shown below), but it is all in inLine(0)

Pinging 192.168.1.6 with 32 bytes of data:
Reply from 192.168.1.6: bytes=32 time<1ms TTL=128
Reply from 192.168.1.6: bytes=32 time<1ms TTL=128
Reply from 192.168.1.6: bytes=32 time<1ms TTL=128
Reply from 192.168.1.6: bytes=32 time<1ms TTL=128

Ping statistics for 192.168.1.6:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms

instead of programming all the different ping responses, i just want to look for "192.168.1.6:" three times. if it can ping 3 times, it has a good enough connection to return TRUE
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok so you want to read a line into a String, split() the string at every space character and copy the resultant array of words into the List.

I suggest you look at API docs for the split method of String.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Joseph wrote:instead of programming all the different ping responses, i just want to look for "192.168.1.6:" three times. if it can ping 3 times, it has a good enough connection to return TRUE


My answer above was in relation to your first post - this seems like a totally different scenario. Can you explain exactly what you want to achieve.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From your second response, I would think you don't need to split the string into an array. The line itself can be scanned for a match.

See if java.langString.contains(CharSequence s) will fit the bill. Try it out and post your code here.
 
Matthew Joseph
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i will check out langString, knute. thanks

Tony, this is my first time trying OOP, so i am doing my best to divide everything up into stand alone methods. its in my head, just not text yet. let me start fresh!
I am trying to make a program that will map our network and supply some info about the computers. The route i have taken is this:
i have 3 methods so far- runSystemCommand, ping, stringSearch
runSystemCommand- runs a cmd command(ping, systeminfo, etc) and directs output to C:\command.txt
ping- calls runSystemCommand, then calls stringSearch
stringSearch- inputs command.txt, and searches for string supplied by ping(or other methods i may make), returning the number of matches found

I am new to this, so it seemed like the best chance of success was to cycle through an array and seeing if it matches.I have just been trying to make the
methods as generic as possible so they can be reused. I hope i didn't just muddy the waters even more. it didn't seem quite so convoluted until i typed it out!
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I hope i didn't just muddy the waters even more. it didn't seem quite so convoluted until i typed it out!


This just shows why you shouldn't start coding until you have written out, in your native language, the exact problem you are trying to solve. Until you fully understand the problem you can't possibly know what you need to do and what you need to take account of.

Personally I would have thought the way to do this was to look for the line "Packets: Sent = 4, Received = 4, Lost = 0 (0% loss)," as that has all the information you need to know if the ping succeeded or not. Once you have found this line it is relatively easy to parse it to get the sent, received and lost values.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a hint: java.lang.* is imported by default, so you get java.lang.String for "free" (you don't have to do anything), and java.lang.String is the type of every String in Java, so you get contains() for free.
 
Matthew Joseph
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Personally I would have thought the way to do this was to look for the line "Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),"


Can't use it. whenever the destination host is unknown, it sends 4, receives 4, and loses 0-but the ip address sent back is the local machines.

So, I changed it around Knute. inLine is now a string. I concantenate the bf.readLine into it and then search for the ip. It works!
Originally, i was hoping to check the number of times it shows up, but even a single good ping will work for my purposes. The rest was just me
being anal. Thanks a lot!
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Joseph wrote:

Personally I would have thought the way to do this was to look for the line "Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),"


Can't use it. whenever the destination host is unknown, it sends 4, receives 4, and loses 0-but the ip address sent back is the local machines.


I've never seen a response like that from ping but then again I don't use it that much.


So, I changed it around Knute. inLine is now a string. I concantenate the bf.readLine into it and then search for the ip. It works!
Originally, i was hoping to check the number of times it shows up, but even a single good ping will work for my purposes. The rest was just me
being anal. Thanks a lot!


I take it you are checking for more than just contains the IP address as a failed ping message will contain the IP address in the first line (or maybe not given what you've said above).
If you do want to count the number of occurrences check out the indexOf() methods. There's one where you can specify where to start searching from.
 
Matthew Joseph
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, im searching for the ip address with ":" at the end. the only time ping outputs the address like that is when it succeeds. I will definitely check out the indexOf methods. thank you for pointing me in a good direction
 
Tony Docherty
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Joseph wrote:well, im searching for the ip address with ":" at the end. the only time ping outputs the address like that is when it succeeds.


In the output you showed earlier that also occurs on the Ping statistics line which on my ping tests also prints out when ping fails.
 
Matthew Joseph
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
good catch! i'll need to change my search string then. thanks
 
Matthew Joseph
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tony, thanks again for pointing that out. i changed it to indexOf, and required 2 or more matches for ping to be successful
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My pleasure. Glad to hear you have it working now.
 
I'm still in control here. LOOK at this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic