Results 1 to 4 of 4
Like Tree2Likes
  • 2 Post By Singing Boyo

Thread: I messed up somewhere in my program, and I can't figure out where.

  1. #1
    Vinx is offline Member
    Join Date
    May 2012
    Rep Power

    Default I messed up somewhere in my program, and I can't figure out where.

    Full source code for the client application here: [Java] -
    I'd rather not work with it much yet, but server source is here: [Java] -
    Sorry if the way I format my code is horrible, I've been trying to improve. The looks at reading 10+ pages just to learn how to format comments doesn't seem so appealing and I'm not sure if it's worth it. That's the younger teenage generation for you I guess.

    Basically I have a Java application meant for roleplaying. A rather nerdy activity, I know

    There are four different chat modes. OOC, IC, ME, and IT. This is my first time really doing much in Java, so I'm aware that my program structure is bad. I'm sure that's why it's not working in the first place considering I'm not getting any errors with Eclipse when I run it. I want it so if you were to type SET:OOC into the chat, it would set rpModeSelection to OOC; etc.
    Java Code:
    	public class SendButtonListener implements ActionListener {
    		String rpModeSelection = "IC";
    		public void actionPerformed(ActionEvent ev) {
    			if (outgoing.getText() == "SET:OOC") {
    				rpModeSelection = "OOC";
    				incoming.append("RP Mode set to OOC");
    			} else if (outgoing.getText() == "SET:IC"){
    				rpModeSelection = "IC";
    				incoming.append("RP Mode set to IC");
    			} else if (outgoing.getText() == "SET:IT") {
    				rpModeSelection = "IT";
    				incoming.append("RP Mode set to IT Statement");
    			} else if (outgoing.getText() == "SET:ME") {
    				rpModeSelection = "ME";
    			} else 
    				//if (!outgoing.getText().equals("SET:OOC") || !outgoing.getText().equals("SET:IC") || !outgoing.getText().equals("SET:ME") || !outgoing.getText().equals("SET:IT")) 
    				try {
    				if (rpModeSelection == "OOC") {
    				writer.println("[OOC] " + charName.getText() + ": "+ outgoing.getText());
    				} else if (rpModeSelection == "IC") {
    					writer.println(charName.getText() + " says '"+ outgoing.getText() + "'");
    				} else if (rpModeSelection == "ME") {
    					writer.println(charName.getText() + " " + outgoing.getText());
    				} else if (rpModeSelection == "IT") {
    					writer.println("**" + outgoing.getText());
    			} catch (Exception ex) {ex.printStackTrace();} 
    		} }
    	} // close inner class
    I have multiple attempts of doing this, originally I wanted to create a JComboBox but I couldn't find any good tutorial on how they work or how to use them. Something about a array list seems messy, I don't like it.

    Also there's another issue with the server itself that has been bothering me. When a user closes the program to disconnect the server gets a SocketException. I wanted to make it seem nice and clean by catching this and saying "User has disconnected.", however I tried catching SocketException but it wouldn't let me.

    Also I forgot to mention this is a knockoff of a program in my book, I wanted to modify it for custom purposes as a fun project.

    Constructive criticism is welcome. Please keep in mind I started learning Java off and on (quite a bit of Battlefield 3[PC] in-between those periods ) since 05-18-2012. It hasn't been that long ago. Personally I'm happy with my progress because I have learning problems in real life, and to be learning programming is better than to be making outrageous sports claims at school bragging over worthless matters. I'd rather prepare for the future than the present. I wanted to thank Java-Forums for their continued support, it's made my learning experience a lot less stressful, and quite frankly enjoyable.

    I am aware that there are security flaws in my design, this is meant to be friends only so I don't care about them.

    Also, offtopic: Course | Programming Methodology - YouTube is a great playlist to learn fundamentals of Java programming.

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Rep Power

    Default Re: I messed up somewhere in my program, and I can't figure out where.

    Looks like the String == String problem. We've all run into it at some point. Replace all your 'stringX == stringY' with stringX.equals(stringY) and you should see something different. Essentially, == compares whether the two variables point to the same object. The string literals (like "Set:OOC") are separate objects from the string returned by outgoing.getText() and rpSelectionMode, so all your if statements are always going to be false. the equals method compares the value of the strings, so outgoing.getText().equals("SET:OOC") will return true if the user enters "SET:OOC". You might also want to look at using equalsIgnoreCase, if you don't care whether the user enters "SET:OOC" or "set:ooc" or "Set:OOC", and want to do the same thing for all of them.

    I'm assuming by 'it won't let me' you mean the SocketException isn't caught. Did you leave the printStackTrace() in? That will print the same thing, with your message before or after the stack trace depending on where you call System.out.println (or however else you display your message). If not, it'd probably be best to post the stack trace, and the code you tried that causes it.

    Constructive criticism: 'catch (Exception ex) {ex.printStackTrace();}' should probably get changed. You definitely want to do something about having an exception occur, even if that something is just a System.exit() call. (But don't call System.exit() in the ClientHandler of the server, your program would exit every time a client disconnects). printStackTrace() in a program you're sharing is also not good. For development it's fine, but when you start cleaning it up and making it user-friendly, change it to print or display something more understandable than a stack trace. ("Unexpected exception: " + ex.getMessage() is always a simple way to make it easier to understand.)
    Vinx and Fubarable like this.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    Vinx is offline Member
    Join Date
    May 2012
    Rep Power

    Default Re: I messed up somewhere in my program, and I can't figure out where.

    Quote Originally Posted by Singing Boyo View Post
    .................................................. .....
    Thank you, the fix worked flawlessly. Just have to update a few serverside things and my program is done. Much is appreciated, Singing Boyo. You helped fix a problem that's been bugging me for days.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: I messed up somewhere in my program, and I can't figure out where.

    Please go through the Forum Rules -- particularly the third paragraph.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. need help to figure out basic java program
    By shane123 in forum New To Java
    Replies: 21
    Last Post: 12-02-2011, 04:12 AM
  2. Replies: 4
    Last Post: 10-26-2011, 02:35 AM
  3. Can't figure out how to write this program.
    By Quinn in forum New To Java
    Replies: 18
    Last Post: 05-18-2011, 09:04 PM
  4. Replies: 10
    Last Post: 04-20-2011, 02:03 AM
  5. [SOLVED] cant figure this program
    By einstein1234 in forum New To Java
    Replies: 26
    Last Post: 04-23-2009, 04:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts