Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Norm

Thread: What is wrong with this code?

  1. #1
    gkoef is offline Member
    Join Date
    Sep 2010
    Posts
    24
    Rep Power
    0

    Default What is wrong with this code?

    I am busy learning Java from a book and the coding it has given me seems to be wrong.

    Java Code:
    import java.io.*;
    import java.net.*;
    
    public class DailyAdviceServer
    {
    
    	String[] adviceList = {"Try to learn some Java today", "Check your Email", "Have you defragged me lately",
    				"Clean my system", "Learn some Java", "check on Open University details", "Wheres OP finance",
    				"Java Learning Day", "Be Happy", "Enjoy the Day"};
    
    	public void go()
    	{
    		try
    		{
    			ServerSocket serverSock = new ServerSocket(4242);		
    
    			while(true)
    			{
    				Socket sock = serverSock.accept();
    
    				PrintWriter writer = new PrintWriter(sock.getOutputStream());		
    				String advice = getAdvice();						
    				writer.println(advice);							
    				writer.close();								
    				System.out.println(advice);
    		}
    		catch (IOException ex)
    		{
    			ex.printStackTrace();
    		}
    	}
    
    	private String getAdvice()
    	{
    		int random = (int) (Math.random() * adviceList.length);
    		return adviceList[random];
    	}
    
    	public static void main(String[] args)
    	{
    		DailyAdviceServer server = new DailyAdviceServer();
    		server.go();
    	}
    }
    I could not work out how to copy and paste the errors from Command Prompt for this post

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    The error messages would help us out. Are you running the code using Windows OS? If so, you can right click it, select all and then copy it.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Also: Have you matched up all your curly braces, do all open-braces have a matching closing brace in the correct location?

  4. #4
    gkoef is offline Member
    Join Date
    Sep 2010
    Posts
    24
    Rep Power
    0

    Default

    Doh! I know how I forgot to miss that curly brace to close the try statement was because of the closing brace of the while() loop
    Thankyou:)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You're welcome!

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default

    Another way to get the contents of a command prompt window

    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    Fubarable likes this.

Similar Threads

  1. What is wrong with this code?
    By Mythreadings in forum New To Java
    Replies: 38
    Last Post: 11-19-2010, 12:43 AM
  2. What Could be Wrong with This code????
    By Manfizy in forum New To Java
    Replies: 9
    Last Post: 08-22-2010, 11:28 AM
  3. what is wrong with my code???
    By EBBOOO in forum New To Java
    Replies: 30
    Last Post: 07-27-2010, 09:59 PM
  4. What's wrong with this code?
    By Doctor Cactus in forum New To Java
    Replies: 4
    Last Post: 11-29-2008, 05:44 PM
  5. What's wrong with this code?
    By Wizard wusa in forum New To Java
    Replies: 14
    Last Post: 01-22-2008, 11:55 PM

Posting Permissions

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