Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Making Commands in Java

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Making Commands in Java

    Ok i am new at java programming. Im trying to write a program but have no code to make a program where a user types in a specific line of text and then java takes the correct information needed from the program i know how to call the method i just have no idea how to make commands please help


  2. #2
    Junior Member
    Join Date
    Oct 2013
    Location
    Chandler, Arizona, USA
    Posts
    25
    My Mood
    Devilish
    Thanks
    10
    Thanked 2 Times in 2 Posts

    Default Re: Making Commands in Java

    A good reference for starting is:

    The Really Big Index

    I am with you, I am learning this too. The method I am using is:
    First import:
    import java.util.Scanner;

    Then in the main part of the program:
    Scanner keyboard = new Scanner(System.in);
    		String input = "g";
    		// Get questions one by one, provide answer as each new question is started.
    		while(randNumberList.size() != 0 && !input.equals("q")){
    				System.out.print("Count: " + randNumberList.size() + "  --------------------------------------------------------------------");
    				qna = qaArrayList.get(randNumberList.get(0));
    				System.out.print("\nQuestion number " + qna.getQEntriesArray(0)  + ":\n" + qna.getQuestion());
    				if(randNumberList.size() > 0)
    				       randNumberList.remove(0);
     
    				input = keyboard.nextLine();
    				System.out.print(qna.getAnswer() + "\n");
    		  }
    		keyboard.close();
    		}

    Some of the code in the while loop is part of my question and answer
    program that reads a question from a file and then when you hit any
    key other than "q", it gives you the answer and the next question, so
    it is not directly related to the function of getting input.

    --- Update ---

    [/COLOR]So If I hack out some of the superfluous code:
    Scanner keyboard = new Scanner(System.in);
    		String input = "g";
    		while(!input.equals("q")){
    				System.out.print("Input what you want to . . . ");
    				input = keyboard.nextLine();
       			        // Do whatever it is you want to do
                                    // it might be a good place for a switch statement 
                                    // if you are using an up-to-date version of java:
                                    // using switch(input) 
                                    // an older version of Java will not accept a string in the switch statement!
    		  }
    		keyboard.close();
    		}

  3. The Following User Says Thank You to Skywola For This Useful Post:

    Camwarp (November 30th, 2013)

Similar Threads

  1. How to Make Java Commands
    By Camwarp in forum Java Theory & Questions
    Replies: 4
    Last Post: November 27th, 2013, 09:05 AM
  2. Execute Linux commands through java program.
    By ars42025 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 25th, 2013, 03:39 AM
  3. Sending commands through Java to Terminal
    By CountryGamer in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 9th, 2013, 02:31 AM
  4. simple java console program, need help recalling commands
    By zero0000000 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 22nd, 2010, 05:47 AM
  5. Executing Linux Commands with Java GUI
    By linuxrockers in forum AWT / Java Swing
    Replies: 2
    Last Post: February 15th, 2010, 10:57 PM