Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Interactive prompts from command line?

Posted on 2002-04-24
6
379 Views
Last Modified: 2007-12-19
Hi there,

I have written a program that runs on a Unix commandline. Sadly I have to parse it over 15 arguments when I run it. Is there anyway that I can in my main method get the program to run and then print line a prompt statement telling a user which parameter to enter so that I can then pump this into a variable for use? ie on the command line I want to see the following:

>java comparator --run the program
>Please enter Value 1: 3 --user enters this value
>Please enter output dir: c:\oput\ --user enters this
>Please.... etc etc

I assume I will need some kind of While statement?

Does anyone have a clue what i'm on about? Can anyone help?

Many Thanks...............
0
Comment
Question by:luinzi
  • 4
  • 2
6 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 6965355
you can use

System.out.print("Please enter Value 1:");
System.in.read(...);
0
 

Author Comment

by:luinzi
ID: 6965371
Thats great but I want to enter strings.

This is what I have done so far:

          InputStream f;

          String a;

          while ((a = f.read()) != null)
          {
               System.out.println("Enter Value: ");

          }
          System.out.println("Value entered was: "+ a);

It thinks I'm trying to enter an integer?

C:\jdk1.3\bin\comparator.java:155: incompatible types
found   : int
required: java.lang.String
          while ((a = f.read()) != null)
                                  ^
1 error

I don't even know if this is the right way of going about it? How would I use the System.in.read?
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6965398
for reading you can use

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String st = reader.readLine();
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 16

Accepted Solution

by:
heyhey_ earned 70 total points
ID: 6965401
of course you must add some exception handling code

    try
    {
      BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
      String name = reader.readLine();
    }
    catch (IOException x)
    {
      x.printStackTrace();
    }
0
 

Author Comment

by:luinzi
ID: 6965413
Got it working! Thanks..................
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6965415
so why did you give me grade B ?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 58
Apps blocked by Java 9 88
hibernate jars 4 45
jsp insert to database example 2 46
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question