Pascal's Weblog
The Grid...



Archives
« November 2009
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
     
       
Today
Click me to subscribe
Search

Links
 

Today's Page Hits: 21

« Sun Inventory | Main
Monday May 19, 2008
Command line parameters
Here is the shortest example I could come up with to enter parameters on the command line (reader classes will be treated as just "it works this way" for now):

import java.io.*;

public class ReadString {

   public static void main (String[] args) throws Exception  {

      System.out.print("Enter your name: ");

      //  open up standard input
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      String name = br.readLine();

      System.out.println("Thanks: " + name);

   }

}
Posted at 11:46AM May 19, 2008 by Pascal Ledru in Java  |  Comments[1]

Comments:

Using java.util.Scanner, the code will be shorter, but probably slower :P But for short example this one works :

import java.util.*;

public class ScanString{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name : ");
String name = sc.next();
System.out.println("Thanks : " + name );
}
}

Too bad you did not allow even <pre> tag :P

Posted by Iseng on September 15, 2008 at 12:07 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed