OK!!!
Next time, please be more careful with how you describe your problem. The more accurate and complete it is, the quicker you will get the right answer.
OK!!!
Next time, please be more careful with how you describe your problem. The more accurate and complete it is, the quicker you will get the right answer.
I ran your program. It does work OK. It does exactly what you just said it should do. There's something wrong with how you are using it in NetBeans. The program itself is OK.
After you see the prompt, click in the NetBeans output window (where you saw the prompt) and type the 10 numbers as instructed.
I just ran your code in NetBeans and it worked just fine.
You just told stultuske that your output was "enter 10 numbers", and told me that you did not see that prompt. Which is true?
Did you enter 10 numbers?
Netbeans is good.
Dd you see the prompt for "enter 10 numbers"?
Does it compile without errors?
Can you run it without getting an error message?
If you can run it, what data did you enter?
ps: You will do a lot better if you install the current Java Development Kit on your own machine.
I'll help you to fix it.
What exacetly does the compiler error message say?
use a for
loop
OK.
If you look at your code you will see:
the setAlpha method just sets that variable. It does not do anything to update the sum. You are still missing some of the code you were supposed to write.
Maybe this is your problem (or maybe not)
sum = alpha + bravo;
this does not define sum. It's an instruction that is executed as part of the method it's in. If you change alpha or bravo you need to execute that instruction (or one just like it) again. It would be great if you could define sum to be aplpha+bravo automatically whatever/whenever alpha or bravo are set, but that's not how it works.
Code Pane
ICMA42 pete = new ICMA42();
pete.sumArgs(); <<<<<<<<<<<<<<<<<<< recognises "pete"
this.setAlpha(42); <<< if you use pete here "it" will recognise it
I get the idea.
Your test code creates an instance of ICMA42 and uses that to call ICMA's sumArgs method. That method is nonsense, but the way you call it is 100% correct.
You need to use the same instance of ICMA42 to call your setAlpha method. Not this
, which is some other instance entirely.
The way you called sumArgs is the correct way to do it.
setAlpha is an instance method of the ICMA42 class. To call it you need an instance of ICMA42.
In your this.setAlpha
the this
refers to the current instance of whatever class that code was in, which obviously isn't ICMA42. That's what the error is syaing - whatever class this
is doesn't have a setAlpha method.
The way you called sumArgs is the correct way to do it.
Let's take this one step at a time:
Exactly which line of code does the error message refer to? (Post the whole error message - don't edit or summarise it)
That message was generated by some other piece of code - if you don't post that then nobody can help.
ps Your sumArgs method makes absolutely no sense whatsoever.
Yes, here's the code I used to confirm the problem...
String s = "1.2 3.4";
Scanner input = new Scanner(s); // my default Locale is UK
System.out.println(input.locale() + " " + input.nextDouble());
Locale.setDefault(new Locale("sv", "SE"));
input = new Scanner(s); // now using Sweden Locale
// input.useLocale(Locale.US);
System.out.println(input.locale() + " " + input.nextDouble());
without the useLocale
it throws an InputMismatch, with it it runs correctly.
The question is: why didn't OP see that Exception?
I suspect the Swedish locale uses the comma as the decimal separator. Your file has . as the decimal separator, so that may be causing a problem in reading floats from the file.
You could try setting your Scanner's locale to US, eg
input.useLocale(Locale.US);
/ and // worked no problem
Hmmm... I tried creating a File with // in the path, then printing its canonicalPath, and found the // to have been replaced by single / characters.
Looks like the File constructor is doing some extra parsing and correcting of file names.
That's interesting. I wouldn't expect that to work! Can you try it with single slashes?
The latest version is worse! You now have an empty catch block, so you will never know if the file was found or not. Alwyas use an e.printStackTrace();
until/unless you know it's right to do something else.
As for the slashes - you need a single slash in the path, regardless of windows vs linux. If its a back-slash you need to code that as a double back-slash because Java treats back-slash as an escape character in string literals. However, that's NOT true for a forward slash. Short version: replace \\
with /
, not //
Why are you printing the contents of weight
only if the file is not found? If you can't find the file then weight
will be empty!
Hint: Indenting your code properly isn't a luxury, or something you do only when the code is finished. It's an essential technique for getting your structure right in the forst place.
Yes, that looks like an empty array - which would explain the 0/0 == NaN
What code are you using to "use this JLabel in a JTextPane"?
NaN is most commony seen when you try to divide by zero, as may happen if the loop on line 8 above executes zero times, so line 12 is trying to print 0/0
I did understood that perfectly.
If you didn't have an explicit classpath then you had the default, which is the current working directory. Your new classpath does not include that value, so the current working directory is not in your new classpath, so your class file is not found.
"The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings."
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html
You just need to ADD servkey-api to your classpath, keeping all the existing entries as well.
Did you set CLASSPATH variable servlet-api
, or did you add servlet-api
to you classpath?
Maybe the problem is that you lost your previous classpath values?
Yes, just the usual try/catch structure. Create the ServerSocket inside the try, and catch any BindException
ps: I'm about to disappear on holiday for a week, so don't be surprised if I suddenly stop replying
J
I don't like the way that connection is updated in different methods - makes it hard to follow its value (especially if connectToPeer is sucessful).
Personally I would prefer to return a Socket from connectToPeer, or null if the connect fails. Then the mainline code would be much clearer, eg
Socket connection = connectToPeer();
if(connection != null){
display("Connected to server peer: " + connection.getInetAddress().getHostAddress());
} else {
display("Couldn't connect to peer...");
server = new ServerSocket(6789, 5);
connection = server.accept();
display("Connected to client peer at: " + connection.getInetAddress().getHostAddress());
}
OK, I can see that would work, although you will presumably need some way of updating that file. Remember that if you have two servers in the same LAN behind a NAT router and you want to access them from the WAN then they will have to be mapped to different ports on the router's IP, so you can't always have the same port for all servers.
Closing it in task manager will do the trick - and just by looking in TM you'll see if there are any ghost instances.
How will a new instance know the ip address(es) of existing servers to try to connect to them?
Don't have a 87 line listing. There's an 81 line listing, but line 18 of that is whileChatting();
so if that's it, it looks like the same as the other NPE?
OK, understand your architectural design now. That should work, although handling errors and shutdown/resart coluld be interesting.
If you end up with two ServerSockets at the same time (JVMBind error) it implies the second instance's attempt to connect as a client has failed.
If this were my program I would set that aside and construct the smallest possible program to test that try-to-connect-and-open-serversocket-if-that-fails design - there's too much going on in your code to see what's happening clearly.
ps: From bitter experience of testing Server implementations I know that its easy to end up with an instance of some failed test version still running and holding on to the port, even if it will no longer handle it properly. If you get weird problems, log out and log on again to terminate any "lost" live instances.
In that case one needs to be the client and the other the server. You can't do anything with just 2 clients or just 2 servers. If you want to have both functions in every instance then either have one machine per instance, or fake it temporarily by using different listen sockets for eacg server.
Line 18 is cln.printStackTrace();
- no way that throws an NPE!
Second NPE - the only variable that will throw an NPE on that line is output
, so you need to trace that back to find out why. (But first fix the empty catch block)
so in order to stop that i placed break; keyword
yes, but did you see my comment "You probably intended to break after finding a match."? YOur break is after NOT finding a match!
Are you trying to do the server and the client in the same program? That's going to cause problems. Don't you want multiple clients and one server?
Of course, if you try to start a second instance of the server on the same machine it will find the listening port already in use (by the first instance).
You also have an empty catch block, so I not going to waste any time looking at the code when there could be a perfectly informative Exception being ignored. Nor will I waste time looking for an NPE when you chose not to say which line it's on!
No, no need for any of that at all... just
ServerSocket serverSocket = new ServerSocket(...
Socket clientSocket;
while (true) {
clientSocket = serverSocket.accept();
new Thread(new ClientHandler(clientSocket)).start();
}
then in the client handler (see previous post) open the input and output streams from the Socket and start communicating.
OK
You'll need:
a Javax.swing.Timer that will run a small method at regular intervals. This is key component that controls the speed of your animation. See the API doc for details.
a variable that shows how many characters should be displayed (init to 1)
in you print method just print that number of characters
in your Timer method increment the character count and call repaint(); (this will trigger a call to your paint method)
It looks like you draw all the strings at the same position. You need to draw each string at increasing y coordinates so they appear one below another
Look at line 33.
As soon as you test a name that's not in the very first element of the array you break from the loop and stop looking.
You probably intended to break after finding a match.
You have an initial Thread that is a simple loop waiting for new connections to the ServerSocket.
When it gets one it opens a Socket and hands that over to a new Thread that handles the ongoing communications with that Socket. The original thread then just goes back to waiting for the next connection.
Ignoring error handling and shutting down, it's kinda like this (pseudo code):
main(...) {
...
new Thread( new ConnectionListener()).start();
...
}
class ConnectionListener implements Runnable {
run() {
while (true) {
wait for client to connect to ServerSocket
open new Socket with client
new Thread(new ClientHandler(socket)).start();
}
}
}
class ClientHandler implements Runnable {
Socket clientSocket;
ClientHandler(Socket clientSocket) {
this.clientSocket = clientSocket;
}
run() {
... do whatever with the client connection
}
}
You just copy/pasted your assignment without even a moment taken to explain what help you need. That's highly disrepestectful to the many people who give their time to help others here.
There are lots of people here who will freely give their time to help you become the best Java programmer you can be. There's nobody here who is interested in helping you cheat or doing your homework for you.
DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules
Post what you have done so far and someone will help you from there.
I'm not going to say it again...
"now it works"?
With the data you just posted there's no way permissions[operation] will fail with operation == 4.
Anyway, earlier you said permissions[0] gives "Alice", but that data is "Manager", and from what you said earlier people and roles have different files and (presumably) code.
You need to identify a test case that fails, and print permissions for that exact test case, not some other case that does work.
I keep saying "print the permissions array"!
It's obviously not what you expect, but if you see what it really is then you can work out why it's wrong.
The idea would be to print permissions
and operation
just BEFORE the exception, so you can see their values. But the message implies that operation == 4
and permissions.length <= 4
Looks like the permissions array isn't what you expected it to be?
ps Did you consider holding the permissions like this...
Alice:Start:Stop:Restart:Status:readConfig:setConfig:Print:Queue:topQueue
George:Print:Queue
I think the code would be a lot simpler (especially if you read that at startup into a Map user -> permissions, so a permissions check looks like
aclMap.getOrDefault(userName, "").contains(operation)
actionPerformed is called on the Swing thread, so the appends should be OK as they are.
Time for a bit more debugging!
Try printing the important values (eg. the contents of permissions
, the value of operation
) and see what tells us.
ps: why not simply store the permissions as pairs of strings (operation:permission) and avoid all that indexing and array referencing?
Your method should add one line to the text area for each bidder in the array list, so you need to debug the actionPerformed by adding a number of simple print statements to display the values of the main variables as the method executes. You need to answer questions like "what is the value of input
?", "what is in the bidders
aray?"
That code looks OK. What help do you need?
This is nothing to do with the compiler. The compiler has no way to know whether the array reference will be valid at run time. It's the Java runtime that checks every actual array index and throws the exception if it finds one out of range.
What is the value of "operation"? It looks like it can be >= length of the permissions array.
The code change you made will ensure that the permissions[operation]
will never be evaluated for an out-of-bounds value of operation, so that supresses the exception, but may not be the result you intended.
The ServerSocket listens on a given port/IP address. When it receives a connection request it creates a new Socket that you can use to communicate to/from the client. This is done by creating one or two new threads to handle inbound/oubound mesages from/to that client.
The ServerSocket then continues listening for new connections.
So one ServerSocket can create any number of Socket connections (depending on any limits in the operating system itself).
So to send messages to multiple recipients you need to create a Socket connection with each of them. Then you can send the same message to any or all of them.