aspose file tools*
The moose likes Java in General and the fly likes exercise getServiceByName Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "exercise getServiceByName" Watch "exercise getServiceByName" New topic
Author

exercise getServiceByName

Paolo Chimenti
Greenhorn

Joined: Nov 05, 2010
Posts: 15
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
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Paolo Chimenti
Greenhorn

Joined: Nov 05, 2010
Posts: 15
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
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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

Joined: Nov 05, 2010
Posts: 15
thank you!
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

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).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Paolo Chimenti
Greenhorn

Joined: Nov 05, 2010
Posts: 15
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!







Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
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

Joined: Nov 05, 2010
Posts: 15
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

Joined: Oct 14, 2008
Posts: 1337
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

Joined: Nov 05, 2010
Posts: 15
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

Joined: Oct 14, 2008
Posts: 1337
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

Joined: Nov 05, 2010
Posts: 15
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

Joined: Oct 14, 2008
Posts: 1337
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

Joined: Nov 05, 2010
Posts: 15
Actualy mi code doesn't works. I don't know what i should edit. It works only for the first line.











Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
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

Joined: Nov 05, 2010
Posts: 15
the right code is (i was wrong in traslation):

Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Same problem: you're not using the value of "line" in the loop at all.
Paolo Chimenti
Greenhorn

Joined: Nov 05, 2010
Posts: 15
I used "line" in while loop... i' don't know where i should use it...
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
"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

Joined: Nov 05, 2010
Posts: 15
ok I understand but what can i do for solve this problem?
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
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

Joined: Nov 05, 2010
Posts: 15
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

Joined: Oct 14, 2008
Posts: 1337
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

Joined: Nov 05, 2010
Posts: 15
ok , now i edited my program into:

Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Lester Burnham wrote:Same problem: you're not using the value of "line" in the loop at all.
Paolo Chimenti
Greenhorn

Joined: Nov 05, 2010
Posts: 15
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

Joined: Oct 14, 2008
Posts: 1337
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

Joined: Nov 05, 2010
Posts: 15
I solved the exercise!

Thank you for your helps..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: exercise getServiceByName