Solved

Command line Java

Posted on 2002-07-10
5
173 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now