|
 |
|
 |
05-19-2003, 05:13 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Apr 2003
Posts: 11
|
Input from command line
How do I input an integer from the command line. Or how do I convert a string to int?
|
|
|
05-21-2003, 05:16 PM
|
#2 (permalink)
|
|
bloomberg
Join Date: Jun 2002
Location: bloomberg
Posts: 263
|
Code:
class A {
public static void main(String[] args){
String in = args[0];
int i = Integer.parseInt(in); // throws invalidNumber or seomthing exception
}
}
run with:
__________________
-- bloomberg.
|
|
|
07-07-2003, 10:37 PM
|
#3 (permalink)
|
|
Registered User
Join Date: Mar 2003
Location: Pittsburgh
Posts: 45
|
If you want to take input, declare a new buffered reader. I can't thing of the syntax right now, but I was thinking
Code:
Bufferedreader reader = new Bufferedreader;
reader = System.in;
or something like that.
You could cast the int, too.
|
|
|
07-08-2003, 11:16 AM
|
#4 (permalink)
|
|
Registered User
Join Date: Apr 2003
Posts: 11
|
thx for replys
The first reply answered my question.
I dont think you can cast a string to an int, I tried and failed at that.
The syntax for the buffered reader is:
import java.io.*;
public class AClass
{
public static void main (String[] args) throws IOException
{
BufferedReader jin = new BufferedReader(new InputStreamReader(System.in));
|
|
|
07-11-2003, 11:00 AM
|
#5 (permalink)
|
|
Centurion Nova Prime
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 285
|
Hey, anyone interested in a CommandLine parsing class? I just wrote one recently (not heavily commented, of course) that takes, name=value style arguments, -y style switches, and property files (designated using: propfile=filename). The Apache folks have one, but it requires a little more overhead than I was interested in.
|
|
|
07-13-2003, 02:58 PM
|
#6 (permalink)
|
|
Code Monkey
Join Date: Jul 2003
Location: canada
Posts: 82
|
I wrote one that takes:
Code:
java theProg -a value_for_a -b "value for b" "and now the final variable"
below is current code:
Code:
int argc = args.length;
if(0 == argc){
show("Usage error: no arguments specified.\r\n" + usage);
return;
}
for(int k = 0; k < argc; k++){
char _switch;
if(args[k].length() == 2 && '-' == args[k].charAt(0)){
_switch = args[k].charAt(1);
} else { // final argument
if('"' == args[k].charAt(0)){
StringBuffer tmp = new StringBuffer();
tmp.append(args[k]);
while(k < argc && args[k].charAt(args[k].length() - 1) != '"')
tmp.append(" " + args[++k]);
/**
* Consider using StringBuffer.getChars(0, len, char[], 0);
*/
path = new String( tmp );
path = new String( path.toCharArray(), 1, path.length() - 2 );
} else
path = args[k];
break;
}
switch(_switch){
case 'm': /** Mode specifed */
if(++k >= argc){
show("Usage error: mode not specified.");
return;
}
char tmp_mode = args[k].charAt(0);
switch(tmp_mode){
case 's':
mode = MODE_SINGLE;
break;
case 'd':
flags = FLAG_DIR_ONLY;
/** Fall through */
case 'm':
mode = MODE_MULTI;
break;
default:
show("Usage error: unknown mode \"" + tmp_mode + "\".");
return;
}
break;
/** case 'm' ends */
case 'h': /** Help requested */
show(usage);
return;
/** case 'h' ends */
case 'o': /** Output path specified */
if(++k >= argc){
show("Usage error: output path not specified.");
return;
}
if('"' == args[k].charAt(0)){
StringBuffer tmp = new StringBuffer();
tmp.append(args[k]);
while(k < argc && args[k].charAt(args[k].length() - 1) != '"')
tmp.append(" " + args[++k]);
new_out_path = new String( tmp );
new_out_path = new String( new_out_path.toCharArray(), 1, new_out_path.length() - 2 );
} else
new_out_path = args[k];
break;
/** case 'o' ends */
case 's': /** Current output directories requested */
show("\tCurrent output directories:");
show("\t\tLocal: " + LOCAL);
show("\t\tMulti: " + DOCPATH);
return;
/** case 's' ends */
case 'v': /** Version requested */
show("TBCA Version: " + VERSION);
return;
/** case 'v' ends */
default:
show("Usage error: unknown switch \"" + _switch + "\".");
return;
/** default case ends */
}
}
__________________
direct entry file specification.
|
|
|
07-14-2003, 04:57 PM
|
#7 (permalink)
|
|
Code Monkey
Join Date: Jul 2003
Location: canada
Posts: 82
|
what does yours look like?
__________________
direct entry file specification.
|
|
|
07-15-2003, 06:34 AM
|
#8 (permalink)
|
|
Centurion Nova Prime
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 285
|
Mine takes:
Code:
java myprog propfile=filename user=techie pass=lin32 -y -b -u
propfile contains a list of name=value entries. It can also take comments.
I included a main to show basic use.
Code:
import java.util.*;
import java.io.*;
public class CommandLine {
StringTokenizer st;
Hashtable ht; //stores cmdline name-value pairs like "sid=actm03p"
Vector switches; //stores cmdline switches like "-n"
String [] cmdstr; // stores args[] from raw cmdline
/** Creates a new instance of CommandLine */
public CommandLine(String[] cmdstr) {
this.cmdstr = cmdstr;
ht = new Hashtable();
switches = new Vector();
parse();
propFile();
}
public boolean parse() {
String key = "";
String value = "";
int cnt = 0;
boolean flag = false;
for (int i=0; i<cmdstr.length; i++) {
st = new StringTokenizer(cmdstr[i], "=");
cnt = st.countTokens();
if (cnt == 2) {
key = st.nextToken();
value = st.nextToken();
ht.put(key, value);
flag = true;
} else if (cnt == 1) {
switches.add(st.nextToken());
flag=true;
} else {
flag = false;
}
}
return flag;
} // end of parse
public void propFile() {
String key = "";
String value = "";
if (checkKey("propfile")) {
// Read properties file.
Properties properties = new Properties();
try {
properties.load(new FileInputStream(getValue("propfile")));
Iterator it = properties.keySet().iterator();
//loop through properties and add to ht (hashtable) if not already present
while (it.hasNext()) {
key = (String)it.next();
value = (String)properties.get(key);
if (!checkKey(key)) {
ht.put(key, value);
}
}
properties.clear(); // empty properties object
} catch (IOException e) {
System.out.println("Failed propFile..." + e);
System.exit(2);
}
}
} // end of propFile
public void printPairs() {
String key = "";
String value = "";
Iterator it = ht.keySet().iterator();
while (it.hasNext()) {
key = (String)it.next();
value = (String)ht.get(key);
System.out.println(key + "=" + value);
}
} // end of printPairs
public void printSwitches() {
System.out.println("");
System.out.println("Switches = " + switches.toString());
} // end of printSwitches
public String getValue(String key) {
return (String)ht.get(key);
}
public boolean checkSwitch(String sw) {
return switches.contains(sw);
}
public boolean checkKey(String key) {
return ht.containsKey(key);
}
public static void main(String[] args) {
CommandLine cl = new CommandLine(args);
cl.printPairs();
cl.printSwitches();
// cl.getValue(somekey) returns the "value" of any name/value pair
//cl.checkSwitch("-y") returns true if "-y" was included on the commandline
}
}
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 05:56 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|