The Artima Developer Community
Sponsored Link

Java Answers Forum
How To Get User Input from Command Prompt

9 replies on 1 page. Most recent reply: Apr 21, 2010 7:04 AM by dilip agarwal

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 9 replies on 1 page
leonglkw

Posts: 43
Nickname: leonglkw
Registered: Feb, 2004

How To Get User Input from Command Prompt Posted: Feb 14, 2004 10:18 AM
Reply to this message Reply
Advertisement
I am trying to write a program to accept user input.
This program will be run in Command Prompt.

My expected output is something as below :

Pls select option : (User will input S for Sales,
D for Daily Order,
C for Collection,
E for Exit)
Once the use input his/her input, If/Else will be used to verify the option chosen.
If the option chose are not one of the S, D, C or E,
the program will request the user input again.

I need to know how to write the code.
The help that all of you offered is most appreciated.
Thanks.


leonglkw

Posts: 43
Nickname: leonglkw
Registered: Feb, 2004

Re: How To Get User Input from Command Prompt Posted: Feb 14, 2004 10:51 AM
Reply to this message Reply
If there are any sample code, pls kindly post it!!!
Thanks a lot...

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: How To Get User Input from Command Prompt Posted: Feb 14, 2004 2:58 PM
Reply to this message Reply
Check out the sample at:
http://www.quantumhyperspace.com/JavaProgramming/JavaSourceBank/viewCode.jsp?javaFile=AgeProcessor.java

You can search or look fo more at"
http://www.quantumhyperspace.com/JavaProgramming/JavaSourceBank/index.jsp

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: How To Get User Input from Command Prompt Posted: Feb 14, 2004 3:09 PM
Reply to this message Reply
There isn't any built-in way to get input from the command prompt. Remember, Java is intended to be cross-platform, and NOT all platforms have a command prompt. This is one of the few areas where older languages, like BASIC, Pascal, or C/C++ have an advantage over Java.

However, since most Java beginners want some way to input from the command prompt, and most of us work on Windows (which does have a command prompt) many people have created custom classes that will do what you are trying to do. The ones that I have seen go by the name Keyboard.java or Console.java. If you do a search for either of those names, you should find something that you can download (usually for free) that will provide the functionality that you are looking for.

Another option is to look at the JOptionPane class. It has several convenient methods that can be used to create input and output dialog boxes. The documentation gives examples of how to use the class. If you haven't bookmarked or downloaded the documentation, I'd do that first.

Since the JOptionPane class is already installed on your computer (assuming that you have version 1.2 or above), using it will mean that you don't have to download anything extra. Of course, you won't have a pure console program any more either.

I hope this helps.

leonglkw

Posts: 43
Nickname: leonglkw
Registered: Feb, 2004

Re: How To Get User Input from Command Prompt Posted: Feb 14, 2004 8:37 PM
Reply to this message Reply
Hi guys, is there anymore sample codes out there?
Because still confusing...
Thanks...

Mike Sandman

Posts: 6
Nickname: n8behavior
Registered: Feb, 2004

Re: How To Get User Input from Command Prompt Posted: Feb 17, 2004 9:04 PM
Reply to this message Reply
this sounds like a "homework" question to me...lol

i'll help this much. you're on your own from there. j/k. if you need more help...let us know ;-)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
/*
 * Created on Feb 14, 2004
 */
 
/**
 * @author mike sandman
 */
public final class World {
 
	public static void main(String[] args) {
		privateMain(args);
	}
 
	private static void privateMain(String[] privateArgs) {
		System.out.println("Start typing, then press <return>");
		BufferedReader in =
			new BufferedReader(new InputStreamReader(System.in));
		String s = null;
		try {
			s = in.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Echo: " + s);
	}
}

shankar m

Posts: 1
Nickname: shankar898
Registered: Oct, 2009

Re: How To Get User Input from Command Prompt Posted: Oct 28, 2009 7:49 AM
Reply to this message Reply
Thanks a lot!!!
Your coding was really helpful for my practicals!!
great job!!
thank yo once again!!

keylogger J

Posts: 2
Nickname: keylogger
Registered: Apr, 2010

Re: How To Get User Input from Command Prompt Posted: Apr 21, 2010 4:36 AM
Reply to this message Reply
import java.io.*;

public class CommandLineInput
{
public static void main(String[] args) throws IOException
{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String s = bf.readLine();
System.out.println(s);
}
}

keylogger J

Posts: 2
Nickname: keylogger
Registered: Apr, 2010

Re: How To Get User Input from Command Prompt Posted: Apr 21, 2010 4:45 AM
Reply to this message Reply
import java.util.Scanner;

public class Command
{
public static void main(String[] args) throws IOException
{
Scanner in = new Scanner(System.in);
String s = in.nextLine();
System.out.println(s);
}
}

dilip agarwal

Posts: 8
Nickname: dagarwal17
Registered: Mar, 2010

Re: How To Get User Input from Command Prompt Posted: Apr 21, 2010 7:04 AM
Reply to this message Reply
import java.io.*;

public class FileIO
{

public static void main(String args[])
{
try
{
int flag = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter from the following options\n");
System.out.println(" S for Sales\nD for Daily Order\nC for Collection\nE for Exit");
while (flag == 0)
{
String choice = br.readLine();
if (choice.equals("s"))
{
System.out.println("you have choose option for salse");
flag = 1;
} else if (choice.equals("D"))
{
System.out.println("you have choose option for order");
flag = 1;
} else if (choice.equals("C"))
{
System.out.println("you have choose option for Collection");
flag = 1;
} else if (choice.equals("E"))
{
System.exit(0);
} else
{
System.out.println("you have not entered sufficient input please enter input again from the folloing options");
System.out.println(" S for Sales\nD for Daily Order\nC for Collection\nE for Exit");
}
}

} catch (IOException ie)
{
System.out.println("io exception=" + ie);
}
}
}

Flat View: This topic has 9 replies on 1 page
Topic: to work in design view in netbeans Previous Topic   Next Topic Topic: to set the time for response

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use