Problem Statement:
Take input from command prompt.
Language:
C and Java
Platform:
Command Prompt
Command:
[Program] [Input]
Expected Input:
[Input] : Any Message
Note: message is always separated by Space!
Expected Output:
Message 1: [Input 1]
Message 2: [Input 2]
...
Example:
Interface in C:
C:\Program> ./read_cmd.exe reading from cmd
Interface in Java:
C:\Program> java read_cmd reading from cmd
Output (Same for both C and Java):
Message 1: reading
Message 2: from
Message 3: cmd
//use argument to take input------ In Java
ReplyDeleteclass HelloArgs{
public static void main(String args[])
{
for(int i = 0; i<3 ; i++){
System.out.println("Message " +(i+1) +" "+ args[i]);
//System.out.println("Message 2"+args[1]);
//System.out.println("Message 3"+args[2]);
}
}
}
//output
C:\Users\cli\Documents\Pushpa\Java test>java HelloArgs READING FROM CMD
Message 1 READING
Message 2 FROM
Message 3 CMD
//one more way to read input from cmd that is user bufferedReadline --- In java
import java.io.*;
class ReadString {
public static void main (String[] args) {
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br3 = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("Message 1");
System.out.println("Thanks for the Message 1, " + br2.readLine());
System.out.println("Enter Message 2");
System.out.println("Thanks for the Message 2, " + br2.readLine());
System.out.println("Enter Message 3");
System.out.println("Thanks for the Message 3, " + br3.readLine());
}
catch (IOException ioe) {
System.out.println("IO error trying to read your name!");
System.exit(1);
}
}
}
//output
C:\Users\cli\Documents\Pushpa\Java test>javac "ReadString .java"
C:\Users\cli\Documents\Pushpa\Java test>java ReadString
Message 1 read
Thanks for the Message 1, read
Enter Message 2
pushpa
Thanks for the Message 2, pushpa
Enter Message 3
ashish
Thanks for the Message 3, ashish
Error in Program HelloArgs:
ReplyDelete$ javac HelloArgs.java
$ java HelloArgs My Computer
Message 1 My
Message 2 Computer
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at HelloArgs.main(HelloArgs.java:8)
Comment on Program ReadString:
1. instead of using 3 InputStreamReader, only one InputStreamReader can be used.
2. hard limit of only 3 input can be change to variable, and a quit/exit command can me introduce.
I am posting two program in continuation of the given input.
ReplyDelete1. First Class ContainNumber contain one method which will check whether given input is number or not.
2. second class is based on the last comment.
//***************************************************
public class ContainNumber {
public static boolean onlyNumbers(String str) {
System.out.println("In side only Number method");
if (str == null || str.length() == 0){
System.out.println("You have not Enter any String");
return false;
}
for (int i = 0; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i))){
System.out.println("Please Enter positive Intger value");
return false;
}
}
System.out.println("Enteres Number is valid Number");
return true;
}
}
//*************************************************
import java.io.*;
class ReadString {
public static void main (String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Enter any Positive Intger Number");
String numberStr = br.readLine();
int num= 0;
if(ContainNumber.onlyNumbers(numberStr)){
num = Integer.parseInt(numberStr);
}
for(int i = 1 ;i<= num; i++){
System.out.println("Enter Message : " + i);
System.out.println("Thanks for the Message :"+i + " "+ br.readLine());
}
}
catch (IOException ioe) {
}
ioe.printStacktrace();
}
}
this is persect ... well done.
ReplyDeletehumm .. there is a concern, input of this program was expected to be given from the command line along with the program execution command. instead of this, your program is taking inputs during the exrcution. i think you need to change the method of the input.
ReplyDeleteexample of expected execution:
$ java ReadString Ashish Nigam Testing Program
--- that's it. input has already been given in the command line. now program is expected to understand the input and print it.
let me know if you still have confusion.