Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default Any Help Much Appreciated!

    Hello, in follow up to my previous thread Java Help Much Apreciated I'm having another minor problem with my code which is causing me much frustration. I've been asked to build a download centre, this version simply provides a description of each of the 3 items available. All the communications take place in the DownloadProtocol class in addition to the obvious Client and Server classes.

    My current issue is that after each description is shown the client is asked whether he/she would like another go, I'm struggling to implement this correctly, if "Y" is entered the client will return to SENTTERMS state where another option can be chosen otherwise the connection is closed if "N" is entered. I've played around with nested if's but don't seem to be making any progress. At current the way I've done it is bad practise since "Y" or "N" can be entered once the options have been sent not after a description shown.

    I'm a beginner at Java and completely stumped at this, so any help is most welcome.

    Java Code:
    package Version_1;
    
    //********************************************************************
    //    DownloadProtocol.java        Daniel Aspey-Smith
    //
    //    Description
    //
    //********************************************************************
    import java.net.*;
    import java.io.*;
    
    public class DownloadProtocol {
    
        private static final int WAITING = 1;
        private static final int SENTTERMS = 2;
        private static final int OPTIONSSENT = 3;
        private static final int SENTDESCRIPTIONS = 4;
        private int state = WAITING;
    
        public String processInput(String theInput) {
            String theOutput = null;
    
            switch (state) {
                case 1: {
                    theOutput = "Terms of reference. Do you accept? Y or N";
                    state = SENTTERMS;
                    break;
                }
                case 2: {
                    if (theInput.equalsIgnoreCase("Y")) {
                        theOutput = "1. computer program 2. picture 3. e-book";
                    } else if (theInput.equalsIgnoreCase("N")) {
                        theOutput = "Connection Closed";
                    } else {
                        theOutput = "Invalid input --- Terms of reference. Do you accept? Y or N";
                    }
                    state = OPTIONSSENT;
                    break;
                }
                case 3: {
                    if (theInput.equalsIgnoreCase("1")) {
                        theOutput = "The program displays a message      Another Y or N?";
                    } else if (theInput.equalsIgnoreCase("2")) {
                        theOutput = "This picture shows a frustrated computer user      Another Y or N?";
                    } else if (theInput.equalsIgnoreCase("3")) {
                        theOutput = "The book is about things we know we know, known unknowns "
                                + "and unknown unknowns      Another Y or N?";
                    } else if (theInput.equalsIgnoreCase("Y")) {
                        theOutput = "1. computer program 2. picture 3. e-book";
                    } else if (theInput.equalsIgnoreCase("N")) {
                        theOutput = "Connection Closed";
                    } else {
                        theOutput = "Invalid input --- Another? Y or N";
                        state = SENTTERMS;
                        break;
                    }
    
                }
            }
            return theOutput;
        }
    }
    Last edited by danielinthebed; 01-03-2012 at 08:43 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Any Help Much Appreciated!

    Can you write some pseudo code that describes what you are trying to do?
    Your comments above says what is to be done if "Y" is entered. What about if "N" or something else is entered?

    Can you change the case statements to use the final variables: WAITING, etc or change the state to use ints?
    state = 1;
    You have a mix that requires you to continually look for the definition of the variable and to compare it to the int value.

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

    Default Re: Any Help Much Appreciated!

    Please use a forum thread title that describes your problem. "Any help much appreciated" tells us nothing about your underlying problem, and many folks here will ignore such threads. If on the other hand your title is informative and briefly summarizes your problem (like a newspaper headline), you're much more likely to get an expert in the field that encompasses your problem.

  4. #4
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: Any Help Much Appreciated!

    So you just want to be able to loop the program around? the key word here being LOOP. And I'm guessing the variable to keep an eye on here is the "State" variable.

    while(state != something)
    {
    do code
    }

    That something could be state's value if the user inputs N. :)

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Any Help Much Appreciated!

    Recommended reading: Use meaningful, specific subject headers.

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

Similar Threads

  1. Stumped... any help would be appreciated!
    By gard007 in forum New To Java
    Replies: 3
    Last Post: 12-01-2011, 07:42 AM
  2. Help with this java code? help appreciated
    By jordan123 in forum New To Java
    Replies: 2
    Last Post: 11-06-2011, 06:06 PM
  3. Java need help! Help will be very much appreciated
    By paccerz in forum New To Java
    Replies: 1
    Last Post: 08-23-2011, 08:47 PM
  4. Comments and suggestions are much appreciated
    By khenzi07 in forum Advanced Java
    Replies: 5
    Last Post: 07-28-2011, 07:33 AM
  5. Problem with KeyPressed any Help is appreciated
    By gryd00 in forum New To Java
    Replies: 3
    Last Post: 05-17-2011, 05:15 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
  •