Results 1 to 6 of 6

Thread: Java will not read input

  1. #1
    Join Date
    Oct 2010
    Posts
    16

    Java will not read input

    I have been working on a text-based Java games since last few days but I am getting a problem that I do not understand how to solve this. Here is my problem. The player gets a choice at the very beginning of the game, where he can get some info about the State and such. My problem is that when I run the game, and select the command option, I enter the info, and it only gives me two or three lines, pressing the Enter key on, but it does not work. How can I solve this problem?

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Java will not read input

    That's quiet strange but why didn't you provided the code of your game? It would have helped us to explain you the problem? Can you atleast provide some part of your code on what you think the problem lies? Moreover does the game is based on flash which is not loaded properly? It may be like your flash is not supported and thus getting the problem. Check your flash settings and make sure it is working properly.

  3. #3
    Join Date
    Oct 2010
    Posts
    16

    Re: Java will not read input

    Here is the Command function:

    Code:
    public void Command(){
                    boolean cmdFound = false;
                    while(!cmdFound){
                    System.out.print("Please enter the command, or type help for a list of commands:");
                    String cmd = bucky.nextLine();
                    if(cmd=="help"||cmd=="Help"){
                            baseFunction.CMDHelp();
                            cmdFound = true;
                    } else if(cmd=="Info" || cmd=="info"){
                            cmdFound = true;
                            System.out.print("Your character's name is ");
                            System.out.println(this.Name);
                            System.out.print("You are level ");
                            System.out.println(this.Level);
                            System.out.print("You have ");
                            System.out.print(this.XP);
                            System.out.println(" XP.");
                    } else {
                            System.out.println("Error: command not found");
                    }}
            }
    This is called by GetAction ():
    Code:
    public int GetAction(String Option1, String Option2, String Option3){
                    int Answer;
                    System.out.println("What do you want to do?");
                    System.out.print("1)");
                    System.out.println(Option1);
                    System.out.print("2)");
                    System.out.println(Option2);
                    System.out.print("3)");
                    System.out.println(Option3);
                    System.out.println("4)Different command");
                    Answer = bucky.nextInt();
                    return Answer;
            }
    Which in turn is called by a switch in the Main (String args [])

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Java will not read input

    An error that you have is because you're comparing strings with ==. Instead of this use minString.equals ("Something else"); possibly minString.equalsIgnoreCase ("Something else");. When you use == on objects, the objects must be of same type so that the comparison should work. The fact that two strings are equal does not mean they are the same object. Whether that's the problem you have now I do not know, but it was a bit difficult to understand what happened or not happened.

  5. #5
    Join Date
    Oct 2010
    Posts
    16

    Re: Java will not read input

    Thanks for your support and suggestions Praetor but it worked partially. Now I have another problem: the program believe that I press the Enter key before I do anything, and says "Error: command not found" which is what I have said it to say if it can not find command. Why is it taking things for granted and showing me this kind of error? How can I avoid this problem?

  6. #6
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Java will not read input

    I guess it has something to do with

    Code:
    String cmd = bucky.nextLine();
    What is Bucky? Check out what the next line(); is doing on this page so you may see the problem. Without knowing what type of Bucky is, it is difficult to give you some better tips. I would however use a BufferedReader and the method readline (). You can check out internet to get some documentation on the same for details.

Similar Threads

  1. Replies: 6
    Last Post: 13-01-2012, 10:08 PM
  2. Need to read input text and write it to output file
    By Tramba-keshwar in forum Software Development
    Replies: 6
    Last Post: 26-06-2011, 10:38 PM
  3. How to read user input from console using Scanner class in java?
    By Constantinee in forum Software Development
    Replies: 4
    Last Post: 06-02-2010, 06:47 PM
  4. How to use Console class to read user input in java?
    By MABON in forum Software Development
    Replies: 4
    Last Post: 04-02-2010, 10:17 PM
  5. how to get user input with char value in java
    By Bearer in forum Software Development
    Replies: 5
    Last Post: 07-01-2010, 05:51 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,972,140.59217 seconds with 17 queries