Solved

Command line Java

Posted on 2002-07-10
5
175 Views
Last Modified: 2010-03-31
In a effort to learn Java, how can one enter two numbers from command line user input followed by the addition of the two numbers?  
C:>javac Prog.java
22  <-- User inputs two numbers
4   <-- Program returns the sum of the two numbers.

Other things in Java appear to be easier than command line input.
0
Comment
Question by:vanuatu
  • 3
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
Ovi earned 75 total points
ID: 7145278
import java.io.*;

public class test1 {

 public static int read() {
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   String line = "";
   try {
     line = br.readLine();
   } catch(Exception e) {}
   return(Integer.parseInt(line));
 }
 
 public static void write(String message) {
   System.out.print(message);
 }
 
 public static void main(String[] args) {
   write("First number: ");
   int n = read();
   write("Second number: ");
   int n1 = read();
   write("Result: " + (n + n1) + "\n");
 }
}
0
 

Author Comment

by:vanuatu
ID: 7146208
Ovi,

Could you explain what each line of code above
public static void main(String[] args) does?
0
 
LVL 9

Expert Comment

by:Ovi
ID: 7148340
//imports classes to have access to IO operations
import java.io.*;

public class test1 {

// method for reading a line from jthe system inputStream. A line means an array of characters terminated with \n
 public static int read() {
   //creates a reader object to read from the inputStream
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  //will hold the readed content
   String line = "";
   try {
     //read a single line
     line = br.readLine();
   } catch(Exception e) {}
   //convert and return the readed string as a int value
   return(Integer.parseInt(line));
 }
 

//method for displaying messages to standard out
 public static void write(String message) {
//just write to system output
   System.out.print(message);
 }

// main method of the class, it specifies that this class is an "executable" one from operating system the command line.
 public static void main(String[] args) {
 //calls to methods for reading and writing
   write("First number: ");
   int n = read();
   write("Second number: ");
   int n1 = read();
   write("Result: " + (n + n1) + "\n");
 }
}
0
 

Author Comment

by:vanuatu
ID: 7154507
Thanks Ovi! :-)
0
 
LVL 9

Expert Comment

by:Ovi
ID: 7156379
Any time.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

777 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