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
Solved

Command line Java

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
reverse digits of a number using for loop 5 52
Unhandled exception type Exception 18 42
ejb mdb examples 1 6
junit as external jar or library 7 22
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

808 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