Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Command line Java

Posted on 2002-07-10
5
Medium Priority
?
180 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
[X]
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
  • 3
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
Ovi earned 300 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.
Suggested Courses

610 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