• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

exercise getServiceByName

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i'm at the beginning of java at university...
I have an exercise that i can't release...
the exercise is:
Write a program that access on the file etc/services and putting the name of a service (http, telnet ecc..), returns the port's name...

SORRY form my bad english
thank you
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paolo and welcome to the Javaranch.

We are NotACodeMill and we have a policy regarding homework questions (here).
We are willing to help you but we are not going to write it for you. So give it a try and if you're stuck report it here.
 
Paolo Chimenti
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok sorry...i've already done but it doeasn't work correctly...
the etc/service file is a file in every computer that contains service names like http, ftp, telnet ecc and corrispondent port..

 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code. It will highlight your code and make it much easier to read. It probably will also increase the number of people helping you. I'll add them for you since you are new so that you can see the difference.
 
Paolo Chimenti
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you!
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would rethink that regex and post the content of etc/services for the people who are not familiar with it (like me). Also could you edit your post and change the Italian to English because that is our main language here. You can edit your post with the button.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You say "it doesn't work correctly". What exactly does that mean? Can you compile the program? Can you run the program? What happens, and how does that differ from what you expected?

Note that a file like /etc/services only exists on computers that have Unix-like operating systems (for example Linux). I don't know if it exists on Windows, and if it does, the filename is certainly different than /etc/services.

In line 12 of your code you are opening the file "services". Since you don't specify any directory name, this will only work if the /etc directory is the current directory while you run the program. You could change line 12 to:

to make it work from any directory (not just the /etc directory).
 
Paolo Chimenti
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I put the file in eclipse directory, so the path is only the name of the file (services).
From yestarday I edited the program into:



The problem is the this program works only for the first line!







 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paolo Chimenti wrote:The problem is the this program works only for the first line!


That doesn't really help diagnose the problem. What, exactly, happens? Are there exceptions? If so, which ones and on which lines? What was the output, and what do you think it should have been? Also note that we don't know the contents of the file.
 
Paolo Chimenti
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if i put the rtmp name, it works correctly and the program returns the corrispondent port (1).
If i put an other name of service it returns always SERVICE NOT FOUND, so my program works correctly only for the first line of the file.
The file services is structured like this:
rtmp 1/ddp #Routing Table Maintenance Protocol
tcpmux 1/udp # TCP Port Service Multiplexer
tcpmux 1/tcp # TCP Port Service Multiplexer
..............
...........
ectc.

So when i insert for example rtmp it returns the number of the port (1) correctly.. when i put an other name like tcpmux it returns always SERVICE NOT FOUND...
I deduce it works correctly only for the first line..
I don't know why it reads only the first line of the file and not all lines....

I hope i explain the probelm better than before.
Thank you for the answers..
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're aware that the while loop will not be executed more than once due to the second break statement, right? I suspect you meant to enclose the else block in brackets.
 
Paolo Chimenti
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes but I can't understand why it read me only the first line.. With the while cicle in theory it should by read me all the lines...
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. The second "break" is always executed - which means the while loop terminates after the first iteration. What is that break statement supposed to do anyway?
 
Paolo Chimenti
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, i can't understand why and what i should edit...
I put the 2 breaks for stopping the :
service found
service found
service found
service found
service found
etc......

 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by "put" - did you remove both? The first one was perfectly OK, assuming that the loop should stop once the service is found.
If you made bigger changes then you should post the relevant code excerpt, so that we know what you're talking about.
 
Paolo Chimenti
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actualy mi code doesn't works. I don't know what i should edit. It works only for the first line.











 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code doesn't look like it would compile, but assuming that it is very close to what you actually have, ponder this: you're not using the value of "line" in the loop at all.
 
Paolo Chimenti
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the right code is (i was wrong in traslation):

 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Same problem: you're not using the value of "line" in the loop at all.
 
Paolo Chimenti
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used "line" in while loop... i' don't know where i should use it...
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"line" is assigned a value, but that value is never used.

Instead, "name" is used - which gets a value assigned exactly once, and that's outside of the loop.
 
Paolo Chimenti
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok I understand but what can i do for solve this problem?
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That the code works even for the first line is just accidental - because it contains two problems, which partially cancel each other out.

Problem #1 is that the value of "line" is not used in the loop. So the code that transforms "line" to "name" -which is used in the loop- must somehow become part of the loop.

Problem #2 is that you're reading the first line of the file outside of the loop, and then start the loop with reading the next line. As soon as you have fixed problem #1, you need to fix this one as well.
 
Paolo Chimenti
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok so for solve problem #1 in theory i should put

into while loop, is it correct?
but after do this:


RETURNS: Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
at esercizio4.main(esercizio4.java:32)
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 4 looks wrong - it seems to repeat the work of the StringTokenizer.

Generally, before you call StringTokenizer.nextToken you need to call StringTokenizer.hasMoreTokens to check if there are any more tokens. You should also print out the value of "line" to make sure that it is what you think it is.
 
Paolo Chimenti
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok , now i edited my program into:

 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lester Burnham wrote:Same problem: you're not using the value of "line" in the loop at all.

 
Paolo Chimenti
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes but editing into:


it RETURNS:
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
at esercizio4.main(esercizio4.java:32)
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I said:

Generally, before you call StringTokenizer.nextToken you need to call StringTokenizer.hasMoreTokens to check if there are any more tokens.



If you had implemented the second piece of my advice:

You should also print out the value of "line" to make sure that it is what you think it is.


you'd know what is being read. You could also print out the value of "name". Trying things on your own should also be quite a bit faster than running some code, stumbling on an exception, posting here, and then waiting for new ideas.
 
Paolo Chimenti
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I solved the exercise!

Thank you for your helps..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic