Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Java
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 05-19-2003, 05:13 PM   #1 (permalink)
Kportertx
Registered User
 
Join Date: Apr 2003
Posts: 11
Kportertx is on a distinguished road
Input from command line

How do I input an integer from the command line. Or how do I convert a string to int?
__________________
Kportertx is offline   Reply With Quote
Old 05-21-2003, 05:16 PM   #2 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
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:
Code:
java A 40
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 07-07-2003, 10:37 PM   #3 (permalink)
Zenogear11
Registered User
 
Zenogear11's Avatar
 
Join Date: Mar 2003
Location: Pittsburgh
Posts: 45
Zenogear11 is on a distinguished road
Send a message via ICQ to Zenogear11 Send a message via AIM to Zenogear11
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.
__________________
Zenogear11 is offline   Reply With Quote
Old 07-08-2003, 11:16 AM   #4 (permalink)
Kportertx
Registered User
 
Join Date: Apr 2003
Posts: 11
Kportertx is on a distinguished road
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));
__________________
Kportertx is offline   Reply With Quote
Old 07-11-2003, 11:00 AM   #5 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 284
technobard is on a distinguished road
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.
__________________
technobard is offline   Reply With Quote
Old 07-13-2003, 02:58 PM   #6 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
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.
npa is offline   Reply With Quote
Old 07-14-2003, 04:57 PM   #7 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
what does yours look like?
__________________
direct entry file specification.
npa is offline   Reply With Quote
Old 07-15-2003, 06:34 AM   #8 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 284
technobard is on a distinguished road
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 } }
__________________
technobard is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Input a word, then spell it out backwards gamehead200 Standard C, C++ 2 10-23-2004 12:19 PM
paramerters in perl & an input problem Apodysophilia All Other Coding Languages 4 12-24-2003 11:30 AM
Multiline Input Prompt moremonks ASP, ASP.NET 1 06-25-2003 10:39 AM
dynamic allocation..urgent help needed!!! kashif Standard C, C++ 4 04-21-2003 08:50 AM
Input and out put, Q&A Apodysophilia Java 6 04-08-2003 03:47 PM


All times are GMT -8. The time now is 08:08 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle